之前的博文,每每写到一个新的东西都会在最前头写上 xx 是啥?今天想换一种方式,今天先不说 Dubbo 是啥?因为这玩意一时半会儿写不明白,也有可能是博主愚昧,看了半天官方文档也没能理解这到底是干嘛的一个玩意儿阿。直到跟着官方给出的 Demo 程序写出第一个 Dubbo 应用(没错,是 Hello World 程序),后来又有幸和使用过 Dubbo 的同事讨论了一下 Dubbo 才云消雾散。
所以这次我们先不去看那些概念了,先实现一个应用(不要小瞧 Hello World)
首先一起思考一个问题,如何把应用从单机扩展到分布式?不妨先自己思考几分钟后再去利用搜索引擎。
不知道大家对于生产消费模型熟不熟悉,消息队列(RabbitMQ、Apache ActiveMQ 等)的架构就是这样的。接着我们要实现的这个 Demo 也可以类比这种模型去理解,我们写一个 Server 作为服务提供者(Provider),写一个Client 作为服务消费者(Consumer)通过 Dubbo 来调用 Server 上提供的一个服务。
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.7.RELEASEversion>
dependency>
public interface HelloService {
String sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
public String sayHello(String name) {
logger.info("From Provider" + name);
return "Hello " + name;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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">
<dubbo:application name="demo-provider"/>
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.xiaoping.dubbotest.HelloService" ref="helloService"/>
<bean id="helloService" class="com.xiaoping.dubbotest.HelloServiceImpl"/>
beans>
public class ProviderApp {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"application.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<dubbo:reference id="helloService" interface="com.xiaoping.dubbotest.HelloService"/>
beans>
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"application.xml"});
context.start();
// obtain proxy object for remote invocation
HelloService helloService = (HelloService) context.getBean("helloService");
// 注: 这里的 HelloService 可以是依赖自 Provider 中的接口,也可以是一个和他一样的副本
String hello = helloService.sayHello("world"); // execute remote invocation
System.out.println(hello); // show the result
}
}
Consumer 输出:
7月 25, 2018 9:51:39 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13a5fe33: startup date [Wed Jul 25 21:51:39 CST 2018]; root of context hierarchy
7月 25, 2018 9:51:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application.xml]
7月 25, 2018 9:51:39 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by javassist.ClassPool (file:/Users/xiaop1ng/apache-maven-3.5.3/repo/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of javassist.ClassPool
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Register: consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Send broadcast message: register consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 to /224.5.6.7:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: register consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Subscribe: consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Send broadcast message: subscribe consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 to /224.5.6.7:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: subscribe consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: register dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Notify urls for subscribe url consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351, urls: [dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590], dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Successed connect to server /192.168.2.184:20880 from NettyClient 192.168.2.184 using dubbo version 2.6.2, channel is NettyChannel [channel=[id: 0x6911f93f, /192.168.2.184:50565 => /192.168.2.184:20880]], dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Start NettyClient bogon/192.168.2.184 connect to the server /192.168.2.184:20880, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Notify urls for subscribe url consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351, urls: [dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590], dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Refer dubbo service com.xiaoping.dubbotest.HelloService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039®ister.ip=192.168.2.184&remote.timestamp=1532526254590&side=consumer×tamp=1532526700351, dubbo version: 2.6.2, current host: 192.168.2.184
Hello world
ProviderApp 输出:
7月 25, 2018 9:44:13 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13a5fe33: startup date [Wed Jul 25 21:44:13 CST 2018]; root of context hierarchy
7月 25, 2018 9:44:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application.xml]
7月 25, 2018 9:44:13 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by javassist.ClassPool (file:/Users/xiaop1ng/apache-maven-3.5.3/repo/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of javassist.ClassPool
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
7月 25, 2018 9:44:14 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] The service ready on spring started. service: com.xiaoping.dubbotest.HelloService, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:14 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Export dubbo service com.xiaoping.dubbotest.HelloService to local registry, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:14 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Export dubbo service com.xiaoping.dubbotest.HelloService to url dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&bind.ip=192.168.2.184&bind.port=20880&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:14 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Register dubbo service com.xiaoping.dubbotest.HelloService url dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&bind.ip=192.168.2.184&bind.port=20880&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 to registry registry://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.6.2&pid=1027®istry=multicast×tamp=1532526254536, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.2.184:20880, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Register: dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Send broadcast message: register dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 to /224.5.6.7:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: register dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Subscribe: provider://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Send broadcast message: subscribe provider://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 to /224.5.6.7:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:15 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: subscribe provider://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:44:16 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger warn
警告: [DUBBO] Ignore empty notify urls for subscribe url provider://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: register consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: subscribe consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Send broadcast message: register dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 to /224.5.6.7:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: register dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] From Providerworld, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: unregister consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: unregister consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] Receive multicast message: unsubscribe consumer://192.168.2.184/com.xiaoping.dubbotest.HelloService?application=demo-consumer&category=providers,configurators,routers&dubbo=2.6.2&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1039&side=consumer×tamp=1532526700351 from /192.168.2.184:1234, dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger warn
警告: [DUBBO] All clients has discontected from /192.168.2.184:20880. You can graceful shutdown now., dubbo version: 2.6.2, current host: 192.168.2.184
7月 25, 2018 9:51:40 下午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: [DUBBO] disconnected from /192.168.2.184:50565,url:dubbo://192.168.2.184:20880/com.xiaoping.dubbotest.HelloService?anyhost=true&application=demo-provider&bind.ip=192.168.2.184&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.2&generic=false&heartbeat=60000&interface=com.xiaoping.dubbotest.HelloService&methods=sayHello&pid=1027&side=provider×tamp=1532526254590, dubbo version: 2.6.2, current host: 192.168.2.184
在跑通了 Demo 程序之后我们来看一下 Dubbo 的架构
Provider
暴露服务的服务提供方
Consumer
调用远程服务的服务消费方
Registry
服务注册与发现的注册中心
Monitor
统计服务的调用次数和调用时间的监控中心
Container
服务运行容器
现在来看这张架构图应该清晰一些,Provider
和 Consumer
使用接口作为服务的契约,通过注册中心 Registry
来完成服务的注册和发现,远程通讯的细节通过代理类来屏蔽。
官方文档
Dubbo 用户手册
Dubbo 开发手册
Dubbo 管理手册