一、前言

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. JavaWeb温习之HttpServletResponse对象

    以下内容均根据"方立勋JavaWeb视频教程"进行总结 1. HttpServletResponse常见应用——设置响应头控制浏览器的行为 1.1 设置http响应头控制浏览器禁止 ...

  2. 转移wordpress到另一台主机

    做项目的代码是两个人,我想把另一个小伙伴做的转移到自己的linux系统上(主要是linux下一片空白,从头做太浪费时间了) 这个过程其实也可以用来类比从本地到服务器的过程(可能略有不同,真上线的时候会 ...

  3. 百度地图API开发----手机地图做导航功能

    第一种方式:手机网页点击打开直接进百度地图APP <a href="baidumap://map/direction?mode=[transit:公交,driving:驾车]& ...

  4. 基础概念 之 Spark on Yarn

    先抛出问题:Spark on Yarn有cluster和client两种模式,它们有什么区别? 用Jupyter写Spark时,只能使用client模式,为什么? 写一篇文章,搞清楚 Spark on ...

  5. Ajax 完整教程(转载)

    第 1 页 Ajax 简介 Ajax 由 HTML.JavaScript™ 技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.本文的作者是 ...

  6. instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例 , 返回true或者false

    <?phpclass MyClass{} class NotMyClass{}$a = new MyClass; var_dump($a instanceof MyClass);var_dump ...

  7. ZOJ 3469Food Delivery(区间DP)

    Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving prob ...

  8. 转!idea 破解版 安装

    原博文地址:https://blog.csdn.net/everest_man/article/details/78985879 1.官网下载  Ultimate版本 2.http://idea.la ...

  9. MVC项目,bootstrap升级后index.d.ts编译出错

    安装最新的Typescript组件 下载链接 https://www.microsoft.com/en-us/download/details.aspx?id=48593

  10. idea中使用插件lombok简化java bean的getter/setter/log等常用功能

    一.安装. 1. 2. 3. 4. . 二.使用 1. 2. 3. 结果分析,如果没有添加@Setter注解,则LombokTest中的student示例无法使用setAge()等方法.使用lombo ...