一、前言

  Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用、整合、集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD、当当在内的众多互联网项目中有着广泛应用。dubbo 通过高性能 RPC 实现服务的输出和输入功能,框架基于 Spring Framework 进行无缝集成,使用过程中基本看不到 Dubbo API的直接调用,Dubbo服务支持RMI、Hessian、Dubbo、WebService等众多通信协议,同时提供了对服务的监控和管理平台,属于一套完整的SOA解决方案。

  详细介绍请参照官方地址:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm,不再重复描述,本文主要记录了详细的开发整合步骤。引用一张官方提供的dubbo架构图:

  

  • Provider    暴露服务方称之为“服务提供者”。

  • Consumer 调用远程服务方称之为“服务消费者”。

  • Registry    服务注册与发现的中心目录服务称之为“服务注册中心”。

  • Monitor     统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”。

  • Container  服务运行“容器”。

二、源码构建

  1、dubbo基于maven构建,需要先配置maven环境,当然如果你不想构建源码,也可以直接下载对应版本的发行包。

  2、dubbo构建需要依赖opensesame组件,先通过 git 获取并安装opensesame,源码地址:

https://github.com/alibaba/opensesame

     执行maven安装:mvn install

  3、通过下面 git 地址获取对应版本的 dubbo 源码,官方推荐使用 dubbo 2.4.9,本文基于trunk版本构建。

https://github.com/alibaba/dubbo

    maven构建:mvn clean install -Dmaven.test.skip,在漫长的等待后(过程需要通过网络下载依赖资源)会有如下提示,生成的 jar 包位于各自目录的target文件夹。

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] dubbo-parent ....................................... SUCCESS [ 1.272 s]
[INFO] dubbo-common ....................................... SUCCESS [ 4.572 s]
[INFO] dubbo-container .................................... SUCCESS [ 0.024 s]
[INFO] dubbo-container-api ................................ SUCCESS [ 1.148 s]
[INFO] dubbo-container-spring ............................. SUCCESS [ 0.524 s]
[INFO] dubbo-container-jetty .............................. SUCCESS [ 0.420 s]
[INFO] dubbo-container-log4j .............................. SUCCESS [ 0.448 s]
[INFO] dubbo-container-logback ............................ SUCCESS [ 0.468 s]
[INFO] dubbo-remoting ..................................... SUCCESS [ 0.016 s]
[INFO] dubbo-remoting-api ................................. SUCCESS [ 1.864 s]
[INFO] dubbo-remoting-netty ............................... SUCCESS [ 1.125 s]
[INFO] dubbo-remoting-mina ................................ SUCCESS [ 0.696 s]
[INFO] dubbo-remoting-grizzly ............................. SUCCESS [ 0.680 s]
[INFO] dubbo-remoting-p2p ................................. SUCCESS [ 1.048 s]
[INFO] dubbo-remoting-http ................................ SUCCESS [ 0.616 s]
[INFO] dubbo-remoting-zookeeper ........................... SUCCESS [ 0.716 s]
[INFO] dubbo-rpc .......................................... SUCCESS [ 0.016 s]
[INFO] dubbo-rpc-api ...................................... SUCCESS [ 1.608 s]
[INFO] dubbo-rpc-default .................................. SUCCESS [ 1.396 s]
[INFO] dubbo-rpc-injvm .................................... SUCCESS [ 0.760 s]
[INFO] dubbo-rpc-rmi ...................................... SUCCESS [ 0.416 s]
[INFO] dubbo-rpc-hessian .................................. SUCCESS [ 0.584 s]
[INFO] dubbo-rpc-http ..................................... SUCCESS [ 0.580 s]
[INFO] dubbo-rpc-webservice ............................... SUCCESS [ 0.536 s]
[INFO] dubbo-cluster ...................................... SUCCESS [ 2.208 s]
[INFO] dubbo-registry ..................................... SUCCESS [ 0.020 s]
[INFO] dubbo-registry-api ................................. SUCCESS [ 1.241 s]
[INFO] dubbo-monitor ...................................... SUCCESS [ 0.016 s]
[INFO] dubbo-monitor-api .................................. SUCCESS [ 0.464 s]
[INFO] dubbo-filter ....................................... SUCCESS [ 0.016 s]
[INFO] dubbo-filter-validation ............................ SUCCESS [ 0.608 s]
[INFO] dubbo-filter-cache ................................. SUCCESS [ 0.604 s]
[INFO] dubbo-registry-default ............................. SUCCESS [ 0.540 s]
[INFO] dubbo-monitor-default .............................. SUCCESS [ 0.588 s]
[INFO] dubbo-registry-multicast ........................... SUCCESS [ 0.632 s]
[INFO] dubbo-config ....................................... SUCCESS [ 0.016 s]
[INFO] dubbo-config-api ................................... SUCCESS [ 1.500 s]
[INFO] dubbo-config-spring ................................ SUCCESS [ 1.520 s]
[INFO] dubbo-rpc-thrift ................................... SUCCESS [ 1.032 s]
[INFO] dubbo-rpc-memcached ................................ SUCCESS [ 0.464 s]
[INFO] dubbo-rpc-redis .................................... SUCCESS [ 0.500 s]
[INFO] dubbo-registry-zookeeper ........................... SUCCESS [ 0.656 s]
[INFO] dubbo-registry-redis ............................... SUCCESS [ 0.640 s]
[INFO] dubbo .............................................. SUCCESS [ 1.009 s]
[INFO] dubbo-simple ....................................... SUCCESS [ 0.016 s]
[INFO] dubbo-registry-simple .............................. SUCCESS [01:43 min]
[INFO] dubbo-monitor-simple ............................... SUCCESS [ 10.379 s]
[INFO] dubbo-admin ........................................ SUCCESS [04:03 min]
[INFO] dubbo-demo ......................................... SUCCESS [ 0.016 s]
[INFO] dubbo-demo-api ..................................... SUCCESS [ 0.308 s]
[INFO] dubbo-demo-provider ................................ SUCCESS [ 2.700 s]
[INFO] dubbo-demo-consumer ................................ SUCCESS [ 2.745 s]
[INFO] dubbo-test ......................................... SUCCESS [ 0.012 s]
[INFO] dubbo-test-benchmark ............................... SUCCESS [01:01 min]
[INFO] dubbo-test-compatibility ........................... SUCCESS [ 0.188 s]
[INFO] dubbo-test-integration ............................. SUCCESS [ 0.052 s]
[INFO] dubbo-test-examples ................................ SUCCESS [ 1.640 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:42 min
[INFO] Finished at: 2015-01-20T14:36:43+08:00
[INFO] Final Memory: 282M/895M
[INFO] ------------------------------------------------------------------------

  4、 构建过程中如果出现maven网络连接相关的异常信息,可以尝试将以下maven mirror 地址添加到 settings.xml 文件。

<mirror>
<id>CN</id>
<mirrorOf>central</mirrorOf>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror> <mirror>
<id>kafeitu</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://maven.kafeitu.me/nexus/content/repositories/public</url>
</mirror> <mirror>
<id>lvu.cn</id>
<name>lvu.cn</name>
<url>http://lvu.cn/nexus/content/groups/public</url>
<mirrorOf>*</mirrorOf>
</mirror>

三、Zookeeper部署(Registry 服务注册中心)

  Dubbo 缺省配置通过 multicast 注册中心广播实现 Provider 和 Consumer 之间的简单远程过程调用(Simple RPC),不需要通过 Registry 注册中心进行注册调度,类似于spring rmi remoting调用,但由于不是Cluster部署,所以作为分布式RPC框架官方建议使用 Zookeeper 作为Registry注册中心服务器(同样支持Redis)实现服务的注册、发现、路由功能。Dubbo在Zookeeper服务器端只增加了dubbo数据节点(如下图),无需其他任何配置,所以只需安装或使用现有 Zookeeper 服务器即可,关于Zookeeper的安装部署可以参照之前的博文:http://www.cnblogs.com/lengfo/p/4146797.html,不再重复介绍。

  

  Zookeeper同样支持集群部署,提供最终一致性数据服务,本文实验环境只使用了一台Linux服务器作为ZK Registry注册服务器,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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/glf/zookeeperServer/data
dataLogDir=/home/glf/zookeeperServer/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

四、服务提供方开发(Provider)

  1、创建Java测试项目 DubboDemo ,集成Spring Framework。由于dubbo基于Spring的Schema扩展进行加载,依赖于低版本spring,所以如果开发项目中使用高版本(spring 3.0及以上)会出现xml解析冲突,官方QA中也有相关提及:http://alibaba.github.io/dubbo-doc-static/FAQ-zh.htm,所以本文只集成了2.5.6版本spring,更多相关版本问题也有待进一步研究,Todo

  2、集成dubbo及相关依赖包,参照下图,如果使用dubbo其他特性可能需要依赖更多包,比如jedis。

  

  3、创建测试服务接口(IGoodsManager)

package ibusiness;

public interface IGoodsManager {
public String LoadGoods();
}

  4、创建测试服务实现类(GoodsManager)

package business;

import com.alibaba.dubbo.rpc.RpcContext;
import ibusiness.IGoodsManager; public class GoodsManager implements IGoodsManager {
@Override
public String LoadGoods() {
return "可口可乐,雀巢咖啡 " + "/n response form provider: " + RpcContext.getContext().getLocalAddress();
}
}

  5、通过spring 集成并配置dubbo测试服务,同时指定registry的zookeeper服务器地址。

<?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 服务提供者应用名称 -->
<dubbo:application name="demo-dubbo-provider-app" /> <!--dubbo 注册中心-->
<dubbo:registry address="zookeeper://10.129.221.64:2181" /> <!--服务提供者 端口-->
<dubbo:protocol name="dubbo" port="30001" /> <!--dubbo提供服务-->
<dubbo:service interface="ibusiness.IGoodsManager" ref="goodsService" /> <!--spring bean 对象-->
<bean id="goodsService" class="business.GoodsManager" />
</beans>

  6、编写控制台程序启动spring容器,编译并打包Provider.jar

package dubboProvider;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProvider {
public static void main(String[] args) {
try {
// 初始化Spring
ApplicationContext ctx = new ClassPathXmlApplicationContext("dubboProvider\\dubboProvider.xml"); System.out.println("dubbo provider is running...");
System.in.read();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

五、服务消费方开发(Consumer)

  1、通过spring 配置指定registry的zookeeper地址,实现对dubbo远程服务的调用

<?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="demo-dubbo-consumer-app" /> <dubbo:registry address="zookeeper://10.129.221.64:2181" /> <!-- 和本地bean一样实现服务 -->
<dubbo:reference id="goodsService" interface="ibusiness.IGoodsManager" />
</beans>

  2、编写调用测试客户端代码,从容器中获取远程bean并调用。

package dubboConsumer;
import ibusiness.IGoodsManager; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboConsumer { public static void main(String[] args) { // 初始化Spring
ApplicationContext ctx = new ClassPathXmlApplicationContext("dubboConsumer\\dubboConsumer.xml"); IGoodsManager goodsManager = (IGoodsManager) ctx.getBean("goodsService"); // 获取远程服务代理
String goodsStr = goodsManager.LoadGoods() ; // 执行远程方法
System.out.println(goodsStr); }
}

  3、分别启动 provider、consumer 的测试程序发布和调用服务,即简单实现了基于 zookeeper registry 的 dubbo 远程过程调用。

  

六、More about dubbo

  以上简单的展示dubbo的RPC服务能力,更多关于dubbo的集群管理、监控、负载均衡、高可用性、可扩展、伸缩性等相关内容请期待后续博文。

七、向开源工作者和组织致敬,@dubbo team,感谢对开源事业作出的任何贡献

基于开源Dubbo分布式RPC服务框架的部署整合的更多相关文章

  1. 【Rpc】基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  2. 基于netty轻量的高性能分布式RPC服务框架forest<上篇>

    工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...

  3. 基于netty轻量的高性能分布式RPC服务框架forest<下篇>

    基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...

  4. RSF 分布式 RPC 服务框架的分层设计

    RSF 是个什么东西? 一个高可用.高性能.轻量级的分布式服务框架.支持容灾.负载均衡.集群.一个典型的应用场景是,将同一个服务部署在多个Server上提供 request.response 消息通知 ...

  5. RPC服务框架dubbo(一):简介和原理解析

    前置概念 在学习dubbo前,需要先了解SOA和RPC这两个概念. SOA 1.英文名称(Service Oriented Ambiguity) 2.中文名称:面向服务架构 2.1 有一个专门提供服务 ...

  6. Thrift 个人实战--Thrift RPC服务框架日志的优化

    前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还 ...

  7. RPC服务框架探索之Thrift

    前言架构服务化后,需要实现一套方便调用各服务的框架,现在开源如日中天,优先会寻找开源实现,如果没有合适自家公司业务的,才会考虑从零开发,尤其是一切以KPI为准绳的公司,谁会跟钱过不去?N个月之前,公司 ...

  8. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

  9. dubbo 订阅 RPC 服务

    Dubbo 订阅 RPC 服务 建立消费者者项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

随机推荐

  1. PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)

    PHP面向对象三大特点学习 学习目标:充分理解抽象.封装.继承.多态   面象对向的三大特点:封装性.继承性.多态性 首先简单理解一下抽象:我们在前面定义一个类的时候,实际上就是把一类事物共有的属性和 ...

  2. ubuntu14.04中国源

    deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://cn.ar ...

  3. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  4. 【转】Apache Solr 访问权限控制

    本文转自:http://www.cnblogs.com/ibook360/archive/2011/11/07/2239247.html 在Tomcat6增加 Solr的访问权限方法如下: 编辑tom ...

  5. Android 中的selector

    今天做程序时,发现了selector 选择器不单单能用系统的自定义属性(比如,  <item android:state_selected="true" android:co ...

  6. Simple Chroma Key 0.1.16 图片抠像(vs2003) 无任何插件

    学习扣像的最基础代码 (只支持 BMP TGA) simplechromakey.rar

  7. codeforces 467C.George and Job 解题报告

    题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...

  8. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

  9. c++流的读写

    std::istream input_stream;//这是一个文件流,想把它写入文件 思路是,先将input_stream流读入一个char* buffer; 然后用std::ofstream将bu ...

  10. CSRF和XSS

    XSS(跨站脚本攻击): 攻击者发现XSS漏洞——构造代码——发送给受害人——受害人打开——攻击者获取受害人的cookie——完成攻击 XSS是什么?它的全名是:Cross-site scriptin ...