Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。
主要核心部件:
Remoting: 网络通信框架,实现了 sync-over-async 和 request-response 消息机制;
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能;
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
工作原理:
Provider:暴露服务方称之为“服务提供者”。
Consumer:调用远程服务方称之为“服务消费者”。
Registry:服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor:统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”。
(1)下载Zookeeper-3.4.6.tar.gz 地址:http://www.apache.org/dist/zookeeper/;
(2)我们放到Linux的/usr/local下,然后解压:
#tar zxvf zookeeper-3.4.6.tar.gz
(3)然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件,如图所示:
由于之前已经成功配置zookpeer服务器,所以我的zookpeeper/conf目录下已经存在zoo.cfg文件。
(4)启动Zookeeper服务,如图所示:
到此Zookeeper的安装和配置完成。
package com.tgb.api; public interface IProcessData { public String deal(String data); }dubbo-tgb-provider 服务提供者:
package com.tgb.serviceImpl; import com.tgb.api.IProcessData; public class ProcessDataImpl implements IProcessData { public String deal(String data) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "Finished:" + data; } }applicationProvider.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 "> <!-- Application name --> <dubbo:application name="dubbo-tgb" /> <!-- registry address, used for service to register itself --> <dubbo:registry address="zookeeper://192.168.24.244:2181" /> <!-- expose this service through dubbo protocol, through port 20880 --> <!-- <dubbo:protocol name="dubbo" port="20880" /> <dubbo:protocol name="dubbo" port="9090" server="netty" client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8" threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192" accepts="1000" payload="8388608" /> --> <!-- Service interface Concurrent Control --> <dubbo:service interface="com.tgb.api.IProcessData" ref="processDataService" executes="10" /> <!-- Default Protocol --> <!-- <dubbo:protocol server="netty" /> --> <!-- designate implementation --> <bean id="processDataService" class="com.tgb.serviceImpl.ProcessDataImpl" /> </beans>启动服务:
package com.tgb.app; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProviderMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("Press any key to exit."); System.in.read(); } }服务调用者的工程
<?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 "> <!-- consumer application name --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry address="zookeeper://192.168.24.244:2181" /> <dubbo:consumer timeout="5000"/> <!-- which service to consume? --> <dubbo:reference id="processDataService" interface="com.tgb.api.IProcessData" /> </beans>调用类:
package com.tgb.app; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tgb.api.IProcessData; public class ConsumerThd implements Runnable { public void run() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); IProcessData processDataService = (IProcessData) context .getBean("processDataService"); // get // proxy String hello = processDataService.deal("nihao"); // do invoke! System.out.println(Thread.currentThread().getName() + " " + hello); } public static void main(String[] args) { new Thread(new ConsumerThd()).start(); } }到此,使用dubbo+zookeeper实现分布式调用的例子已讲解完,源码下载地址:http://pan.baidu.com/s/1qWveFwg。