Detailed description of configuration mode
At present, dubbo provides four configurations, they are:
- XML Configuration
- Properties Configurtion
- API Configuration
- Annotation Configuration
Now let's introduce them in detail.
XML Configuration
I wrote an introductory demo about xml configuration before, so let's take a look at it first.
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>
All tags support custom parameters, so we can meet the special configuration requirements of different extension points.
For example:
<dubbo:protocol name="jms"> <dubbo:parameter key="queue" value="your_queue" /> </dubbo:protocol> or <dubbo:protocol name="jms" p:queue="your_queue" />
For the moment, let's not talk about the specific meanings of these tags. There is a general image available.
The relations between configuration tags
Label name | Purpose (function) | introduce |
---|---|---|
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
Take timeout as an example (retries, load balance, actives are also applicable). Priorities from high to low are as follows:
- Method Level, Interface Level, Default/Global Level
- At the same level, consumer s have a higher priority than provider s
The configuration of the provider side is passed to the consumer side through registry.
Provider is best to set a timeout for each service, because the provider knows exactly how long each method should be executed. If consumer introduces multiple services at the same time, it does not need to care about the setting of timeout for each service.
In theory, all configuration items supported by Reference Config can be configured by ConsumerConfig, Service Config, Provider Config (default meaning, if not, use them).
- spring 3.2.16+,
See bulletin: "xmlns: p=" http://www.spring framework.org/schema/p" - Reference bean s are lazy by default unless they are referenced by another instance. If you need to initialize ahead of time, configure this configuration [dubbo: reference... init= "true" />]
Properties Configuration
We can use dubbo.properties as the default configuration for simple applications that do not require multiple registries, multiple protocols, and need to share configurations in spring container s.
Dubbo will automatically load the dubbo.properties file in the classpath root directory, when the JVM parameter - Ddubbo. properties. file = xxxx. properties can also be used to specify the address.
Mapping Rules
There is a simple mapping rule between attribute file configuration and xml configuration: the tag name and attribute name of xml are combined and separated by [.]. Each property occupies one line.
- dubbo.application.name=foo equals
- dubbo.registry.address=10.20.153.10:9090 equals
If there is an extra tags in the xml configuration, we can use ID to distinguish them. If you do not specify an id, ti will be applied to all tags.
- dubbo.protocol.rmi.port=1099 is equivalent to
- dubbo.registry.china.address=10.20.153.10:9090 is equivalent to
A typical dubbo.properties configuration is as follows:
dubbo.application.name=foo dubbo.application.owner=bar dubbo.registry.address=10.20.153.10:9090
Overrides and Priorities
Priorities from high to low are:
- JVM-D parameter, when you deploy or start an application, it can easily override the configuration, such as changing the port of buddo protocol.
- XML. Configuration in XML overrides dubbo.properties
- Properties, which is generally the default value, takes effect only when neither of them is configured
API Configuration
All API attributes have their corresponding configurations in XML, for example, Application Config. setName ("xxx") is equivalent to
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 { }