1. 依赖工具
- Maven
- Git
- JDK
- IntelliJ IDEA
2. 源码拉取
从官方仓库 https://github.com/alibaba/dubbo Fork
出属于自己的仓库。
使用 IntelliJ IDEA
从 Fork
出来的仓库拉取代码。拉取完成后,Maven
会下载依赖包,可能会花费一些时间,耐心等待下。
在图中,本文会涉及到的有三个角色:
- 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 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。
扫描二维码关注公众号,回复: 2951596 查看本文章![]()
注意,不需要启动!!!简单的说,本小节的内容可以直接跳过。
3.2 Zookeeper Registry
Zookeeper Registry 基于 Zookeeper 实现,详细参见文档 《zookeeper 注册中心》 。
搭建步骤如下:
1、启动 Zookeeper 服务
我这里为了方便,直接在window上装单机版zookeeper
3、修改 dubbo-demo-consumer.xml
,启动 Consumer 。2、修改 dubbo-demo-provider.xml
,启动 Provider 。
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
在 IDEA 中,右键 Debug 运行 com.alibaba.dubbo.demo.provider.Provider
。4. 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-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
5. Consumer注意,如果使用 Multicast Registry 作为注册中心,在 IDEA 中需要使用 Debug 模式运行,原因参见:dubbo-demo set “java.net.preferIPv4Stack” not work 。
在 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-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
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
本人用multicast作注册中心启动没成功,报错信息如下,哪个哥要是知道原因望告知小弟:
原创出处 http://svip.iocoder.cn/Dubbo/build-debugging-environment/ 「芋道源码」