本地服务:(Spring配置)
local.xml
<
bean
id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<
bean
id=“xxxAction” class=“com.xxx.XxxAction”>
<
property
name=“xxxService” ref=“xxxService” />
</
bean
>
|
远程服务:(Spring配置)
在本地服务的基础上,只需做简单配置,即可完成远程化:
- 将上面的local.xml配置拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml。
- 并在提供方增加暴露服务配置<dubbo:service>,在消费方增加引用服务配置<dubbo:reference>。
如下:
remote-provider.xml
<
bean
id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<!-- 和本地服务一样实现远程服务 -->
<
dubbo:service
interface=“com.xxx.XxxService” ref=“xxxService” />
<!-- 增加暴露远程服务配置 -->
|
remote-consumer.xml
<
dubbo:reference
id=“xxxService” interface=“com.xxx.XxxService” />
<!-- 增加引用远程服务配置 -->
<
bean
id=“xxxAction” class=“com.xxx.XxxAction”>
<!-- 和本地服务一样使用远程服务 -->
<
property
name=“xxxService” ref=“xxxService” />
</
bean
>
|
快速启动
![]() | Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。 |
服务提供者
(#)
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
DemoService.java
package
com.alibaba.dubbo.demo;
public
interface
DemoService {
String sayHello(String name);
}
|
在服务提供方实现接口:(对服务消费方隐藏实现)
DemoServiceImpl.java
package
com.alibaba.dubbo.demo.provider;
import
com.alibaba.dubbo.demo.DemoService;
public
class
DemoServiceImpl
implements
DemoService {
public
String sayHello(String name) {
return
"Hello "
+ name;
}
}
|
用Spring配置声明暴露服务:
provider.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
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
>
|
加载Spring配置:
Provider.java
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配置引用远程服务:
consumer.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
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配置,并调用远程服务:(也可以使用IoC注入)
Consumer.java
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 );
// 显示调用结果
}
}
|
必需依赖
- JDK1.5+
![]() | 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策略。 |
缺省依赖
通过mvn dependency:tree > dep.log命令分析,Dubbo缺省依赖以下三方库:
[INFO] +- com.alibaba:dubbo:jar:2.1.2:compile
[INFO] | +- log4j:log4j:jar:1.2.16:compile
[INFO] | +- org.javassist:javassist:jar:3.15.0-GA:compile
[INFO] | +- org.springframework:spring:jar:2.5.6.SEC03:compile
[INFO] | +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | \- org.jboss.netty:netty:jar:3.2.5.Final:compile
|
这里所有依赖都是换照Dubbo缺省配置选的,这些缺省值是基于稳定性和性能考虑的。
- log4j.jar和commons-logging.jar日志输出包。
- 可以直接去掉,dubbo本身的日志会自动切换为JDK的java.util.logging输出。
- 但如果其它三方库比如spring.jar间接依赖commons-logging,则不能去掉。
- javassist.jar 字节码生成。
- 如果<dubbo:provider proxy="jdk" />或<dubbo:consumer proxy="jdk" />,以及<dubbo:application compiler="jdk" />,则不需要。
- spring.jar 配置解析。
- 如果用ServiceConfig和ReferenceConfig的API调用,则不需要。
- netty.jar 网络传输。
- 如果<dubbo:protocol server="mina"/>或<dubbo:protocol server="grizzly"/>,则换成mina.jar或grizzly.jar。
- 如果<protocol name="rmi"/>,则不需要。
可选依赖
以下依赖,在主动配置使用相应实现策略时用到,需自行加入依赖。
- mina: 1.1.7
- grizzly: 2.1.4
- httpclient: 4.1.2
- hessian_lite: 3.2.1-fixed
- xstream: 1.4.1
- fastjson: 1.1.8
- zookeeper: 3.3.3
- jedis: 2.0.0
- xmemcached: 1.3.6
- jfreechart: 1.0.13
- hessian: 4.0.7
- jetty: 6.1.26
- hibernate-validator: 4.2.0.Final
- zkclient: 0.1
- curator: 1.1.10
- cxf: 2.6.1
- thrift: 0.8.0
JEE:
- servlet: 2.5
- bsf: 3.1
- validation-api: 1.0.0.GA
- jcache: 0.4
- <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心。
- <dubbo:reference/> 引用配置,用于创建一个远程服务代理,一个引用可以指向多个注册中心。
- <dubbo:protocol/> 协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
- <dubbo:application/> 应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
- <dubbo:module/> 模块配置,用于配置当前模块信息,可选。
- <dubbo:registry/> 注册中心配置,用于配置连接注册中心相关信息。
- <dubbo:monitor/> 监控中心配置,用于配置连接监控中心相关信息,可选。
- <dubbo:provider/> 提供方的缺省值,当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值,可选。
- <dubbo:consumer/> 消费方缺省配置,当ReferenceConfig某属性没有配置时,采用此缺省值,可选。
- <dubbo:method/> 方法配置,用于ServiceConfig和ReferenceConfig指定方法级的配置信息。
- <dubbo:argument/> 用于指定方法参数配置。
Configuration Override:
- 上图中以timeout为例,显示了配置的查找顺序,其它retries, loadbalance, actives等类似。
- 方法级优先,接口级次之,全局配置再次之。
- 如果级别一样,则消费方优先,提供方次之。
- 其中,服务提供方配置,通过URL经由注册中心传递给消费方。
- 建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置。
- 理论上ReferenceConfig的非服务标识配置,在ConsumerConfig,ServiceConfig, ProviderConfig均可以缺省配置。