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

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

  • 支持多层级配置,按照预定的优先级进行层层覆盖,最终将采集的配置汇总到总线URL中,供后续服务暴露和服务引用所使用
  • ApplicationConfig、ServiceConfig、ReferenceConfig 可以被理解成配置来源的一种,是直接面向用户编程的配置采集方式。
  • 配置格式以 Properties 为主,在配置内容上遵循约定的 path-based 的命名规范

配置来源

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

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

JVM >> Externalized Configuration >> 用户编程 >> 本地配置文件

配置格式

目前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=7000dubbo.registries.unit1.address=zookeeper://127.0.0.1:2181
  4. dubbo.registries.unit2.address=zookeeper://127.0.0.1:2182
  5. dubbo.protocols.dubbo.name=dubbo
  6. dubbo.protocols.dubbo.port=20880
  7. dubbo.protocols.hessian.name=hessian
  8. 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

启动应用

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"/>

注解

  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
  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. }

SpringBoot

  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. }