dubbo user guider笔记之三配置方式详解

配置方式详解

目前dubbo提供了四种配置方式,他们分别是:

  1. XML Configuration
  2. Properties Configurtion
  3. API Configuration
  4. Annotation Configuration

下面我们来分别详细的介绍一下他们

XML Configuration

之前写过一个关于xml配置的入门demo,不妨先看看。

provider.xml demo

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/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="org.apache.dubbo.demo.DemoService" ref="demoServiceLocal" />  
    <dubbo:reference id="demoServiceRemote" interface="org.apache.dubbo.demo.DemoService" />  
</beans>

所有的标签都支持自定义的参数,所以我们可以满足不同扩展点的特殊配置需求,
例如:

<dubbo:protocol name="jms">
    <dubbo:parameter key="queue" value="your_queue" />
</dubbo:protocol>

or

 <dubbo:protocol name="jms" p:queue="your_queue" />  

关于这些标签的具体含义,暂时先不讲,现有一个大概映像即可。

The relations between configuration tags

image

标签名 目的(作用) 介绍
Service Export Used to export service, define service metadata, export service with multiple protocols, register service to multiple registries
Service Reference Used to create a remote proxy, subscribe to multiple registries
Protocol Config Configure the protocol for services on provider side, the consumer side follows.
Application Config Applies to both provider and consumer.
Module Config Optional.
Registry Center Registry info: address, protocol, etc.
Monitor Center Monitor info: address, address, etc. Optional.
Default Config for Providers Default Config for ServiceConfigs. Optional.
Default Config for Consumers Default Config for ReferenceConfigs. Optional.
Method level Config Method level Config for ServiceConfig and ReferenceConfig.
Argument Config Used to specify the method parameter configuration.

Overrides and Priorities(属性的覆盖和优先级)

拿timeout做一个例子 (retries, loadbalance, actives 也适用),从高到低的优先级如下所示:

  • 方法级,接口级,默认/全局级
  • 相同等级的情况下,consumer的优先级比provider的更高

provider端的配置会通过registry 传递给consumer 端。

image

provider 最好为每个服务都设置一个超时时间,因为provider能够准确的知道每个方法应该去执行多长时间。假如consumer 同时引入了多个服务,它并不需要关心每个服务超时时间的设置。

理论上,ReferenceConfig 所支持的所有的配置项都可以由 ConsumerConfig, ServiceConfig, ProviderConfig来配置(缺省使用的意思,如果没配,就用他们的)。

  1. 需要spring 3.2.16+,
    详见公告:“xmlns:p="http://www.springframework.org/schema/p”
  2. 引用bean默认是懒加载的,除非它被另外一个实例引用了。假如你需要提前初始化,这样配置【<dubbo:reference ... init="true" />】

Properties Configuration(属性文件配置)

对于不需要多注册中心,多协议的比较简单的应用,并且需要在spring container里共享配置的,我们可以使用dubbo.properties作为默认配置。

Dubbo将自动加载classpath根目录下的dubbo.properties文件,当让也可以使用JVM的参数-Ddubbo.properties.file=xxx.properties来指定地址。

Mapping Rules

属性文件配置跟xml的配置形式有一个简单的映射规则:即将xml的tag名和属性名组合起来,用【.】分隔即可。每个属性占一行。

  • dubbo.application.name=foo 相当于
  • dubbo.registry.address=10.20.153.10:9090 相当于

如果xml配置中有多余一个的tags,我们可以使用id来区分开。如果你不指定一个id,ti将被应用到所有的tags。

  • dubbo.protocol.rmi.port=1099 等同于
  • dubbo.registry.china.address=10.20.153.10:9090 等同于

一个典型的dubbo.properties配置如下:

dubbo.application.name=foo
dubbo.application.owner=bar
dubbo.registry.address=10.20.153.10:9090

Overrides and Priorities

image

优先级由高到低依次是:

  • JVM -D参数,当你部署或者启动应用时,使用它可以很容易的覆盖掉配置,又比如改变buddo协议的端口
  • XML, XML中的配置会覆盖dubbo.properties中的
  • Properties,一般作为默认值,仅仅当以上俩者都没配置时它才生效

API Configuration

所有的API属性在XML中都有与之对应的配置,比如,ApplicationConfig.setName("xxx") 等同于

Provider Side

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;
 
// Implementation
XxxService xxxService = new XxxServiceImpl();
 
// Application Info
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
 
// Registry Info
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
 
// Protocol
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
 
// NOTES: ServiceConfig holds the serversocket instance and keeps connections to registry, please cache it for performance.
 
// Exporting
ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // In case of memory leak, please cache.
service.setApplication(application);
service.setRegistry(registry); // Use setRegistries() for multi-registry case
service.setProtocol(protocol); // Use setProtocols() for multi-protocol case
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");
 
// Local export and register
service.export();

Consumer Side

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;
 
// Application Info
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
 
// Registry Info
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
 
// NOTES: ReferenceConfig holds the connections to registry and providers, please cache it for performance.
 
// Refer remote service
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // In case of memory leak, please cache.
reference.setApplication(application);
reference.setRegistry(registry); 
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
 
// Use xxxService just like a local bean
XxxService xxxService = reference.get(); // NOTES: Please cache this proxy instance.

Annotation Configuration

Provider Side

Service annotation for exporting

@Service
public class AnnotationServiceImpl implements AnnotationService {
    @Override
    public String sayHello(String name) {
        return "annotation: hello, " + name;
    }
}

Add application sharing configuration

# dubbo-provider.properties
dubbo.application.name=annotation-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

Spring scan path

@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static public class ProviderConfiguration {
       
}

Consumer Side

Reference annotation for reference

@Component("annotationAction")
public class AnnotationAction {

    @Reference
    private AnnotationService annotationService;
    
    public String doSayHello(String name) {
        return annotationService.sayHello(name);
    }
}

Add application sharing configuration

# dubbo-consumer.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000

Spring scan path

@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"})
static public class ConsumerConfiguration {

}

猜你喜欢

转载自www.cnblogs.com/po-shi/p/11244527.html