dubbo是阿里巴巴开源的一款分布式服务治理框架。面向高访问量应用提供高性能远程调用服务。
dubbo架构如下:
节点角色说明:
Provider: 暴露服务的服务提供方
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次数和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
1 一个dubbo demo程序
本文demo的服务注册中心使用zookeeper,所以首先安装zookeeper,以windows单机为例。本文官网下载zookeeper,解压缩到本地,在conf目录下修改zookeeper配置文件zoo_sample.cfg为zoo.cfg。添加配置信息如下:
dataDir=E:\zookeeper\data
dataLogDir=E:\zookeeper\log
指定zookeeper的数据保存路径和日志保存路径。
命令行cd到bin目录下运行zkServer.cmd,如图:
在cmd中输入jps,出现QuorumPeerMain进程说明zookeeper启动完毕。
新建maven项目,项目结构如图:
首先导入zookeeper,dubbo和spring相关包。
其中DemoService为一个共享服务接口,定义如下:
public interface DemoService {
void sayHello();
}
定义服务DemoServiceImpl实现DemoService。
public class DemoServiceImpl implements DemoService {
public void sayHello() {
System.out.println("Hello dubbo");
}
}
配置dubbo provider,在resources中添加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://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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="demotest-provider" owner="chengzx" organization="czx"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
<dubbo:service interface="com.czx.dubboProject.service.DemoService" ref="demoService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="demoService" class="com.czx.dubboProject.service.impl.DemoServiceImpl"/>
</beans>
定义服务启动类StartProvider:
public class StartProvider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.out.println("服务已经启动");
System.in.read();
}
}
定义Consumer类,对服务进行调用:
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService=context.getBean(DemoService.class);
System.out.println("consumer");
demoService.sayHello();
}
}
配置dubbo consumer,在resources中新建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://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="demotest-consumer" owner="chengzx" organization="czx"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="permissionService" interface="com.czx.dubboProject.service.DemoService"/>
</beans>
demo启动顺序,zookeeper->StartProvider->StartConsumer