http://dubbo.io/User+Guide-zh.htm
dubbo官方用户指南如上,读后有感
dubbo是阿里巴巴的开源项目,该项目专注服务提供和服务消费,是基于spring的一个扩展
适用于分布式服务,因此dubbo是一个分布式服务框架,既然是框架,框架内部就具备多样性,即支持可选的具体实现
好比我的毛坯房就是一个框架,我可以选择是用坐便器还是蹲便器,它们都能实现我拉屎的功能
也可以选择挂式空调或者柜式空调,它们都能制冷
dubbo划分有四种角色,
- Provider: 暴露服务的服务提供方。
- Consumer: 调用远程服务的服务消费方。
- Registry: 服务注册与发现的注册中心。
- Monitor: 统计服务的调用次调和调用时间的监控中心。
- Container: 服务运行容器。
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/dubbo/zookeeper-3.3.3/data
clientPort=2181
server.1=10.20.153.10:2555:3555
server.2=10.20.153.11:2555:3555
如果dubbo选择使用zookeeper作为注册中心,用法为在spring配置中使用dubbo的registry标签声明zookeeper做为注册中心,并附上zookeeper的服务器地址,如下所示
<
dubbo:registry
protocol
=
"zookeeper"
address
=
"10.20.153.10:2181,10.20.153.11:2181"
/>
|
由于spring本身是一个轻量级容器,可以不依赖第三方容器即可管理bean,用以下代码加载spring配置即可完成服务的发布
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
Provider {
public
static
void
main(String[] args)
throws
Exception {
ClassPathXmlApplicationContext context =
new
ClassPathXmlApplicationContext(
new
String[]{
"http://10.20.160.198/wiki/display/dubbo/provider.xml"
});
context.start();
System.in.read();
// 按任意键退出
}
}
spring的完整配置provider.xml如下:
<?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="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
下面是dubbo服务消费方需要做的
dubbo服务消费方也是基于spring配置的,通过Spring配置引用远程服务,配置文件consumer.xml如下
<?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="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
</beans>
加载上面这个Spring配置文件中的配置,就可以调用远程服务了,做法如下
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}