项目
博客
文档
归档
资源链接
关于我
项目
博客
文档
归档
资源链接
关于我
Dubbo 源码分析 —— 调试环境搭建
2020-05-01
·
芋道源码
·
转载
·
Dubbo
架构设计
mybatis
·
本文共 2,388个字,预计阅读需要 8分钟。
> `转载 `于【[芋道源码](http://svip.iocoder.cn/)】 > 本文基于 Dubbo 2.6.1 版本,望知悉。 ## 1. 依赖工具 - Maven - Git - JDK - IntelliJ IDEA ## 2. 源码拉取 从官方仓库
`Fork` 出属于自己的仓库。为什么要 `Fork` ?既然开始阅读、调试源码,我们可能会写一些注释,有了自己的仓库,可以进行自由的提交。😈 使用 `IntelliJ IDEA` 从 `Fork` 出来的仓库拉取代码。拉取完成后,`Maven` 会下载依赖包,可能会花费一些时间,耐心等待下。 ------ 在等待的过程中,我们可以来看一张 Dubbo 的架构图[![img](http://static2.iocoder.cn/images/Dubbo/2018_01_01/01.png)](http://static2.iocoder.cn/images/Dubbo/2018_01_01/01.png) 在图中,本文会涉及到的有**三个**角色: - Registry 注册中心,用于服务的注册与发现。 - Provider 服务提供者,通过向 Registry 注册服务。 - Consumer 服务消费者,通过从 Registry 发现服务。 另外,Consumer 对 Provider 的调用,**直接发起**,无需经过 Registry 。 ------ Dubbo 提供了 [`dubbo-demo`](https://github.com/alibaba/dubbo/tree/2c20d99d9804271e1c1953242ea0aa510854eda1/dubbo-common) 项目,用于开发者快速入门。 本文也使用该项目,搭建调试环境。 ## 3. Registry Dubbo 提供了多种 Registry 的实现,参见 [《注册中心参考手册》](http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html) 。 在 `dubbo-demo` 项目中,使用 Multicast Registry 。 实际**生产环境**中,使用 Zookeeper Registry 。 > 友情提示:「`3.1 Multicast Registry`」和 「`3.2 Zookeeper Registry`」二选一,建议先使用前者搭建调试环境,因为简单方便。 ### 3.1 Multicast Registry Multicast Registry 基于 [组播协议](http://blog.csdn.net/liu251890347/article/details/39211685) 实现,详细参见文档 [《Multicast 注册中心》](http://dubbo.apache.org/zh-cn/docs/user/references/registry/multicast.html) 。 > Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。 **注意**,不需要启动!!!简单的说,本小节的内容可以直接跳过。 ### 3.2 Zookeeper Registry Zookeeper Registry 基于 [Zookeeper](https://zookeeper.apache.org/) 实现,详细参见文档 [《zookeeper 注册中心》](http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html) 。 搭建步骤如下: 1、启动 Zookeeper 服务。 ```shell $ pwd /Users/yunai/百度云同步盘/Programs/Zookeeper/zookeeper-3.4.8 $ bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /Users/yunai/百度云同步盘/Programs/Zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg Starting zookeeper ... STARTED ``` 2、修改 [`dubbo-demo-provider.xml`](https://github.com/alibaba/dubbo/blob/2c20d99d9804271e1c1953242ea0aa510854eda1/dubbo-demo/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml) ,启动 Provider 。 ```xml
``` 3、修改 [`dubbo-demo-consumer.xml`](https://github.com/alibaba/dubbo/blob/2c20d99d9804271e1c1953242ea0aa510854eda1/dubbo-demo/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml) ,启动 Consumer 。 ```xml-dtd
``` ## 4. Provider 在 IDEA 中,右键 **Debug** 运行 [`com.alibaba.dubbo.demo.provider.Provider`](https://github.com/alibaba/dubbo/blob/HEAD/dubbo-demo/dubbo-demo-provider/src/main/java/com/alibaba/dubbo/demo/provider/Provider.java) 。 当输出如下日志,表示成功: ```verilog [18/02/18 10:04:42:042 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cd2dae5: startup date [Sun Feb 18 10:04:42 CST 2018]; root of context hierarchy [18/02/18 10:04:42:042 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-provider.xml] [18/02/18 10:04:42:042 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [18/02/18 10:04:53:053 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.alibaba.dubbo.demo.DemoService, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to local registry, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to url dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.port=22222&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.alibaba.dubbo.demo.DemoService url dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.port=22222&side=provider×tamp=1518919503284 to registry registry://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&pid=43392&qos.port=22222®istry=multicast×tamp=1518919493265, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.116:20880, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Register: dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Subscribe: provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: subscribe provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: subscribe provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:21:021 CST] main WARN multicast.MulticastRegistry: [DUBBO] Ignore empty notify urls for subscribe url provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 ``` **注意**,如果使用 Multicast Registry 作为注册中心,在 IDEA 中需要使用 **Debug 模式**运行,原因参见:[dubbo-demo set “java.net.preferIPv4Stack” not work](https://github.com/alibaba/dubbo/issues/997) 。 ## 5. Consumer 在 IDEA 中,右键 **Debug** 运行 [`com.alibaba.dubbo.demo.consumer.Consumer`](https://github.com/alibaba/dubbo/blob/HEAD/dubbo-demo/dubbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/Consumer.java) 。 当输出如下日志,表示成功: ```verilog [18/02/18 09:45:22:022 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cd2dae5: startup date [Sun Feb 18 09:45:22 CST 2018]; root of context hierarchy [18/02/18 09:45:22:022 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-consumer.xml] [18/02/18 09:45:22:022 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Register: consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: register consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Subscribe: consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: subscribe consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: subscribe consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, urls: [dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO transport.AbstractClient: [DUBBO] Successed connect to server /192.168.1.116:20880 from NettyClient 192.168.1.116 using dubbo version 2.0.0, channel is NettyChannel [channel=[id: 0x1b7bac12, /192.168.1.116:63856 => /192.168.1.116:20880]], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO transport.AbstractClient: [DUBBO] Start NettyClient 123312.local/192.168.1.116 connect to the server /192.168.1.116:20880, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, urls: [dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO config.AbstractConfig: [DUBBO] Refer dubbo service com.alibaba.dubbo.demo.DemoService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333®ister.ip=192.168.1.116&remote.timestamp=1518918226970&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 ```