dubbo 的使用
Dubbo是[3] 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输
出和输入功能,可以和 [4] Spring框架无缝集成。
主要核心部件
Remoting: 网络通信框架,实现了 sync-over-async 和 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅
工作原理
Provider
暴露服务方称之为“服务提供者”。
Consumer
调用远程服务方称之为“服务消费者”。
Registry
服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor
统计服务的调用次数和调用时间的日志服务称之为“服务监控中心”。
(1) 连通性:
注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小
监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示
服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销
服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销
注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外
注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者
注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表
注册中心和监控中心都是可选的,服务消费者可以直连服务提供者
(2) 健状性:
监控中心宕掉不影响使用,只是丢失部分采样数据
数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务
注册中心对等集群,任意一台宕掉后,将自动切换到另一台
注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯
服务提供者无状态,任意一台宕掉后,不影响使用
服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复
(3) 伸缩性:
注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心
服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者
以下是需要的ZooKeeper 安装,和管理页面的安装,测试的提供者和消费者。
所需的文件下载地址:http://download.csdn.net/detail/liuc0317/8314333
1. 我在windows 下安装的zooKeeper ,所下载的文件 zookeeper-3.3.6.tar.gz 解压到一个文件夹下。如图
目录如图:
进入conf 修改配置文件 zoo.cfg
# The number of milliseconds of each tick 心跳间隔 毫秒每次 tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting anacknowledgement syncLimit=5 # the directory where the snapshot isstored. //镜像数据位置 dataDir=D:\\data\\zookeeper #日志位置 dataLogDir=D:\\logs\\zookeeper # the port at which the clients willconnect 客户端连接的端口 clientPort=2181
修改了配置文件dataLogDir 的配置,这个配置地址需要人为的去创建,要不然会报错。这样,配置就基本完成了。
第二步:进入bin 目录,然后在 dos 下运行。
这样 zookeeper 就启动了,同时可以使用zookeeper 自带的客户端查看一下。如图:
编写服务的提供者和消费者 如图:
DemoService.java
package com.unj.dubbotest.provider; import java.util.List; public interface DemoService { String sayHello(String name); public List getUsers(); }
DemoServiceImp.java
package com.unj.dubbotest.provider.impl; import java.util.ArrayList; import java.util.List; import com.unj.dubbotest.provider.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } public List getUsers() { List list = new ArrayList(); User u1 = new User(); u1.setName("jack"); u1.setAge(20); u1.setSex("m"); User u2 = new User(); u2.setName("tom"); u2.setAge(21); u2.setSex("m"); User u3 = new User(); u3.setName("rose"); u3.setAge(19); u3.setSex("w"); list.add(u1); list.add(u2); list.add(u3); return list; } }
applicationContext.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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="xixi_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.20.5.106:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" /> </beans>
Provider.java
package com.unj.dubbotest.provider.impl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
运行main 方法:日志如下:
2014-12-30 15:45:46,349 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd87b2: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd87b2]; startup date [Tue Dec 30 15:45:46 CST 2014]; root of context hierarchy 2014-12-30 15:45:46,410 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml] 2014-12-30 15:45:46,554 INFO [com.alibaba.dubbo.common.logger.LoggerFactory] - using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter 2014-12-30 15:45:46,585 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd87b2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1319c 2014-12-30 15:45:46,831 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1319c: defining beans [demoService,xixi_provider,com.alibaba.dubbo.config.RegistryConfig,dubbo, com.unj.dubbotest.provider.DemoService]; root of factory hierarchy 2014-12-30 15:45:46,834 INFO [com.alibaba.dubbo.config.AbstractConfig] - [DUBBO] The service ready on spring started. service: com.unj.dubbotest.provider.DemoService, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:46,992 INFO [com.alibaba.dubbo.config.AbstractConfig] - [DUBBO] Export dubbo service com.unj.dubbotest.provider.DemoService to local registry, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:46,992 INFO [com.alibaba.dubbo.config.AbstractConfig] - [DUBBO] Export dubbo service com.unj.dubbotest.provider.DemoService to url dubbo://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost=true&application =xixi_provider&dubbo=2.5.2&interface=com.unj.dubbotest.provider.DemoService&methods =sayHello,getUsers&pid=12192&side=provider×tamp=1419925546891, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:46,992 INFO [com.alibaba.dubbo.config.AbstractConfig] - [DUBBO] Register dubbo service com.unj.dubbotest.provider.DemoService url dubbo://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost =true&application=xixi_provider&dubbo=2.5.2&interface=com.unj.dubbotest.provider.DemoService&methods =sayHello,getUsers&pid=12192&side=provider×tamp=1419925546891 to registry registry://10.20.5.106:2181/com.alibaba.dubbo.registry.RegistryService?application =xixi_provider&dubbo=2.5.2&pid=12192®istry=zookeeper×tamp=1419925546845, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:47,161 INFO [com.alibaba.dubbo.remoting.transport.AbstractServer] - [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /10.20.5.106:20880, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:47,183 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Load registry store file C:\Users\lenovo\.dubbo\dubbo-registry-10.20.5.106.cache, data: {com.unj.dubbotest.provider.DemoService =empty://10.20.5.106/com.unj.dubbotest.provider.DemoService?application =hehe_consumer&category=providers&dubbo =2.5.2&interface=com.unj.dubbotest.provider.DemoService&methods =sayHello,getUsers&pid=21892&side=consumer×tamp =1419925451328 empty://10.20.5.106/com.unj.dubbotest.provider.DemoService?application =hehe_consumer&category=configurators&dubbo=2.5.2&interface =com.unj.dubbotest.provider.DemoService&methods=sayHello,getUsers&pid =21892&side=consumer×tamp =1419925451328 empty://10.20.5.106/com.unj.dubbotest.provider.DemoService?application =hehe_consumer&category=routers&dubbo =2.5.2&interface=com.unj.dubbotest.provider.DemoService&methods =sayHello,getUsers&pid=21892&side =consumer×tamp=1419925451328}, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:47,198 INFO [org.I0Itec.zkclient.ZkEventThread] - Starting ZkClient event thread. 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:host.name=10.20.5.106 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.version=1.6.0_30 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.vendor=Sun Microsystems Inc. 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.home=D:\soft\UCFSOFTDOC\soft\JDK\Java_JDK1.6_32bit\Java\jdk1.6.0_30\jre 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.class.path =C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\classes; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\commons-lang-2.3.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\commons-logging-1.1.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\commons-logging-api-1.1.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\dubbo-2.5.2.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\dubbo-admin-2.5.2-sources.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\dubbo-demo-consumer-2.5.2-sources.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\dubbo-demo-provider-2.5.2-sources.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\dubbo-registry-simple-2.5.2-sources.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\javassist-3.4.GA.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\jline-0.9.94.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\netty-3.2.2.Final.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\netty-3.2.5.Final.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-aop.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-beans.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-context-support.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-context.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-core.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-jdbc.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-jms.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-orm.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-test.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-tx.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-web.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-webmvc-portlet.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-webmvc-struts.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring-webmvc.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\spring.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\zkclient-0.1.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\zookeeper-3.4.5.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\log4j-1.2.12.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\slf4j-api-1.6.1.jar; C:\Users\lenovo\Downloads\provider\dubboprovider\WebRoot\WEB-INF\lib\slf4j-log4j12-1.6.1.jar 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.library.path=D:\soft\UCFSOFTDOC\soft\JDK\Java_JDK1.6_32bit\Java\jdk1.6.0_30\bin; C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows; C:/Program Files (x86)/Java/jre7/bin/client; C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386; C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\; C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86; C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64; C:\Program Files\Intel\Intel(R) Management Engine Components\DAL; C:\Program Files\Intel\Intel(R) Management Engine Components\IPT; C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL; C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT; D:\soft\UCFSOFTDOC\soft\JDK\Java_JDK1.6_32bit\Java\jdk1.6.0_30\bin; D:\Program Files\TortoiseSVN\bin; D:\soft\UCFSOFTDOC\soft\Maven\apache-maven-3.1.1_20140127\apache-maven-3.1.1\bin; C:\Program Files (x86)\OpenV*N\bin;D:\soft\UCFSOFTDOC\soft\IDE\eclipse_JAVAEE;;. 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.io.tmpdir=C:\Users\lenovo\AppData\Local\Temp\ 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.compiler=<NA> 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.name=Windows 7 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.arch=x86 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.version=6.1 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.name=lenovo 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.home=C:\Users\lenovo 2014-12-30 15:45:47,217 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.dir= C:\Users\lenovo\Downloads\provider\dubboprovider 2014-12-30 15:45:47,218 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=10.20.5.106:2181 sessionTimeout =30000 watcher=org.I0Itec.zkclient.ZkClient@2acc57 2014-12-30 15:45:47,239 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server 10.20.5.106/10.20.5.106:2181. Will not attempt to authenticate using SASL (无法定位登录配置) 2014-12-30 15:45:47,240 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established to 10.20.5.106/10.20.5.106:2181, initiating session 2014-12-30 15:45:47,306 WARN [org.apache.zookeeper.ClientCnxnSocket] - Connected to an old server; r-o mode will be unavailable 2014-12-30 15:45:47,306 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server 10.20.5.106/10.20.5.106:2181, sessionid = 0x14a9a27ba100000, negotiated timeout = 30000 2014-12-30 15:45:47,309 INFO [org.I0Itec.zkclient.ZkClient] - zookeeper state changed (SyncConnected) 2014-12-30 15:45:47,312 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Register: dubbo://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost=true&application =xixi_provider&dubbo=2.5.2&interface=com.unj.dubbotest.provider.DemoService&methods =sayHello,getUsers&pid=12192&side=provider×tamp=1419925546891, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:47,446 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Subscribe: provider://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost=true&application =xixi_provider&category=configurators✓=false&dubbo=2.5.2&interface =com.unj.dubbotest.provider.DemoService&methods=sayHello,getUsers&pid =12192&side=provider×tamp=1419925546891, dubbo version: 2.5.2, current host: 127.0.0.1 2014-12-30 15:45:47,559 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Notify urls for subscribe url provider://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost =true&application=xixi_provider&category=configurators✓=false&dubbo=2.5.2&interface =com.unj.dubbotest.provider.DemoService&methods=sayHello,getUsers&pid=12192&side =provider×tamp=1419925546891, urls: [empty://10.20.5.106:20880/com.unj.dubbotest.provider.DemoService?anyhost =true&application=xixi_provider&category=configurators✓=false&dubbo=2.5.2&interface =com.unj.dubbotest.provider.DemoService&methods=sayHello,getUsers&pid=12192&side =provider×tamp=1419925546891], dubbo version: 2.5.2, current host: 127.0.0.1
这样 就在 zookeeper 的控制页面中就会查询到 此服务 如图:
编写服务的消费者 项目如图:
DemoService.java 这个 服务提供者 提供的服务类,在实际开发中,需要提供者把这个类,打成一个jar 包提供给调用者。
package com.unj.dubbotest.provider; import java.util.List; public interface DemoService { String sayHello(String name); public List getUsers(); }
Consumer.java
package com.alibaba.dubbo.demo.pp; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.unj.dubbotest.provider.DemoService; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String hello = demoService.sayHello("tom"); System.out.println(hello); List list = demoService.getUsers(); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } System.in.read(); } }
applicationContext.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="hehe_consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://10.20.5.106:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" /> </beans>
运行的结果:
这样就调用完了。