探秘Dubbo原理与源码 —— 调试环境搭建

1、 依赖⼯具

  • Maven
  • Git
  • JDK
  • IntelliJ IDEA

2、源码拉取
从官⽅仓库 https://github.com/alibaba/dubbo Fork 出属于⾃⼰的仓库。为什么要 Fork ?既然开始阅读、调试源码,我们可能会写⼀些注释,有了⾃⼰的仓库,可以进⾏⾃由的提交。

使⽤ IntelliJ IDEA 从 Fork 出来的仓库拉取代码。拉取完成后, Maven 会下载依赖包,可能会花费⼀些时间,耐⼼等待下。

在等待的过程中,我们可以来看⼀张 Dubbo 的架构图
在这里插入图片描述
在图中,本⽂会涉及到的有三个⻆⾊:

  • Registry 注册中⼼,⽤于服务的注册与发现。
  • Provider 服务提供者,通过向 Registry 注册服务。
  • Consumer 服务消费者,通过从 Registry 发现服务。

另外,Consumer 对 Provider 的调⽤,直接发起,⽆需经过 Registry 。
Dubbo 提供了 dubbo-demo 项⽬,⽤于开发者快速⼊⻔。 本⽂也使⽤该项⽬,搭建调试环境。

3. Registry
Dubbo 提供了多种 Registry 的实现,参⻅ 《注册中⼼参考⼿册》 。
在 dubbo-demo 项⽬中,使⽤ Multicast Registry 。 实际⽣产环境中,使⽤ Zookeeper Registry 。

友情提示:「3.1 Multicast Registry」 和 「3.2 Zookeeper Registry」 ⼆选⼀,建议先使⽤前
者搭建调试环境,因为简单⽅便。

3.1 Multicast Registry
Multicast Registry 基于 组播协议 实现,详细参⻅⽂档 《Multicast 注册中⼼》 。

Multicast 注册中⼼不需要启动任何中⼼节点,只要⼴播地址⼀样,就可以互相发现。

注意,不需要启动!!!简单的说,本⼩节的内容可以直接跳过。

3.2 Zookeeper Registry
Zookeeper Registry 基于 Zookeeper 实现,详细参⻅⽂档 《zookeeper 注册中⼼》 。

搭建步骤如下:
1、启动 Zookeeper 服务。

$ pwd
~/Documents/Dubbo/zookeeper-3.4.13
$ bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/szz/Documents/Dubbo/zookeeper-
3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

2、修改 dubbo-demo-provider.xml ,启动 Provider 。

<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

3、修改 dubbo-demo-consumer.xml ,启动 Consumer 。

<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

4. Provider
在 IDEA 中,右键 Debug 运⾏ com.alibaba.dubbo.demo.provider.Provider 。
当输出如下⽇志,表示成功:

[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-demoprovider.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=demoprovider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&in
terface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.p
ort=22222&side=provider&timestamp=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=demoprovider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&in
terface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.p
ort=22222&side=provider&timestamp=1518919503284 to registry
registry://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?
application=demoprovider&dubbo=2.0.0&pid=43392&qos.port=22222&registry=multicast&timestamp=
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=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43392&side=provider&timestamp=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=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43392&side=provider&timestamp=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=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43392&side=provider&timestamp=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=demoprovider&category=configurators&check=false&dubbo=2.0.0&generic=false&inter
face=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=pro
vider&timestamp=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=demoprovider&category=configurators&check=false&dubbo=2.0.0&generic=false&inter
face=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=pro
vider&timestamp=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=demoprovider&category=configurators&check=false&dubbo=2.0.0&generic=false&inter
face=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=pro
vider&timestamp=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=demoprovider&category=configurators&check=false&dubbo=2.0.0&generic=false&inter
face=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=pro
vider&timestamp=1518919503284, dubbo version: 2.0.0, current host:
192.168.1.116

注意,如果使⽤ Multicast Registry 作为注册中⼼,在 IDEA 中需要使⽤ Debug 模式运⾏,原因参
⻅:dubbo-demo set “java.net.preferIPv4Stack” not work

5. Consumer
在 IDEA 中,右键 Debug 运⾏ com.alibaba.dubbo.demo.consumer.Consumer 。
当输出如下⽇志,表示成功:

[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-democonsumer.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=democonsumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.d
ubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consum
er&timestamp=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=democonsumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.d
ubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consum
er&timestamp=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=democonsumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&i
nterface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.
port=33333&side=consumer&timestamp=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=democonsumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.d
ubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consum
er&timestamp=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=democonsumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&i
nterface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.
port=33333&side=consumer&timestamp=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=democonsumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&i
nterface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.
port=33333&side=consumer&timestamp=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=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43355&side=provider&timestamp=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=democonsumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&i
nterface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.
port=33333&side=consumer&timestamp=1518918323457, urls:
[dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?
anyhost=true&application=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43355&side=provider&timestamp=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=democonsumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&i
nterface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.
port=33333&side=consumer&timestamp=1518918323457, urls:
[dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?
anyhost=true&application=demoprovider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoSer
vice&methods=sayHello&pid=43355&side=provider&timestamp=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=democonsumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.
demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&register.ip=192.
168.1.116&remote.timestamp=1518918226970&side=consumer&timestamp=1518918323
457, dubbo version: 2.0.0, current host: 192.168.1.116
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880
Hello world, response form provider: 192.168.1.116:20880

6. 遇到的bug
6.1在MAC系统中使⽤dubbo的multicast模式,启动报错:

Dubbo的⼴播模式下Can't assign requested address问题
Exception in thread "main" java.lang.IllegalStateException: Can't assign
requested address
 at org.apache.dubbo.registry.multicast.MulticastRegistry.<init>
(MulticastRegistry.java:117)
 at
org.apache.dubbo.registry.multicast.MulticastRegistryFactory.createRegistry
(MulticastRegistryFactory.java:31)
 at
org.apache.dubbo.registry.support.AbstractRegistryFactory.getRegistry(Abstr
actRegistryFactory.java:96)
 at
org.apache.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFact
ory$Adaptive.java)
 at
org.apache.dubbo.registry.integration.RegistryProtocol.getRegistry(Registry
Protocol.java:204)
 at
org.apache.dubbo.registry.integration.RegistryProtocol.export(RegistryProto
col.java:138)
 at
org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListen
erWrapper.java:55)
 at
org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWr
apper.java:98)
 at
org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java)
 at
org.apache.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfi
g.java:513)
 at
org.apache.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358)
 at
org.apache.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317)
 at org.apache.dubbo.config.ServiceConfig.export(ServiceConfig.java:215)
 at
org.apache.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.j
ava:99)
 at
org.apache.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.j
ava:50)
 at
org.springframework.context.event.SimpleApplicationEventMulticaster.doInvok
eListener(SimpleApplicationEventMulticaster.java:172)
 at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeL
istener(SimpleApplicationEventMulticaster.java:165)
 at
org.springframework.context.event.SimpleApplicationEventMulticaster.multica
stEvent(SimpleApplicationEventMulticaster.java:139)
 at
org.springframework.context.support.AbstractApplicationContext.publishEvent
(AbstractApplicationContext.java:393)
 at
org.springframework.context.support.AbstractApplicationContext.publishEvent
(AbstractApplicationContext.java:347)
 at
org.springframework.context.support.AbstractApplicationContext.finishRefres
h(AbstractApplicationContext.java:883)
 at
org.springframework.context.support.AbstractApplicationContext.refresh(Abst
ractApplicationContext.java:546)
 at org.springframework.context.support.ClassPathXmlApplicationContext.
<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.
<init>(ClassPathXmlApplicationContext.java:93)
 at org.apache.dubbo.demo.provider.Provider.main(Provider.java:29)
Caused by: java.net.SocketException: Can't assign requested address
 at java.net.PlainDatagramSocketImpl.join(Native Method)
 at
java.net.AbstractPlainDatagramSocketImpl.join(AbstractPlainDatagramSocketIm
pl.java:178)
 at java.net.MulticastSocket.joinGroup(MulticastSocket.java:323)
 at org.apache.dubbo.registry.multicast.MulticastRegistry.<init>
(MulticastRegistry.java:90)
 ... 24 more

造成这种原因的主要是系统中开启了IPV6协议,java⽹络编程经常会获取到IPv6的地址。

解决⽅法:
添加vm参数 -Djava.net.preferIPv4Stack=true
在这里插入图片描述
6.2 使⽤zookeeper进⾏注册时,会报
java.lang.NoClassDefFoundError:
com/google/common/base/Function
缺少依赖,添加guava依赖即可

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>27.0-jre</version>
</dependency>

更详细的内容请阅读:https://www.roncoo.com/view/192

猜你喜欢

转载自blog.csdn.net/java_66666/article/details/85101414