配置覆盖关系

以 timeout 为例,显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似:

  • 方法级优先,接口级次之,全局配置再次之。
  • 如果级别一样,则消费方优先,提供方次之。

其中,服务提供方配置,通过 URL 经由注册中心传递给消费方。

查找次序:

  1. reference method
<dubbo:reference interface="com.xxx.XxxService">

    <dubbo:method name="findXxx" timeout="1000">

</dubbo:reference

<dubbo:reference interface="com.xxx.XxxService">

    <dubbo:method name="findXxx" timeout="1000">

</dubbo:reference
  1. service method
<dubbo:service interface="com.xxx.XxxService">

      <dubbo:method name="findXxx" timeout="1000">

</dubbo:service
<dubbo:service interface="com.xxx.XxxService">

      <dubbo:method name="findXxx" timeout="1000">

</dubbo:service
  1. reference
<dubbo:reference interface="com.xxx.XxxService" timeout="3000"/>
<dubbo:reference interface="com.xxx.XxxService" timeout="3000"/>
  1. service
<dubbo:service interface="com.xxx.XxxService" timeout="4000">
<dubbo:service interface="com.xxx.XxxService" timeout="4000">
  1. consumer
<dubbo:consumer timeout="5000"/>
<dubbo:consumer timeout="5000"/>
  1. provider
<dubbo:provider timeout="6000" />
<dubbo:provider timeout="6000" />

建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置。

属性配置:

Dubbo 将自动加载 classpath 根目录下的 dubbo.properties,可以通过JVM启动参数 -Ddubbo.properties.file=xxx.properties 改变缺省配置位置。

映射规则:

将 XML 配置的标签名,加属性名,用点分隔,多个属性拆成多行

比如: dubbo.application.name=foo 等价于

比如: dubbo.registry.address=10.20.153.10:9090 等价于

如果 XML 有多行同名标签配置,可用 id 号区分,如果没有 id 号将对所有同名标签生效

比如: dubbo.protocol.rmi.port=1234 等价于

比如: dubbo.registry.china.address=10.20.153.10:9090 等价于

覆盖策略

  1. -D

java

-Ddubbo.protocol.port=20880

  1. XML

dubbo.xml

  1. dubbo.properties

dubbo.properties

dubbo.protocal.port=20880

JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口。

XML 次之,如果在 XML 中有配置,则 dubbo.properties 中的相应配置项无效。

Properties 最后,相当于缺省值,只有 XML 没有配置时,dubbo.properties 的相应配置项才会生效,通常用于共享公共配置,比如应用名。

API配置

服务提供者

import com.alibaba.dubbo.rpc.config.ApplicationConfig;
import com.alibaba.dubbo.rpc.config.RegistryConfig;
import com.alibaba.dubbo.rpc.config.ProviderConfig;
import com.alibaba.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;

// 服务实现

XxxService xxxService = new XxxServiceImpl();

// 当前应用配置

ApplicationConfig application = new ApplicationConfig();

application.setName("xxx");

// 连接注册中心配置

RegistryConfig registry = new RegistryConfig();

registry.setAddress("10.20.130.230:9090");

registry.setUsername("aaa");

registry.setPassword("bbb");

// 服务提供者协议配置

ProtocolConfig protocol = new ProtocolConfig();

protocol.setName("dubbo");

protocol.setPort(12345);

protocol.setThreads(200);

// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口

// 服务提供者暴露服务配置

ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例很重,封装

了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏

service.setApplication(application);

service.setRegistry(registry); // 多个注册中心可以用setRegistries()

service.setProtocol(protocol); // 多个协议可以用setProtocols()

service.setInterface(XxxService.class);

service.setRef(xxxService);

service.setVersion("1.0.0");

// 暴露及注册服务

service.export();

import com.alibaba.dubbo.rpc.config.ApplicationConfig;
import com.alibaba.dubbo.rpc.config.RegistryConfig;
import com.alibaba.dubbo.rpc.config.ProviderConfig;
import com.alibaba.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;

// 服务实现

XxxService xxxService = new XxxServiceImpl();

// 当前应用配置

ApplicationConfig application = new ApplicationConfig();

application.setName("xxx");

// 连接注册中心配置

RegistryConfig registry = new RegistryConfig();

registry.setAddress("10.20.130.230:9090");

registry.setUsername("aaa");

registry.setPassword("bbb");

// 服务提供者协议配置

ProtocolConfig protocol = new ProtocolConfig();

protocol.setName("dubbo");

protocol.setPort(12345);

protocol.setThreads(200);

// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口

// 服务提供者暴露服务配置

ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例很重,封装

了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏

service.setApplication(application);

service.setRegistry(registry); // 多个注册中心可以用setRegistries()

service.setProtocol(protocol); // 多个协议可以用setProtocols()

service.setInterface(XxxService.class);

service.setRef(xxxService);

service.setVersion("1.0.0");

// 暴露及注册服务

service.export();

消费者

import com.alibaba.dubbo.rpc.config.ApplicationConfig;

import com.alibaba.dubbo.rpc.config.RegistryConfig;

import com.alibaba.dubbo.rpc.config.ConsumerConfig;

import com.alibaba.dubbo.rpc.config.ReferenceConfig;

import com.xxx.XxxService;

// 当前应用配置

ApplicationConfig application = new ApplicationConfig();

application.setName("yyy");

// 连接注册中心配置

RegistryConfig registry = new RegistryConfig();

registry.setAddress("10.20.130.230:9090");

registry.setUsername("aaa");

registry.setPassword("bbb");

// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接

// 引用远程服务

ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重

,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏

reference.setApplication(application);

reference.setRegistry(registry); // 多个注册中心可以用setRegistries()

reference.setInterface(XxxService.class);

reference.setVersion("1.0.0");

// 和本地bean一样使用xxxService

XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,
import com.alibaba.dubbo.rpc.config.ApplicationConfig;

import com.alibaba.dubbo.rpc.config.RegistryConfig;

import com.alibaba.dubbo.rpc.config.ConsumerConfig;

import com.alibaba.dubbo.rpc.config.ReferenceConfig;

import com.xxx.XxxService;

// 当前应用配置

ApplicationConfig application = new ApplicationConfig();

application.setName("yyy");

// 连接注册中心配置

RegistryConfig registry = new RegistryConfig();

registry.setAddress("10.20.130.230:9090");

registry.setUsername("aaa");

registry.setPassword("bbb");

// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接

// 引用远程服务

ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重

,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏

reference.setApplication(application);

reference.setRegistry(registry); // 多个注册中心可以用setRegistries()

reference.setInterface(XxxService.class);

reference.setVersion("1.0.0");

// 和本地bean一样使用xxxService

XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,

集群容错模式

重试次数配置:

<dubbo:service retries="2" />

<dubbo:reference retries="2" />

<dubbo:reference>
    <dubbo:method name="findFoo" retries="2" />
</dubbo:reference>
<dubbo:service retries="2" />

<dubbo:reference retries="2" />

<dubbo:reference>
    <dubbo:method name="findFoo" retries="2" />
</dubbo:reference>

Failfast Cluster

快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。

Failsafe Cluster

失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。

Failback Cluster

失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。

Forking Cluster

并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks="2" 来设置最大并行数。

Broadcast Cluster

广播调用所有提供者,逐个调用,任意一台报错则报错 。通常用于通知所有提供者更新缓存或日志等本地资源信息。

按照以下示例在服务提供方和消费方配置集群模式

<dubbo:service cluster="failsafe" />

or

<dubbo:reference cluster="failsafe" />

<dubbo:service cluster="failsafe" />

or

<dubbo:reference cluster="failsafe" />

负载均衡策略

random LoadBalance

随机,按权重设置随机概率。

RoundRobin LoadBalance

轮循,按公约后的权重设置轮循比率。

存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。

LeastActive LoadBalance

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。

使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。

ConsistentHash LoadBalance

一致性 Hash,相同参数的请求总是发到同一提供者。

当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。

配置

服务端服务级别

<dubbo:service interface="..." loadbalance="roundrobin" />

客户端服务级别

<dubbo:reference interface="..." loadbalance="roundrobin" />

服务端方法级别

<dubbo:service interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>
<dubbo:service interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>

客户端方法级别

<dubbo:reference interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>
<dubbo:reference interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>

直连提供者

忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从注册中心获取列表。

  1. JVM Arugment

java -Dcom.xxx.XxxService=dubbo://10.20.153.10:20880

  1. Mapping File

${user.home}/dubbo-resolve.properties

com.xxx.XxxService=dubbo://10.20.153.10:20880

  1. Reference Config

通过XML配置

通过 -D 参数指定

java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890

通过文件映射

java -Ddubbo.resolve.file=xxx.properties

xxx.properties:

com.alibaba.xxx.XxxService=dubbo://localhost:20890

只订阅

为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行。

可以让服务提供者开发方,只订阅服务(开发的服务可能依赖其它服务),而不注册正在开发的服务,通过直连测试正在开发的服务。

禁用注册配置

<dubbo:registry address="10.20.153.10:9090" register="false" />
or
<dubbo:registry address="10.20.153.10:9090?register=false" />
<dubbo:registry address="10.20.153.10:9090" register="false" />
or
<dubbo:registry address="10.20.153.10:9090?register=false" />

只注册

如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部署,另一个注

册中心还没来得及部署,而两个注册中心的其它应用都需要依赖此服务。这个时候,可以让

服务提供者方只注册服务到另一注册中心,而不从另一注册中心订阅服务。

禁用订阅配置

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />

<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />

or

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />

<dubbo:registry id="qdRegistry" address="10.20.141.150:9090?subscribe=false" />

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />

<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />

or

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />

<dubbo:registry id="qdRegistry" address="10.20.141.150:9090?subscribe=false" />

多协议

不同服务不同协议

不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议

<?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://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springfr

    amework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp:

    //code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="world" />

    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" passwo

 rd="hello1234" />

    <!-- 多协议配置 -->

    <dubbo:protocol name="dubbo" port="20880" />

    <dubbo:protocol name="rmi" port="1099" />

    <!-- 使用dubbo协议暴露服务 -->

    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref=

        "helloService" protocol="dubbo" />

    <!-- 使用rmi协议暴露服务 -->

    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="

        demoService" protocol="rmi" />

</beans>

使用多个协议暴露服务

<?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://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springfr

    amework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp:

    //code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="world" />

    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" passwo

        rd="hello1234" />

    <!-- 多协议配置 -->

    <dubbo:protocol name="dubbo" port="20880" />

    <dubbo:protocol name="hessian" port="8080" />

    <!-- 使用多个协议暴露服务 -->

    <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" ve

        rsion="1.0.0"    protocol="dubbo,hessian" />

</beans>

多注册中心

比如:中文站有些服务来不及在青岛部署,只在杭州部署,而青岛的其它应用需要引用此服务,就可以将服务同时注册到两个注册中心。

<?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://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springfr

    amework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp:

    //code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="world" />

    <!-- 多注册中心配置 -->

    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />

    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false"/>

    <!-- 向多个注册中心注册 -->

    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref=

        "helloService" registry="hangzhouRegistry,qingdaoRegistry" />

</beans>

不同服务使用不同注册中心

比如:CRM 有些服务是专门为国际站设计的,有些服务是专门为中文站设计的。

<?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://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springfr

    amework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp:

    //code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="world" />

    <!-- 多注册中心配置 -->

    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />

    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />

    <!-- 向中文站注册中心注册 -->

    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref=

        "helloService" registry="chinaRegistry" />

    <!-- 向国际站注册中心注册 -->

    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="

    demoService" registry="intlRegistry" />

</beans>

多注册中心引用

比如:CRM 需同时调用中文站和国际站的 PC2 服务,PC2 在中文站和国际站均有部署,接口及版本号都一样,但连的数据库不一样。

<?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://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springfr

    amework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp:

    //code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="world" />

    <!-- 多注册中心配置 -->

    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />

    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />

    <!-- 引用中文站服务 -->

    <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloServ

        ice" version="1.0.0" registry="chinaRegistry" />

    <!-- 引用国际站站服务 -->

    <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloServi

        ce" version="1.0.0" registry="intlRegistry" />

</beans>

多版本

当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。

可以按照以下的步骤进行版本迁移:

\1. 在低压力时间段,先升级一半提供者为新版本

\2. 再将所有消费者升级为新版本

\3. 然后将剩下的一半提供者升级为新版本

老版本服务提供者配置:

新版本服务提供者配置:

老版本服务消费者配置:

新版本服务消费者配置:

如果不需要区分版本,可以按照以下的方式配置 :

结果缓存

结果缓存 ,用于加速热门数据的访问速度,Dubbo 提供声明式缓存,以减少用户加缓存的工作量 。

  • lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存。
  • threadlocal 当前线程缓存,比如一个页面渲染,用到很多 portal,每个 portal 都要去查用户信息,通过线程缓存,可以减少这种多余访问。
  • jcache 与 JSR107 集成,可以桥接各种缓存实现。
<dubbo:reference interface="com.foo.BarService" cache="lru" />

or

<dubbo:reference interface="com.foo.BarService">
    <dubbo:method name="findBar" cache="lru" />
</dubbo:reference>

异步调用

基于 NIO 的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小。

在 consumer.xml 中配置:

<dubbo:reference id="fooService" interface="com.alibaba.foo.FooService">
    <dubbo:method name="findFoo" async="true" />
</dubbo:reference>

<dubbo:reference id="barService" interface="com.alibaba.bar.BarService">
    <dubbo:method name="findBar" async="true" />
</dubbo:reference>

调用代码:

// 此调用会立即返回null
fooService.findFoo(fooId);
// 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
Future<Foo> fooFuture = RpcContext.getContext().getFuture();
// 此调用会立即返回null
barService.findBar(barId);
// 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
Future<Bar> barFuture = RpcContext.getContext().getFuture();
// 此时findFoo和findBar的请求同时在执行,客户端不需要启动多线程来支持并行,而是借助NIO的非阻塞完成
// 如果foo已返回,直接拿到返回值,否则线程wait住,等待foo返回后,线程会被notify唤醒
Foo foo = fooFuture.get();
// 同理等待bar返回
Bar bar = barFuture.get();
// 如果foo需要5秒返回,bar需要6秒返回,实际只需等6秒,即可获取到foo和bar,进行接下来的处理。

你也可以设置是否等待消息发出:

sent="true" 等待消息发出,消息发送失败将抛出异常。

sent="false" 不等待消息发出,将消息放入 IO 队列,即刻返回。

如果你只是想异步,完全忽略返回值,可以配置 return="false" ,以减少 Future 对象的创建和管理成本:

延迟暴露

如果你的服务需要预热时间,比如初始化缓存,等待相关资源就位等,可以使用 delay 进行延迟暴露。

延迟5秒暴露服务

延迟到spring初始化完成后,在暴露服务

并发控制

限制 com.foo.BarService 的每个方法,服务器端并发执行(或占用线程池线程数)不能超过10 个:

限制 com.foo.BarService 的 sayHello 方法,服务器端并发执行(或占用线程池线程数)不能超过 10 个:

<dubbo:service interface="com.foo.BarService">
    <dubbo:method name="sayHello" executes="10" />
</dubbo:service>

限制 com.foo.BarService 的每个方法,每客户端并发执行(或占用连接的请求数)不能超过10 个:

<dubbo:service interface="com.foo.BarService" actives="10" />
或
<dubbo:reference interface="com.foo.BarService" actives="10" />

限制 com.foo.BarService 的 sayHello 方法,每客户端并发执行(或占用连接的请求数)不能超过 10 个:

<dubbo:service interface="com.foo.BarService">
    <dubbo:method name="sayHello" actives="10" />
</dubbo:service>

or

<dubbo:reference interface="com.foo.BarService">
    <dubbo:method name="sayHello" actives="10" />
</dubbo:service>

Load Balance 均衡

配置服务的客户端的 loadbalance 属性为 leastactive ,此 Loadbalance 会调用并发数最小的 Provider(Consumer端并发数)。

<dubbo:reference interface="com.foo.BarService" loadbalance="leastactive" />

or

<dubbo:service interface="com.foo.BarService" loadbalance="leastactive" />

粘滞连接

粘滞连接用于有状态服务,尽可能让客户端总是向同一提供者发起调用,除非该提供者挂了,再连另一台。

粘滞连接将自动开启延迟连接,以减少长连接数。

Multicast注册中心

Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。

\1. 提供方启动时广播自己的地址

\2. 消费方启动时广播订阅请求

\3. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了 unicast=false ,则广播给订阅者

\4. 消费方收到提供方地址时,连接该地址进行 RPC 调用

组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段: 224.0.0.0 - 239.255.255.255

配置

<dubbo:registry address="multicast://224.5.6.7:1234" />

or

<dubbo:registry protocol="multicast" address="224.5.6.7:1234" />

为了减少广播量,Dubbo 缺省使用单播发送提供者地址信息给消费者,如果一个机器上同时启了多个消费者进程,消费者需声明 unicast=false ,否则只会有一个消费者能收到消息:

<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />

or

<dubbo:registry protocol="multicast" address="224.5.6.7:1234”>     
    <dubbo:parameter key="unicast" value="false" /> 
</dubbo:registry>

zookeeper 注册中心

支持以下功能:

  • 当提供者出现断电等异常停机时,注册中心能自动删除提供者信息
  • 当注册中心重启时,能自动恢复注册数据,以及订阅请求
  • 当会话过期时,能自动恢复注册数据,以及订阅请求当设置 时,记录失败注册和订阅请求,后台定时重试
  • 可通过 设置 zookeeper 登录信息
  • 可通过 设置 zookeeper 的根节点,不设置将使用无根树
  • 支持 * 号通配符 ,可订阅服务的所有分组和所有版本的提供者

使用 zkclient 客户端

配置

<dubbo:registry ... client="zkclient" />

or

dubbo.registry.client=zkclient

or

zookeeper://10.20.153.10:2181?client=zkclient

使用 curator 客户端

配置

<dubbo:registry ... client="curator" />

or

dubbo.registry.client=curator

or

zookeeper://10.20.153.10:2181?client=curator

Zookeeper单机配置:

<dubbo:registry address="zookeeper://10.20.153.10:2181" />

or

<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />

Zookeeper集群配置

<dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.
153.12:2181" />

or

<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.2 0.153.12:2181" />

同一Zookeeper,分成多组注册中心:

<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" gr oup="china" />

<dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" gro up="intl" />

推荐用法

在Provider 上尽量多配置 Consumer 端属性原因如下:

  • 作服务的提供者,比服务使用方更清楚服务性能参数,如调用的超时时间,合理的重试次数,等等
  • 在 Provider 配置后,Consumer 不配置则会使用 Provider 的配置值,即 Provider 配置可以作为 Consumer 的缺省值 。否则,Consumer 会使用 Consumer 端的全局设置,这对于 Provider 不可控的,并且往往是不合理的

Provider 上尽量多配置 Consumer 端的属性,让 Provider 实现者一开始就思考 Provider 服务特点、服务质量的问题。

<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="hel loService"     timeout="300" retry="2" loadbalance="random" actives="0" /> 

<dubbo:service interface="com.alibaba.hello.api.WorldService" version="1.0.0" ref="hel loService"     timeout="300" retry="2" loadbalance="random" actives="0" >     
    <dubbo:method name="findAllPerson" timeout="10000" retries="9" loadbalance="leasta ctive" actives="5" /> 
<dubbo:service/>