一、介绍
首先要声明的是,dubbo是一款高性能、轻量级的开源Java RPC框架。它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。之前有阿里进行开发和维护,下载已经贡献到apache,访问地址为:http://dubbo.apache.org。从2.7.0版本开始,正式由apache发布,之前为com.alibaba。dubbo整体架构图如下:
节点角色说明
节点 | 角色说明 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
调用关系说明
- 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
二、测试代码
这里是根据官网进行代码的编码,使用zookeeper注册中心。这里使用spring xml配置的方式进行。
1、使用maven创建一个多模块项目,创建一个消费者consumer和生产者provider,结构如下:
2、在主pom文件中引入dubbo和zookeeper,
<groupId>com.skyhawk</groupId>
<artifactId>dubboStudy</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider</module>
<module>consumer</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dubbo.version>2.7.0</dubbo.version>
<curator.version>4.2.0</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
</dependencies>
3、配置生产者
(1)创建DemoService
public interface DemoService {
String sayHello(String name);
}
(2)DemoServiceImpl
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello "+name;
}
}
(3)配置文件provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.skyhawk.service.DemoService" ref="demoService"/>
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.skyhawk.service.DemoServiceImpl"/>
</beans>
(4)加载spring配置
public class ProviderApp
{
public static void main( String[] args ) throws IOException {
System.out.println( "Hello Provider!" );
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("provider.xml");
ctx.start();
System.in.read(); //按任意键退出
}
}
4、配置消费者
(1)添加DemoService
public interface DemoService {
String sayHello(String name);
}
(2)配置consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="comsumer"/>
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.skyhawk.service.DemoService"/>
</beans>
(3)加载spring配置,调用远程服务
public class Consumer
{
public static void main( String[] args ) {
System.out.println( "Hello Consumer!" );
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("consumer.xml");
ctx.start();
DemoService demoService= (DemoService) ctx.getBean("demoService");
String hello=demoService.sayHello("world");
System.out.println(hello);
}
}
5、启动zookeeper,先执行生产者,后执行消费者
(1)执行生产者后,会打印“Hello Provider!”
(2)执行消费者,会打印“Hello Consumer!”,并调用生产者后打印“Hello world”
到此,一个初步的dubbo测试代码就完成了。
三、注册中心分析
使用zk客户端进入zk,观察到目录结构如下:
可以看到生产者和消费者均注册到了注册中心。查看生产者节点,如下:
[dubbo%3A%2F%2F10.71.8.61%3A20880%2Fcom.skyhawk.service.DemoService%3Fanyhost%3Dtrue%26application%3Dprovider%26bean.name%3Dcom.skyhawk.service.DemoService%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.skyhawk.service.DemoService%26methods%3DsayHello%26pid%3D12656%26release%3D2.7.0%26side%3Dprovider%26timestamp%3D1562809219072]
执行URL解码可以看到如下信息:
[dubbo://10.71.8.61:20880/com.skyhawk.service.DemoService?anyhost=true&application=provider&bean.name=com.skyhawk.service.DemoService&dubbo=2.0.2&dynamic=true&generic=false&interface=com.skyhawk.service.DemoService&methods=sayHello&pid=12656&release=2.7.0&side=provider×tamp=1562809219072]
数据变成了我们配置的信息。在测试后发现,注册到zk生产者节点属于临时节点,而dubbo节点和com.skyhawk.service.DemoService属于持久节点。在配置注册中心的时候可以添加dynamic参数进行调整,默认是true。如果设为false,生产者节点也会持久化。表示注册后将显示后disable状态,需人工启用,并且服务提供者停止时,也不会自动取消册,需人工禁用。这些参数信息将会后续进行解释。
zk注册中心结构图如下: