Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例

    xiaoxiao2021-04-16  32

    入门实例解析

    第一:provider-提供服务和相应的接口

     

    创建DemoService接口

    [java]  view plain copy print ? <span style="font-size:18px;">package com.unj.dubbotest.provider;      import java.util.List;      /**   * 定义服务接口,该接口需要单独打包,在服务提供方和消费方共享   *    * @author lishehe-2015年6月22日   *   */   public interface DemoService {       /*       * sayHello方法       */       String sayHello(String name);          /*       * 获取用户信息方法       */       public List getUsers();      }</span>  

     

    创建本接口的实现类

    [java]  view plain copy print ? <span style="font-size:18px;">package com.unj.dubbotest.provider.impl;      import java.util.ArrayList;   import java.util.List;      import com.unj.dubbotest.provider.DemoService;   /*   * 实现类DemoServiceImpl-李社河-2015年6月22日   */   public class DemoServiceImpl implements DemoService {       //声明sayHello方法       public String sayHello(String name) {           return "Hello " + name;       }   /**   * 获取用户信息getUsers-李社河-2015年6月22日   */       public List getUsers() {           List list = new ArrayList();           User u1 = new User();           //jack信息           u1.setName("jack");           u1.setAge(20);           u1.setSex("m");                      //tom信息           User u2 = new User();           u2.setName("tom");           u2.setAge(21);           u2.setSex("m");              //rose信息           User u3 = new User();           u3.setName("rose");           u3.setAge(19);           u3.setSex("w");              list.add(u1);           list.add(u2);           list.add(u3);           return list;//返回数据集合       }   }   </span>  

    创建provider.xml文件

    [html]  view plain copy print ? <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://code.alibabatech.com/schema/dubbo           http://code.alibabatech.com/schema/dubbo/dubbo.xsd           ">       <!-- 具体的实现bean,李社河 -->       <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />          <!-- 提供方应用信息,用于计算依赖关系,李社河  -->       <dubbo:application name="xixi_provider" />          <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"            /> -->          <!-- 使用zookeeper注册中心暴露服务地址 -->       <dubbo:registry address="zookeeper://127.0.0.1:2181" />          <!-- 用dubbo协议在20880端口暴露服务 -->       <dubbo:protocol name="dubbo" port="20880" />          <!-- 声明需要暴露的服务接口 -->       <dubbo:service interface="com.unj.dubbotest.provider.DemoService"           ref="demoService" />      </beans></span>  

    创建启动类

    [java]  view plain copy print ? <span style="font-size:18px;">public class Provider {          public static void main(String[] args) throws Exception {           //启动spring容器,把服务器启动之后注册到Zookeeper           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                   new String[] { "applicationContext.xml" });           context.start();           System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟       }   }</span>  

     

    创建dubboconsumer(消费者)

     注意 provider项目中的DemoService接口打包demo-service-api.jar放在class path中

     

    创建consumer.xml配置文件

     

    applicationContext.xml

    [html]  view plain copy print ? <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://code.alibabatech.com/schema/dubbo           http://code.alibabatech.com/schema/dubbo/dubbo.xsd           ">          <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->       <dubbo:application name="hehe_consumer" />          <!-- 使用zookeeper注册中心暴露服务地址 -->       <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->       <dubbo:registry address="zookeeper://127.0.0.1:2181" />          <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->       <dubbo:reference id="demoService"           interface="com.unj.dubbotest.provider.DemoService" />      </beans></span>  

    创建consumer启动类

    [java]  view plain copy print ? <span style="font-size:18px;">package com.alibaba.dubbo.demo.pp;      import java.util.List;      import org.springframework.context.support.ClassPathXmlApplicationContext;      import com.unj.dubbotest.provider.DemoService;   /**   * Consumer执行起始类   * @author 李社河-2015年6月22日   *   */   public class Consumer {          public static void main(String[] args) throws Exception {           //初始化Consumer中的spring容器           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                   new String[] { "applicationContext.xml" });           context.start();           // 获取远程服务代理           DemoService demoService = (DemoService) context.getBean("demoService");           //执行远程方法           String hello = demoService.sayHello("tom");           //打印信息           System.out.println(hello);              //同样执行远程方法,打印相关信息           List list = demoService.getUsers();           if (list != null && list.size() > 0) {               for (int i = 0; i < list.size(); i++) {                   System.out.println(list.get(i));               }           }           System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟       }      }</span>  

     

    启动好zookeeper、tomcat之后我们执行运行Provider.class、Consumer.class

    成功调到远程服务-----执行Consumer之后结果

     

    我们在看管理后台的信息

    提供者

     

    消费者

     

     

    实例升级--Dubbo服务集群容错实践

           手机应用是以聊天室为基础的,我们需要收集用户的操作行为,然后计算聊天室中在线人数,并实时在手机应用端显示人数,整个系统的架构如图所示:

     

       上图中,主要包括了两大主要流程:日志收集并实时处理流程、调用读取实时计算结果流程,我们使用基于Dubbo框架开发的服务来提供实时计算结果读取聊天人数的功能。上图中,实际上业务接口服务器集群也可以基于Dubbo框架构建服务,就看我们想要构建什么样的系统来满足我们的需要。

       如果不使用注册中心,服务消费方也能够直接调用服务提供方发布的服务,这样需要服务提供方将服务地址暴露给服务消费方,而且也无法使用监控中心的功能,这种方式成为直连。

       如果我们使用注册中心,服务提供方将服务发布到注册中心,而服务消费方可以通过注册中心订阅服务,接收服务提供方服务变更通知,这种方式可以隐藏服务提供方的细节,包括服务器地址等敏感信息,而服务消费方只能通过注册中心来获取到已注册的提供方服务,而不能直接跨过注册中心与服务提供方直接连接。这种方式的好处是还可以使用监控中心服务,能够对服务的调用情况进行监控分析,还能使用Dubbo服务管理中心,方便管理服务,我们在这里使用的是这种方式,也推荐使用这种方式。使用注册中心的Dubbo分布式服务相关组件结构,如下图所示:

     

    总结

            层层深入继续努力吧,随着项目的进行不断的和大家分享……

    源码来源: minglisoft.cn/technology

    转载请注明原文地址: https://ju.6miu.com/read-673006.html

    最新回复(0)