dubbo 是一个分布式服务框架,致力于提供高性能和透明化的rpc远程服务调用方案,以及soa服务治理方案。
dubbo 就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东。
说白了就是个远程服务调用的分布式框架(告别web service模式中的wsdl,以服务者与消费者的方式在dubbo上注册)
其核心部分包含:
1. 远程通讯: 提供对多种基于长连接的nio框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查×××提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何api侵入。
2.软负载均衡及容错机制,可在内网替代f5等硬件负载均衡器,降低成本,减少单点。
3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的ip地址,并且能够平滑添加或删除服务提供者。
dubbo采用全spring配置方式,透明化接入应用,对应用没有任何api侵入,只需用spring加载dubbo的配置即可,dubbo基于spring的schema扩展进行加载。
dubbo架构图如下所示:

provider: 暴露服务的服务提供方。
consumer: 调用远程服务的服务消费方。
registry: 服务注册与发现的注册中心。
monitor: 统计服务的调用次调和调用时间的监控中心。
container: 服务运行容器。
0 服务容器负责启动,加载,运行服务提供者。
dubbo采用全spring配置方式,透明化接入应用,对应用没有任何api侵入,只需用spring加载dubbo的配置即可,dubbo基于spring的schema扩展进行加载。如果不想使用spring配置,而希望通过api的方式进行调用(不推荐)
下面我们就来看看spring配置方式的写法:
1. 下载 zookeeper 注册中心,
下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/
下载后解压即可,进入d:\apach-zookeeper-3.4.5\bin,
双击zkserver.cmd启动注册中心服务。
package com.unj.dubbotest.provider;
import java.util.list;
public interface demoservice {
string sayhello(string name);
public list getusers();
}
import java.util.arraylist;
import java.util.linkedlist;
public class demoserviceimpl implements demoservice{
public string sayhello(string name) {
return "hello " + name;
}
public list getusers() {
list list = new arraylist();
list.add("u1");
list.add("u2");
list.add("u3");
return list;
用spring配置声明暴露服务:
<?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
">
<!-- 具体的实现bean -->
<bean id="demoservice" class="com.unj.dubbotest.provider.demoserviceimpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="xixi_provider" />
<!-- 使用multicast广播注册中心暴露服务地址
<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.unj.dubbotest.provider.demoservice" ref="demoservice" />
</beans>
加载spring配置,启动服务:
package com.unj.dubbotest.provider;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class provider {
public static void main(string[] args) throws exception {
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(new string[] {"applicationcontext.xml"});
context.start();
system.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
applicationcontext-dubbo.xml 中注册自己需要调用的接口,我刚开始测试的时候需要的接口很多,所以把这个文件写的满满的,后来熟悉了把接口按业务类型分开,写了n多个 applicationcontext-dubbo-***.xml 简练多了 》。
1.通过spring配置引用远程服务:
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="hehe_consumer" />
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoservice -->
<dubbo:reference id="demoservice"
interface="com.unj.dubbotest.provider.demoservice" />
2.加载spring配置,并调用远程服务:
package com.alibaba.dubbo.demo.pp;
import java.util.list;
import com.unj.dubbotest.provider.demoservice;
public class consumer {
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(
new string[] { "applicationcontext.xml" });
context.start();
demoservice demoservice = (demoservice) context.getbean("demoservice"); //
string hello = demoservice.sayhello("tom"); //
system.out.println(hello); //
//
list list = demoservice.getusers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
system.out.println(list.get(i));
}
}
// system.out.println(demoservice.hehe());
system.in.read();
}
调用结果为:
dubbo管理页面:
这个管理页面还需要部署一个环境的,一开始我还以为是dubbo自带的,找了半天没有找到....
应用页面:
提供者页面:
消费者页面:
服务页面:
测试是否成功,我觉得只要看看状态是否正常,就ok了 ....
案例代码下载:http://download.csdn.net/detail/yiyu1/7116319