配置加载流程

此篇文档主要讲在应用启动阶段,Dubbo框架如何将所需要的配置采集起来(包括应用配置、注册中心配置、服务配置等),以完成服务的暴露和引用流程。

根据驱动方式的不同(比如Spring或裸API编程)配置形式上肯定会有所差异,具体请参考XML配置Annotation配置API配置三篇文档。除了外围驱动方式上的差异,Dubbo的配置读取总体上遵循了以下几个原则:

  1. Dubbo支持了多层级的配置,并按预定优先级自动实现配置间的覆盖,最终所有配置汇总到数据总线URL后驱动后续的服务暴露、引用等流程。
  2. ApplicationConfig、ServiceConfig、ReferenceConfig可以被理解成配置来源的一种,是直接面向用户编程的配置采集方式。
  3. 配置格式以Properties为主,在配置内容上遵循约定的path-based的命名规范

配置来源

首先,从Dubbo支持的配置来源说起,默认有四种配置来源:

  • JVM System Properties,-D参数
  • Externalized Configuration,外部化配置
  • ServiceConfig、ReferenceConfig等编程接口采集的配置
  • 本地配置文件dubbo.properties

覆盖关系

下图展示了配置覆盖关系的优先级,从上到下优先级依次降低:

覆盖关系

点此查看外部化配置详情

配置格式

目前Dubbo支持的所有配置都是.properties格式的,包括-DExternalized Configuration等,.properties中的所有配置项遵循一种path-based的配置格式:

  1. # 应用级别
  2. dubbo.{config-type}[.{config-id}].{config-item}={config-item-value}
  3. # 服务级别
  4. dubbo.service.{interface-name}[.{method-name}].{config-item}={config-item-value}
  5. dubbo.reference.{interface-name}[.{method-name}].{config-item}={config-item-value}
  6. # 多配置项
  7. dubbo.{config-type}s.{config-id}.{config-item}={config-item-value}
  • 应用级别
  1. dubbo.application.name=demo-provider
  2. dubbo.registry.address=zookeeper://127.0.0.1:2181
  3. dubbo.protocol.port=-1
  • 服务级别
  1. dubbo.service.org.apache.dubbo.samples.api.DemoService.timeout=5000
  2. dubbo.reference.org.apache.dubbo.samples.api.DemoService.timeout=6000
  3. dubbo.reference.org.apache.dubbo.samples.api.DemoService.sayHello.timeout=7000
  • 多配置项
  1. dubbo.registries.unit1.address=zookeeper://127.0.0.1:2181
  2. dubbo.registries.unit2.address=zookeeper://127.0.0.1:2182
  3. dubbo.protocols.dubbo.name=dubbo
  4. dubbo.protocols.dubbo.port=20880
  5. dubbo.protocols.hessian.name=hessian
  6. dubbo.protocols.hessian.port=8089
  • 扩展配置
  1. dubbo.application.parameters.item1=value1
  2. dubbo.application.parameters.item2=value2
  3. dubbo.registry.parameters.item3=value3
  4. dubbo.reference.org.apache.dubbo.samples.api.DemoService.parameters.item4=value4

几种编程配置方式

接下来,我们看一下选择不同的开发方式时,对应到ServiceConfig、ReferenceConfig等编程接口采集的配置的变化。

Spring

  • XML

参见示例

  1. <!-- dubbo-provier.xml -->
  2. <dubbo:application name="demo-provider"/>
  3. <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>
  4. <dubbo:registry address="zookeeper://127.0.0.1:2181" simplified="true"/>
  5. <dubbo:metadata-report address="redis://127.0.0.1:6379"/>
  6. <dubbo:protocol name="dubbo" port="20880"/>
  7. <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
  8. <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
  • Annotation

参见示例

  1. // AnnotationService服务实现
  2. @Service
  3. public class AnnotationServiceImpl implements AnnotationService {
  4. @Override
  5. public String sayHello(String name) {
  6. System.out.println("async provider received: " + name);
  7. return "annotation: hello, " + name;
  8. }
  9. }
  1. ## dubbo.properties
  2. dubbo.application.name=annotation-provider
  3. dubbo.registry.address=zookeeper://127.0.0.1:2181
  4. dubbo.protocol.name=dubbo
  5. dubbo.protocol.port=20880
  • Spring Boot

参见示例

  1. ## application.properties
  2. # Spring boot application
  3. spring.application.name=dubbo-externalized-configuration-provider-sample
  4. # Base packages to scan Dubbo Component: @com.alibaba.dubbo.config.annotation.Service
  5. dubbo.scan.base-packages=com.alibaba.boot.dubbo.demo.provider.service
  6. # Dubbo Application
  7. ## The default value of dubbo.application.name is ${spring.application.name}
  8. ## dubbo.application.name=${spring.application.name}
  9. # Dubbo Protocol
  10. dubbo.protocol.name=dubbo
  11. dubbo.protocol.port=12345
  12. ## Dubbo Registry
  13. dubbo.registry.address=N/A
  14. ## DemoService version
  15. demo.service.version=1.0.0

API

  1. public static void main(String[] args) throws IOException {
  2. ServiceConfig<GreetingsService> service = new ServiceConfig<>();
  3. service.setApplication(new ApplicationConfig("first-dubbo-provider"));
  4. service.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
  5. service.setInterface(GreetingsService.class);
  6. service.setRef(new GreetingsServiceImpl());
  7. service.export();
  8. System.out.println("first-dubbo-provider is running.");
  9. System.in.read();
  10. }

参考示例