一、前言

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 服务注册中心)

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 ,不再重复介绍。

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 ,感谢对开源事业作出的任何贡献

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

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

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

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

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

  3. dubbo分布式rpc框架用法

    dubbo是阿里巴巴开源的分布式服务框架,致力于提供高性能和透明化的rpc远程服务调用方案,以及soa服务治理方案,如果没有分布式需求,是不需要dubbo的,分布式环境dubbo的使用架构官方给出了一 ...

  4. 3.阿里巴巴dubbo分布式rpc框架详细使用教程

    dubbo是阿里巴巴开源的分布式服务框架,致力于提供高性能和透明化的rpc远程服务调用方案,以及soa服务治理方案,如果没有分布式需求,是不需要dubbo的,分布式环境dubbo的使用架构官方给出了一 ...

  5. Dapeng框架-开源高性能分布式微服务框架

    我们公司性质是新零售,公司也有专门的框架组.这群大牛自己开发了一整套分布式微服务框架.我们也在使用这套框架,有很多心得体会. 该框架既Dapeng也!开源github地址:https://github ...

  6. 3.9-3.10 分布式协作服务框架Zookeeper

    一.zookeeper概述 一个开源的分布式的,为分布式应用提供协调服务的Apache项目. 提供一个简单的原语集合,以便于分布式应用可以在它之上构建更高层次的同步服务. 设计非常易于编程,它使用的是 ...

  7. 基于spring-boot的rest微服务框架

    周末在家研究spring-boot,参考github上的一些开源项目,整了一个rest微服务框架,取之于民,用之于民,在github上开源了,地址如下: https://github.com/yjmy ...

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

    1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的rest服务,通常这些服务要不就是用于手机app的开发,要不就是提供给第三方开 ...

  9. 分布式服务框架选型:面对Dubbo,阿里巴巴为什么选择了HSF?

    转载:http://www.sohu.com/a/141490021_268033 阿里巴巴集团内部使用的分布式服务框架 HSF(High Speed Framework,也有人戏称“好舒服”)已经被 ...

随机推荐

  1. [干货] 有了微信小程序,谁还学ReactNative?

    版权声明:本文由贺嘉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/145 来源:腾云阁 https://www.qclou ...

  2. libvirt kvm 虚拟机上网 – Bridge桥接

    版权声明:本文由陈煜东原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/90 来源:腾云阁 https://www.qclou ...

  3. 关于font-size对垂直居中影响的问题

    背景:三个inline-block元素,其中两个内容为空,另外一个包含文字,设置文字的font-size之后,原本垂直居中的三个inline-block的元素,会变的不再垂直居中. 原因: 当设置了f ...

  4. kerberos认证协议分析

    Kerberos认证协议分析 Kerberos认证协议流程 如上图: * 第一步:client和认证服务器(AS)通信完成认证过程,如果认证成功AS返回给client一个TGT(用来向TGS获取tic ...

  5. Android 微信分享,分享到朋友圈与分享到好友,以及微信登陆

    extends:http://www.cnblogs.com/android100/p/Android-qq.html 一.申请你的AppID http://open.weixin.qq.com/ 友 ...

  6. Android - 采用 SharedPreferences 存储数据

    SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data/data/<包名& ...

  7. MyBatis DAO层传递参数到mapping.xml 几种方式

    Dao层传递参数到mapping.xml文件的几种方式:(Mybatis传值总结) 第一种:传递单个参数 Dao层Code片段: /** * 根据articleId查询XXXX详情. * * @par ...

  8. 07.Curator计数器

        这一篇文章我们将学习使用Curator来实现计数器.顾名思义,计数器是用来计数的,利用ZooKeeper可以实现一个集群共享的计数器.只要使用相同的path就可以得到最新的计数器值,这是由Zo ...

  9. .NET截取指定长度字符超出部分以"..."代替

    /// <summary> /// 将指定字符串按指定长度进行剪切, /// </summary> /// <param name= "Str "&g ...

  10. 关于#ifndef以及#ifndef WIN32

    一般用法是这样的: 这里可以定义如下: #define XXXXX #ifdef XXXXX 这里做一些操作,这些操作只有在XXXX已经被define的情况下才会执行到.一般还可能有else,如 #e ...