dubbo+zookeeper实现服务远程调用

来源:互联网 发布:linux复制文件和文件夹 编辑:程序博客网 时间:2024/05/29 07:56

dubbo场景:

           随着网站应用规模的扩大,服务越来越多,服务间依赖关系变得错综复杂,服务的调用量越来越大,服务的容量问题暴露出来,dubbo在这种情况下诞生。

dubbo是什么:

           是一种分布式服务框架,解决上面面对的问题,先看dubbo架构图:


节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

搭建zookeeper注册中心

搭建dubbo服务端,客户端,admin管理控制台(待整理)

创建及使用dubbo服务过程

dubbo提供者:

web工程pom文件下引入主要包:
[java] view plain copy


  1. <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-context</artifactId>  
  4.             <version>${spring-framework.version}</version>  
  5.         </dependency>  
  6.   
  7.         <dependency>  
  8.             <groupId>com.alibaba</groupId>  
  9.             <artifactId>dubbo</artifactId>  
  10.             <version>2.4.10</version>  
  11.             <exclusions>  
  12.                 <exclusion>  
  13.                     <artifactId>spring</artifactId>  
  14.                     <groupId>org.springframework</groupId>  
  15.                 </exclusion>  
  16.             </exclusions>  
  17.         </dependency>  
  18.   
  19.         <dependency>  
  20.             <groupId>com.101tec</groupId>  
  21.             <artifactId>zkclient</artifactId>  
  22.             <version>0.3</version>  
  23.         </dependency>  

[java] view plain copy


  1. 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)  
  2. package com.alibaba.dubbo.demo;  
  3.    
  4. public interface DemoService {  
  5.    
  6.     String sayHello(String name);  
  7.    
  8. }  
[java] view plain copy


  1. 在服务提供方实现接口:(对服务消费方隐藏实现)  
  2. package com.alibaba.dubbo.demo.provider;  
  3.    
  4. import com.alibaba.dubbo.demo.DemoService;  
  5.    
  6. public class DemoServiceImpl implements DemoService {  
  7.    
  8.     public String sayHello(String name) {  
  9.         return “Hello ” + name;  
  10.     }  
  11.    
  12. }  
[java] view plain copy


  1. 用Spring配置声明暴露服务:  
  2. <?xml version=”1.0” encoding=“UTF-8”?>  
  3. <beans xmlns=”http://www.springframework.org/schema/beans”  
  4.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”  
  5.     xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”  
  6.     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”>  
  7.    
  8.     <!– 提供方应用信息,用于计算依赖关系 –>  
  9.     <dubbo:application name=”hello-world-app”  />  
  10.    
  11.     <!– 使用multicast广播注册中心暴露服务地址 –>  
  12.     <dubbo:registry address=”multicast://224.5.6.7:1234” />  
  13.    
  14.     <!– 用dubbo协议在20880端口暴露服务 –>  
  15.     <dubbo:protocol name=”dubbo” port=“20880” />  
  16.    
  17.     <!– 声明需要暴露的服务接口 –>  
  18.     <dubbo:service interface=“com.alibaba.dubbo.demo.DemoService” ref=“demoService” />  
  19.    
  20.     <!– 和本地bean一样实现服务 –>  
  21.     <bean id=”demoService” class=“com.alibaba.dubbo.demo.provider.DemoServiceImpl” />  
  22.    
  23. </beans>  
[java] view plain copy


  1. 加载Spring配置:  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3.    
  4. public class Provider {  
  5.    
  6.     public static void main(String[] args) throws Exception {  
  7.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“http://dubbo.io/provider.xml”});  
  8.         context.start();  
  9.    
  10.         System.in.read(); // 按任意键退出  
  11.     }  
  12.    
  13. }  

说明:

   dubbo:registry 标签一些属性的说明:

      1register是否向此注册中心注册服务,如果设为false,将只订阅,不注册

      2check注册中心不存在时,是否报错。

      3subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅

      4timeout注册中心请求超时时间(毫秒)

      5address可以Zookeeper集群配置,地址可以多个以逗号隔开等。

  dubbo:service标签的一些属性说明:

     1interface服务接口的路径

     2ref引用对应的实现类的BeanID

     3registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A

     4register 默认true ,该协议的服务是否注册到注册中心。

dubbo消费者

[java] view plain copy


  1. 通过Spring配置<span style=“font-family: Arial, Helvetica, sans-serif;”>consumer.xml</span><span style=“font-family: Arial, Helvetica, sans-serif;”>引用远程服务:</span>  
  2. <?xml version=”1.0” encoding=“UTF-8”?>  
  3. <beans xmlns=”http://www.springframework.org/schema/beans”  
  4.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”  
  5.     xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”  
  6.     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”>  
  7.    
  8.     <!– 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 –>  
  9.     <dubbo:application name=”consumer-of-helloworld-app”  />  
  10.    
  11.     <!– 使用multicast广播注册中心暴露发现服务地址 –>  
  12.     <dubbo:registry address=”multicast://224.5.6.7:1234” />  
  13.    
  14.     <!– 生成远程服务代理,可以和本地bean一样使用demoService –>  
  15.     <dubbo:reference id=”demoService” interface=“com.alibaba.dubbo.demo.DemoService” />  
  16.    
  17. </beans>  
[java] view plain copy


  1. 加载Spring配置,并调用远程服务:(也可以使用IoC注入)  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3. import com.alibaba.dubbo.demo.DemoService;  
  4.    
  5. public class Consumer {  
  6.    
  7.     public static void main(String[] args) throws Exception {  
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“http://dubbo.io/consumer.xml”});  
  9.         context.start();  
  10.    
  11.         DemoService demoService = (DemoService)context.getBean(”demoService”); // 获取远程服务代理  
  12.         String hello = demoService.sayHello(”world”); // 执行远程方法  
  13.    
  14.         System.out.println( hello ); // 显示调用结果  
  15.     }  
  16.    
  17. }  

说明:

   dubbo:reference 的一些属性的说明:

      1interface调用的服务接口

      2check 启动时检查提供者是否存在,true报错,false忽略

      3registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>id属性,多个注册中心ID用逗号分隔

      4loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用

阅读全文
0 0