dubbo+zookeeper实现服务远程调用

Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和  Spring 框架无缝集成。

主要核心部件:

  • Remoting:  网络通信框架,实现了 sync-over-async 和 request-response 消息机制.
  • RPC:   (Remote Procedure Call Protocol远程过程调用协议), 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
  • Registry:  服务目录框架用于服务的注册和服务事件发布和订阅

Dubbo工作原理

说明:

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

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

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

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

Container: 服务运行容器。

调用关系说明:
0. 服务容器负责启动,加载,运行服务提供者。

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

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

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

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

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

 

代码实例:

一,dubbo提供者(服务提供者)

1、web工程pom文件下引入主要包:引入需要的jar包

 
  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>

2、定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)

 
  1.  
  2. package com.alibaba.dubbo.demo;

  3.  
  4. public interface DemoService {

  5.  
  6. String sayHello(String name);

  7.  
  8. }

3、接口实现:(对服务消费方隐藏实现)

 
  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. }

4、发布服务:

通过dubbo 建立service这个服务,并且到zookeeper上面注册,填写对应的zookeeper服务所在 的IP及端口号。

 
  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>

说明:

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

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

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

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

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

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

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

     1)interface服务接口的路径

     2)ref引用对应的实现类的Bean的ID

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

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

二、dubbo消费者(服务消费者)

通过dubbo引入需要调用的服务:

 
  1. 通过Spring配置consumer.xml引用远程服务:

  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>

调用服务(直接调用):

 
  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. }

 

转自:https://blog.csdn.net/weixin_42724467/article/details/88422800?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-7&spm=1001.2101.3001.4242

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值