dubbo 多注册中心的配置和使用

Dubbo 支持同一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心上去,甚至可以同时引用注册在不同注册中心上的同名服务。另外,注册中心是支持自定义扩展的。

本文介绍 Dubbo 多注册中心的配置和使用。

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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


    <dubbo:application name="demo"/>


    <dubbo:registry id="zk1" protocol="zookeeper" address="127.X.X.11:2181,127.X.X.12:2181,127.X.X.13:2181"/>
    <dubbo:registry id="zk2" protocol="zookeeper"
                    address="zookeeper://127.X.X.21:2181?backup=127.X.X.22:2181,127.X.X.23:2181"/>


    <dubbo:protocol name="dubbo" port="21992" threads="200"/>

    <dubbo:service protocol="dubbo" interface="com.test.api.TestService"
                   ref="testService" registry="zk1,zk2" timeout="2000"/>

    <bean id="testService" class="com.test.api.TestServiceImpl"/>
</beans>

Properties 方式配置

dubbo.scan.basePackages  = com.test.api
dubbo.application.id = demo
dubbo.application.name = demo
dubbo.application.qos-enable=false
dubbo.application.qos-port=10882
dubbo.application.logger = slf4j
dubbo.application.dump.directory=logs
dubbo.registries.zk1.address=127.X.X.11:2181,127.X.X.12:2181,127.X.X.13:2181
dubbo.registries.zk2.address=zookeeper://127.X.X.21:2181?backup=127.X.X.22:2181,127.X.X.23:2181
dubbo.registry.client = curator
dubbo.registry.wait = 10000
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1
dubbo.provider.threadpool = fixed
dubbo.provider.threads = 800
dubbo.provider.accepts = 1000
dubbo.provider.connections = 10
dubbo.metadata-report.cycle-report=false
dubbo.provider.filter=logTimeout

demo.version = 1.0.0

提供者配置使用

同时将服务注册到多个注册中心:

import org.apache.dubbo.config.annotation.DubboService;

@DubboService(version = "${demo.version}", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = {"zk1","zk2"}, delay = 10000)
public class TestServiceImpl implements TestService {

}

消费者配置使用

客户端调用不同注册中心的同一接口:

import org.apache.dubbo.config.annotation.DubboReference;

@DubboReference(version = "${demo.version}", application = "${dubbo.application.id}", registry = {"zk1","zk2"}, timeout = 100000)
private TestServiceImpl testService;

Yaml 方式配置

dubbo:
  config:
    multiple: true
  protocols:
    thpro:
      name: dubbo
      port: -1
    zwpro:
      name: dubbo
      port: -1
  provider:
    accepts: 1500
    connections: 10
    threadpool: fixed
    threads: 1000
  registries:
    zk1:
      address: 127.X.X.11:2181,127.X.X.12:2181,127.X.X.13:2181
      client: zkclient
    zk2:
      address: zookeeper://127.X.X.21:2181?backup=127.X.X.22:2181,127.X.X.23:2181
      client: zkclient
    zk3:
      address: zookeeper://127.X.X.31:2181?backup=127.X.X.32:2181,127.X.X.33:2181
      client: zkclient
  scan:
    basePackages: com.test.api
      
demo:
  version: 1.0.0      

配置使用

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;

@Slf4j
@Service(version = "${demo.version}", application = "zw", protocol = "zwpro", registry = {"zk1", "zk2"},
        filter = "tracing")
public class TestServiceImpl implements TestService {

    @Reference(application = "th", registry = "zk1", connections = 10, retries = 1)
    private HbaseDataServer hbaseDataServer;

    @Resource
    private DroolsChecker droolsChecker;
}