示例:
<?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"
/> <dubbo:registry
address="multicast://224.5.6.7:1234"
/> <dubbo:protocol
name="dubbo"
port="20880"
/> <dubbo:service
interface="com.alibaba.dubbo.demo.DemoService"
ref="demoServiceLocal"
/> <dubbo:reference
id="demoServiceRemote"
interface="com.alibaba.dubbo.demo.DemoService"
/></beans> |
| 所有标签者支持自定义参数,用于不同扩展点实现的特殊配置。 |
如:
<dubbo:protocol
name="jms"></dubbo:protocol> |
或:(2.1.0开始支持)
| 注意声明:xmlns:p="http://www.springframework.org/schema/p" |
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"></beans> |
Configuration Relation:

Configuration Override:
| 如果公共配置很简单,没有多注册中心,多协议等情况,或者想多个Spring容器想共享配置,可以使用dubbo.properties作为缺省配置。 |
| Dubbo将自动加载classpath根目录下的dubbo.properties,可以通过JVM启动参数:-Ddubbo.properties.file=xxx.properties 改变缺省配置位置。 |
| 如果classpath根目录下存在多个dubbo.properties,比如多个jar包中有dubbo.properties,Dubbo会任意加载,并打印Error日志,后续可能改为抛异常。 |
映射规则:
典型配置如:
dubbo.application.name=foodubbo.application.owner=bardubbo.registry.address=10.20.153.10:9090 |

覆盖策略:
| 2.2.1以上版本支持 |
服务提供方注解:
import
com.alibaba.dubbo.config.annotation.Service;@Service(version="1.0.0")public
class FooServiceImpl implements
FooService { // ......} |
服务提供方配置:
<!-- 公共信息,也可以用dubbo.properties配置 --><dubbo:application
name="annotation-provider"
/><dubbo:registry
address="127.0.0.1:4548"
/><!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --><dubbo:annotation
package="com.foo.bar.service"
/> |
服务消费方注解:
import
com.alibaba.dubbo.config.annotation.Reference;import
org.springframework.stereotype.Component;@Componentpublic
class BarAction { @Reference(version="1.0.0") private
FooService fooService;} |
服务消费方配置:
<!-- 公共信息,也可以用dubbo.properties配置 --><dubbo:application
name="annotation-consumer"
/><dubbo:registry
address="127.0.0.1:4548"
/><!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --><dubbo:annotation
package="com.foo.bar.action"
/> |
也可以使用:(等价于前面的:<dubbo:annotation package="com.foo.bar.service" />)
<dubbo:annotation
/><context:component-scan
base-package="com.foo.bar.service"> <context:include-filter
type="annotation"
expression="com.alibaba.dubbo.config.annotation.Service"
/></context:component-scan> |
Spring2.5及以后版本支持component-scan,如果用的是Spring2.0及以前版本,需配置:
|
| API属性含义参考 API属性与配置项一对一,各属性含义,请参见:配置参考手册 (+), 比如:ApplicationConfig.setName("xxx") 对应 <dubbo:application name="xxx" /> |
import
com.alibaba.dubbo.rpc.config.ApplicationConfig;import
com.alibaba.dubbo.rpc.config.RegistryConfig;import
com.alibaba.dubbo.rpc.config.ProviderConfig;import
com.alibaba.dubbo.rpc.config.ServiceConfig;import
com.xxx.XxxService;import
com.xxx.XxxServiceImpl;// 服务实现XxxService xxxService =
new XxxServiceImpl();// 当前应用配置ApplicationConfig application =
new ApplicationConfig();application.setName("xxx");// 连接注册中心配置RegistryConfig registry =
new RegistryConfig();registry.setAddress("10.20.130.230:9090");registry.setUsername("aaa");registry.setPassword("bbb");// 服务提供者协议配置ProtocolConfig protocol =
new ProtocolConfig();protocol.setName("dubbo");protocol.setPort(12345);protocol.setThreads(200);// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口// 服务提供者暴露服务配置ServiceConfig<XxxService> service =
new ServiceConfig<XxxService>();
// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏service.setApplication(application);service.setRegistry(registry);
// 多个注册中心可以用setRegistries()service.setProtocol(protocol);
// 多个协议可以用setProtocols()service.setInterface(XxxService.class);service.setRef(xxxService);service.setVersion("1.0.0");// 暴露及注册服务service.export(); |
import
com.alibaba.dubbo.rpc.config.ApplicationConfig;import
com.alibaba.dubbo.rpc.config.RegistryConfig;import
com.alibaba.dubbo.rpc.config.ConsumerConfig;import
com.alibaba.dubbo.rpc.config.ReferenceConfig;import
com.xxx.XxxService;// 当前应用配置ApplicationConfig application =
new ApplicationConfig();application.setName("yyy");// 连接注册中心配置RegistryConfig registry =
new RegistryConfig();registry.setAddress("10.20.130.230:9090");registry.setUsername("aaa");registry.setPassword("bbb");// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接// 引用远程服务ReferenceConfig<XxxService> reference =
new ReferenceConfig<XxxService>();
// 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏reference.setApplication(application);reference.setRegistry(registry);
// 多个注册中心可以用setRegistries()reference.setInterface(XxxService.class);reference.setVersion("1.0.0");// 和本地bean一样使用xxxServiceXxxService xxxService = reference.get();
// 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用 |
注:下面只列出不同的地方,其它参见上面的写法
...// 方法级配置List<MethodConfig> methods =
new ArrayList<MethodConfig>();MethodConfig method =
new MethodConfig();method.setName("createXxx");method.setTimeout(10000);method.setRetries(0);methods.add(method);// 引用远程服务ReferenceConfig<XxxService> reference =
new ReferenceConfig<XxxService>();
// 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏...reference.setMethods(methods);
// 设置方法级配置... |
...ReferenceConfig<XxxService> reference =
new ReferenceConfig<XxxService>();
// 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏// 如果点对点直连,可以用reference.setUrl()指定目标地址,设置url后将绕过注册中心,// 其中,协议对应provider.setProtocol()的值,端口对应provider.setPort()的值,// 路径对应service.setPath()的值,如果未设置path,缺省path为接口名reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService");
... |
原文:http://blog.csdn.net/p0p0p0p01/article/details/44811007