探索分布式服务框架Dubbo4:Dubbo初体验

阿里 RPC 框架 DUBBO 初体验

Jun 7, 2018 | haifeiWu |  Java | 231 阅读

文章目录

  1. 1. 快速开始
  2. 2. 接口创建
  3. 3. 接口配置
  4. 4. 准备测试
  5. 5. 运行
  6. 6. 小结
  7. 7. 号外

最近研究了一下阿里开源的分布式RPC框架dubbo,楼主写了一个 demo,体验了一下dubbo的功能。

快速开始

实际上,dubbo的官方文档已经提供了如何使用这个RPC框架example代码,基于 Netty 的长连接。楼主看这个框架主要是为了在微服务,service mesh大火的今天做一些技术储备以及了解一下分布式 RPC 框架的设计。

当然即便是写一个dubbo的demo也不能随便写写就好了,要认真对待说不定哪一天可以派上用场呢,下面是楼主写的代码的目录结构:
dubboCode图dubboCode图

下面我来一一说明一下每个model的作用,

  1. micro-service-dubbo-common是通用工具模块其他的model都需要依赖它 。
  2. micro-service-dubbo-dal是整个项目的dao模块,有关数据库操作的相关代码都放在这里。
  3. micro-service-dubbo-interface 是通用接口模块,专门用来声明接口,被consumer与provider同时依赖,这么做是为了项目的可拆分与分布式部署。
  4. micro-service-dubbo-model是公用的实体类模块,不限于数据库对应的model,也可以放DTO,VO等。
  5. micro-service-dubbo-provider项目的服务提供者。
  6. micro-service-dubbo-web 项目的消费者,也是直接跟前端交互的controller层。

另外需要在pom文件中添加相关依赖

<!--dubbo-->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>${dubbo.version}</version>

</dependency>


<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>${zkclient_version}</version>

</dependency>


<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>${zookeeper_version}</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator_version}</version>
</dependency>

接口创建

既然是 RPC 服务,那就需要一个接口,再有一个实现类。在这里的接口定义是在我们的micro-service-dubbo-interface,具体实现是在provider这里创建,在楼主的项目中就是在micro-service-dubbo-provider中创建DemoService 的实现。

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

 
  1. public interface DemoService {

  2. String sayHello(String name);

  3.  
  4. public List getUsers();

  5. }

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

  10. 10

  11. 11

  12. 12

  13. 13

  14. 14

  15. 15

  16. 16

  17. 17

  18. 18

  19. 19

  20. 20

  21. 21

  22. 22

  23. 23

  24. 24

  25. 25

  26. 26

  27. 27

  28. 28

  29. 29

  30. 30

 
  1. @Service("demoService")

  2. public class DemoServiceImpl implements DemoService {

  3.  
  4. @Override

  5. public String sayHello(String name) {

  6. System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext

  7. .getContext().getRemoteAddress());

  8. return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();

  9. }

  10.  
  11. @Override

  12. public List getUsers() {

  13. List list = new ArrayList();

  14. User u1 = new User();

  15. u1.setName("hejingyuan");

  16. u1.setAge(20);

  17. u1.setSex("f");

  18.  
  19. User u2 = new User();

  20. u2.setName("xvshu");

  21. u2.setAge(21);

  22. u2.setSex("m");

  23.  
  24.  
  25. list.add(u1);

  26. list.add(u2);

  27.  
  28. return list;

  29. }

  30. }

然后consumer的 pom.xml 添加对这个接口的依赖,在这里的接口定义是在我们的consumer就是micro-service-dubbo-web

 
  1. <dependency>

  2. <groupId>com.whforever</groupId>

  3. <artifactId>micro-service-dubbo-provider</artifactId>

  4. <version>1.0-SNAPSHOT</version>

  5. </dependency>

  6. <dependency>

  7. <groupId>com.whforever</groupId>

  8. <artifactId>micro-service-dubbo-interface</artifactId>

  9. <version>1.0-SNAPSHOT</version>

  10. </dependency>

有了接口,就需要配置一下。

接口配置

首先在提供方这里发布接口。创建一个 xml 文件,名为:dubbo-provider.xml

文件内容:

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

  10. 10

  11. 11

  12. 12

  13. 13

  14. 14

  15. 15

  16. 16

  17. 17

  18. 18

  19. 19

  20. 20

  21. 21

  22. 22

  23. 23

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  4. xmlns="http://www.springframework.org/schema/beans"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

  6. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  7.  
  8. <!-- provider's application name, used for tracing dependency relationship -->

  9. <dubbo:application name="demo-provider"/>

  10.  
  11. <!-- use multicast registry center to export service -->

  12. <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

  13.  
  14. <!-- use dubbo protocol to export service on port 20880 -->

  15. <dubbo:protocol name="dubbo" port="20880"/>

  16.  
  17. <!-- service implementation, as same as regular local bean -->

  18. <bean id="demoProviderService" class="com.whforever.service.impl.DemoServiceImpl"/>

  19.  
  20. <!-- declare the service interface to be exported -->

  21. <dubbo:service interface="com.whforever.service.DemoService" ref="demoProviderService"/>

  22.  
  23. </beans>

很简单,发布了一个接口,类似 Spring 的一个 bean。

同样的,在consumer即micro-service-dubbo-web的 resource 文件下,也创建一个dubbo-consumer.xml文件。内容稍有不同。

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

  10. 10

  11. 11

  12. 12

  13. 13

  14. 14

  15. 15

  16. 16

  17. 17

  18. 18

  19. 19

  20. 20

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  4. xmlns="http://www.springframework.org/schema/beans"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

  6. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  7.  
  8. <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),

  9. don't set it same as provider -->

  10. <dubbo:application name="demo-consumer"/>

  11.  
  12. <!-- use multicast registry center to discover service -->

  13. <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->

  14. <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

  15.  
  16. <!-- generate proxy for the remote service, then demoService can be used in the same way as the

  17. local regular interface -->

  18. <dubbo:reference id="demoConsumerService" check="false" interface="com.whforever.service.DemoService"/>

  19.  
  20. </beans>

从上面可以看出这两个文件的注册发现协议是zookeeper,因此在服务启动之前需要启动zookeeper,具体移步Zookeeper 注册中心安装启动

准备测试

测试之前还要做点点工作。

在启动provider事需要一部分引导程序,请看如下代码:

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

 
  1. public class ProviderMain {

  2. public static void main(String[] args) throws IOException {

  3. System.setProperty("java.net.preferIPv4Stack", "true");

  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");

  5. context.start();

  6.  
  7. System.in.read(); // press any key to exit

  8. }

  9. }

 

consumer代码

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

  10. 10

  11. 11

  12. 12

  13. 13

  14. 14

  15. 15

 
  1. @Controller

  2. @RequestMapping("/")

  3. public class IndexController {

  4.  
  5. @Autowired

  6. DemoService demoService;

  7.  
  8. @RequestMapping("/echo")

  9. @ResponseBody

  10. public String echo() {

  11. System.out.println(">>>>>>echo");

  12. return JSON.toJSONString(demoService.getUsers());

  13. }

  14. }

 

运行

先运行provider:

 
  1. 1

  2. 2

  3. 3

  4. 4

  5. 5

 
  1. [06/06/18 11:56:29:029 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.whforever.service.DemoService, dubbo version: 2.6.1, current host: 192.168.1.120

  2. [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.whforever.service.DemoService to local registry, dubbo version: 2.6.1, current host: 192.168.1.120

  3. [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.whforever.service.DemoService to url dubbo://192.168.1.120:20880/com.whforever.service.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.120&bind.port=20880&dubbo=2.6.1&generic=false&interface=com.whforever.service.DemoService&methods=sayHello,getUsers&pid=13992&side=provider&timestamp=1528300589682, dubbo version: 2.6.1, current host: 192.168.1.120

  4. [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.whforever.service.DemoService url dubbo://192.168.1.120:20880/com.whforever.service.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.120&bind.port=20880&dubbo=2.6.1&generic=false&interface=com.whforever.service.DemoService&methods=sayHello,getUsers&pid=13992&side=provider&timestamp=1528300589682 to registry registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.6.1&pid=13992&registry=zookeeper&timestamp=1528300589673, dubbo version: 2.6.1, current host: 192.168.1.120

  5. [06/06/18 11:56:30:030 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.120:20880, dubbo version: 2.6.1, current host: 192.168.1.120

 

再运行sonsumer:

consumer图consumer图

通过查看dubbo监控中心,可以看到如下所示的情况,具体dubbo监控中心如何安装部署请移步Simple 监控中心安装

dubboAdmin图dubboAdmin图

小结

对于dubbo听其大名已久,直到最近才动手写了一些demo,总体来看上手还是比较简单,官方也提供了比较详细的文档,社区也比较活跃。关于本篇博客中的代码,楼主已经放到了github,该兴趣的小伙伴,请移步Dubbo初体验Demo模板代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值