Dubbo的常用配置讲解
配置
provider配置xml
<?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="service-provider2" owner="xiaoming" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" id="r1" timeout="10000"/>
<!-- 用dubbo协议在20882端口暴露服务
<dubbo:protocol name="dubbo" port="20882" /> -->
<!-- 用dubbo协议在20883端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20883" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.lagou.service.HelloService" ref="helloService" />
<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.lagou.service.impl.HelloServiceImpl" />
</beans>
consumer配置xml
<?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="dubbo-consumer" owner="lisi" >
<dubbo:parameter key="qos.enable" value="true" ></dubbo:parameter>
<dubbo:parameter key="qos.port" value="33333"></dubbo:parameter>
<dubbo:parameter key="qos.accept.foreign.ip" value="true" ></dubbo:parameter>
</dubbo:application>
<!-- -->
<dubbo:consumer timeout="2000" check="false">
</dubbo:consumer>
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="106.75.144.210:2181" timeout="10000" protocol="zookeeper" id="myRegistry">
</dubbo:registry>
<dubbo:protocol name="dubbo"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="service" interface="com.my.study.service.Service" registry="myRegistry" timeout="4000" retries="2" version="0.0.0"/>
<dubbo:annotation package="com.my.study"></dubbo:annotation>
</beans>
配置讲解xml形式
1.dubbo:application 当前应用的信息
1. name: 当前应用程序的名称,在dubbo-admin中我们也可以看到,这个代表这个应用名称。我们 在真正时是时也会根据这个参数来进行聚合应用请求。
2. owner: 当前应用程序的负责人,可以通过这个负责人找到其相关的应用列表,用于快速定位到责 任人。
3. qosEnable : 是否启动QoS 默认true。
4. qosPort : 启动QoS绑定的端口 默认22222 5. qosAcceptForeignIp: 是否允许远程访问 默认是false。
2.dubbo:registry 注册中心的信息
1. id : 当当前服务中provider或者consumer中存在多个注册中心时,则使用需要增加该配置。在一 些公司,会通过业务线的不同选择不同的注册中心,所以一般都会配置该值。
2. address : 当前注册中心的访问地址。
3. protocol : 当前注册中心所使用的协议是什么。也可以直接在 address 中写入,比如使用 zookeeper,就可以写成 zookeeper://xx.xx.xx.xx:2181。
4. timeout : 当与注册中心不再同一个机房时,大多会把该参数延长。
3.dubbo:protocol 数据传输使用的协议
1. id : 在大公司,可能因为各个部门技术栈不同,所以可能会选择使用不同的协议进行交互。这里 在多个协议使用时,需要指定。
2. name : 指定协议名称。默认使用 dubbo 。
4.dubbo:service 提供者能提供的服务
1. interface : 指定当前需要进行对外暴露的接口是什么。
2. ref : 具体实现对象的引用,一般我们在生产级别都是使用Spring去进行Bean托管的,所以这里面 一般也指的是Spring中的BeanId。
3. version : 对外暴露的版本号。不同的版本号,消费者在消费的时候只会根据固定的版本号进行消费。
4. executes: 用于在提供者做配置,来确保最大的并行度。
1. 可能导致集群功能无法充分利用或者堵塞 。
2. 但是也可以启动部分对应用的保护功能 。
3. 可以不做配置,结合后面的熔断限流使用。
5.dubbo:reference 消费者配置
1. mock: 用于在方法调用出现错误时,当做服务降级来统一对外返回结果,后面我们也会对这个方 法做更多的介绍。
2. timeout: 用于指定当前方法或者接口中所有方法的超时时间。我们一般都会根据提供者的时长来 具体规定。比如我们在进行第三方服务依赖时可能会对接口的时长做放宽,防止第三方服务不稳定 导致服务受损。
3. check: 用于在启动时,检查生产者是否有该服务。我们一般都会将这个值设置为false,不让其进 行检查。因为如果出现模块之间循环引用的话,那么则可能会出现相互依赖,都进行check的话, 那么这两个服务永远也启动不起来。
4. retries: 用于指定当前服务在执行时出现错误或者超时时的重试机制。
1. 注意提供者是否有幂等,否则可能出现数据一致性问题。
2. 注意提供者是否有类似缓存机制,如出现大面积错误时,可能因为不停重试导致雪崩。5. id : 指定该Bean在注册到Spring中的id。
6. interface: 服务接口名 3. version : 指定当前服务版本,与服务提供者的版本一致。
7. registry : 指定所具体使用的注册中心地址。这里面也就是使用上面在 dubbo:registry 中所声 明的id。
8.cache:声明式缓存。
6 dubbo:method 指定具体方法级别在进行RPC操作时候的配置
1. name : 指定方法名称,用于对这个方法名称的RPC调用进行特殊配置。
2. async: 是否异步 默认false
配置讲解注解+properties形式
consumer配置
配置和启动类
public class ConsumerMain {
// 启动方法
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
// 获取消费者组件
ConsumerComponent service = context.getBean(ConsumerComponent.class);
Executor executor = (Executor) context.getBean("taskExecutor");
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 35; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
String result = service.methodA();
}
});
executor.execute(new Runnable() {
@Override
public void run() {
String result = service.methodB();
}
});
executor.execute(new Runnable() {
@Override
public void run() {
String result = service.methodC();
}
});
}
}
}
// 配置静态类
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(basePackages = "com.my.study")
@EnableDubbo
static class ConsumerConfiguration{
}
}
properties文件
dubbo.application.name=dubbo-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20889
dubbo.application.owner=zhangsan
dubbo.registry.address=zookeeper://106.75.144.210:2181
dubbo.application.qosEnable=false
dubbo.application.qosPort=33334
dubbo.application.qosAcceptForeignIp=false
provider配置
provider配置application
dubbo.application.name=dubbo-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20889
dubbo.application.owner=zhangsan
dubbo.registry.address=zookeeper://106.75.144.210:2181
dubbo.application.qosEnable=false
dubbo.application.qosPort=33334
dubbo.application.qosAcceptForeignIp=false
provider配置和启动类
public class DubboPureMain {
// 启动方法
public static void main(String[] args) throws Exception{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
// 配置静态类
@Configuration
@EnableDubbo(scanBasePackages = "com.my.study.service")
@PropertySource("classpath:/dubbo-provider.properties")
static class ProviderConfiguration{
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://106.75.144.210:2181?timeout=10000");
//registryConfig.setTimeout(10000);
return registryConfig;
}
}
}}
1.@EnableDubbo(scanBasePackages =”com.my.study.service”)
提供者需要对外提供的方法直接通过包扫描的方式去进行注册到ioc中。
2.@Configuration
代表当前是个配置类
3.@PropertySource(“classpath:/dubbo-provider.properties”)
导入外部的文件并解析
4.@ComponentScan(basePackages =”com.my.study”)
将需要的类包扫描的方式去进行注册到ioc中。
总结
本文介绍的只是部分常用的配置,更多详细配置可以参考dubbo官方文档,配置模块的文档去了解学习。