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

一、前言

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架构图:

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

  • 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文件夹。

【Rpc】基于开源Dubbo分布式RPC服务框架的部署整合
[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 服务注册中心)

D ubbo 缺省配置通过 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 ,不再重复介绍。

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

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。

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

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 远程过程调用。

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

六、More about dubbo

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

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

上一篇:RSF 分布式 RPC 服务框架的分层设计


下一篇:基于开源Dubbo分布式RPC服务框架的部署整合