Dubbo 提供多种配置方式,并拥有不同的优先级
XML 配置
provider
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-provider"/><dubbo:registry address="zookeeper://127.0.0.1:2181"/><dubbo:protocol name="dubbo" port="20890"/><bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/><dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/></beans>
consumer
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-consumer"/><dubbo:registry group="aaa" address="zookeeper://127.0.0.1:2181"/><dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/></beans>

| 标签 | 用途 | 解释 | 
|---|---|---|
| 服务配置 | 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 | |
| 引用配置 | 用于创建一个远程服务代理,一个引用可以指向多个注册中心 | |
| 协议配置 | 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 | |
| 应用配置 | 用于配置当前应用信息,不管该应用是提供者还是消费者 | |
| 模块配置 | 用于配置当前模块信息,可选 | |
| 注册中心配置 | 用于配置连接注册中心相关信息 | |
| 监控中心配置 | 用于配置连接监控中心相关信息,可选 | |
| 提供方配置 | 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选 | |
| 消费方配置 | 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选 | |
| 方法配置 | 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息 | |
| 参数配置 | 用于指定方法参数配置 | 
不同粒度配置的覆盖关系
以 timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似:
- 方法级优先,接口级次之,全局配置再次之。
 - 如果级别一样,则消费方优先,提供方次之。
 
其中,服务提供方配置,通过 URL 经由注册中心传递给消费方。
        
配置关联
- dubbo:service ==> ServiceConfig
 - dubbo:reference ===> ReferenceConfig
 - dubbo:protocol ===> ProtocolConfig
 - dubbo:application ===> ApplicationConfig
 - dubbo:registry ===> RegistryConfig
 - dubbo:provider ===> providerConfig
 - dubbo:consumer ===> ConsumerConfig
 
这些配置的XML文件最终都会组成一个*Config文件配置. 然后按照上上图中的意图添加默认参数. 进行服务暴露即可/服务引用即可
API 配置
按照XML配置生成*Config的描述,可以直接使用Java Bean的格式来进行引用即可
服务提供者
import org.apache.dubbo.rpc.config.ApplicationConfig;import org.apache.dubbo.rpc.config.RegistryConfig;import org.apache.dubbo.rpc.config.ProviderConfig;import org.apache.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 org.apache.dubbo.rpc.config.ApplicationConfig;import org.apache.dubbo.rpc.config.RegistryConfig;import org.apache.dubbo.rpc.config.ConsumerConfig;import org.apache.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(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
点对点直连
...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");...
注解配置
需要
2.6.3及以上版本支持。 点此查看 https://github.com/apache/dubbo-samples/tree/master/java/dubbo-samples-annotation
服务提供方
Service 注解暴露服务
@Servicepublic class AnnotationServiceImpl implements AnnotationService {@Overridepublic String sayHello(String name) {return "annotation: hello, " + name;}}
增加应用共享配置
# dubbo-provider.propertiesdubbo.application.name=annotation-providerdubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.protocol.name=dubbodubbo.protocol.port=20880
指定Spring扫描路径
@Configuration@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl")@PropertySource("classpath:/spring/dubbo-provider.properties")static public class ProviderConfiguration {}
服务消费方
Reference注解引用服务
@Component("annotationAction")public class AnnotationAction {@Referenceprivate AnnotationService annotationService;public String doSayHello(String name) {return annotationService.sayHello(name);}}
