1. 概述
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案
主要核心部件
Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
2. 简单实例
首先maven项目增加dubbo的jar依赖,因为要用到zookeeper注册中心,也要依赖但是要去掉自带的log4j不然会默认的版本依赖jms-1.1.jar jmxtools-1.2.1.jar jmxri-1.2.1.jar等3个包,下载挺麻烦,当然如果个人已经在自己的仓库中有了就无所谓了。
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
Spring的依赖自己添加就好了
因为要增加zookeeper的注册管理,所以如果有可用的zookeeper就用可用的zookeeper,没有可以按照如下的安装去本地安装一个。
http://blog.csdn.net/ruishenh/article/details/23180355
项目结构图
/gomeTest/src/main/resources/spring/dubbo-provider.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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:applicationname="hello-world-app" />
<!--zookeeper注册中心 -->
<dubbo:registryprotocol="zookeeper" address="10.57.41.19:2181" />
<!--使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://10.57.41.19:1234" />-->
<dubbo:protocolname="dubbo" port="20880" />
<dubbo:serviceinterface="com.ruishenh.dubbo.example.DemoService"
ref="demoService"/> <!-- 和本地bean一样实现服务 -->
<beanid="demoService"class="com.ruishenh.dubbo.example.DemoServiceImpl" />
</beans>
/gomeTest/src/main/resources/spring/dubbo-consumer.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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:applicationname="consumer-of-helloworld-app" />
<!--zookeeper注册中心 -->
<dubbo:registry protocol="zookeeper"address="10.57.41.19:2181" />
<!--使用multicast广播注册中心暴露的服务地址 -->
<!--<dubbo:registryaddress="multicast://10.57.41.19:1234" /> -->
<!-- 生成远程服务代理,可以和本地bean一样使用demoService-->
<dubbo:referenceid="demoService"interface="com.ruishenh.dubbo.example.DemoService" />
</beans>
/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoService.java
package com.ruishenh.dubbo.example;
public interface DemoService {
public voidsayHello();
public String returnHello();
public MsgInfo returnMsgInfo(MsgInfo info);
}
/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoServiceImpl.java
package com.ruishenh.dubbo.example;
public class DemoServiceImpl implements DemoService{
public void sayHello() {
System.out.println("hello world!");
}
public String returnHello() {
return "hello world!";
}
public MsgInforeturnMsgInfo(MsgInfo info) {
info.getMsgs().add("处理完毕");
return info;
}
}
/gomeTest/src/main/java/com/ruishenh/dubbo/example/LuncherProvider.java
package com.ruishenh.dubbo.example;
importorg.springframework.context.ApplicationContext;
importorg.springframewor