配置原则
官网:http://dubbo.apache.org/ 官方文档写的更加清晰,
dubbo推荐在Provider上尽量多配置Consumer端属性
1、作服务的提供者,比服务使用方更清楚服务性能参数,如调用的超时时间,合理的重试次数,等等
2、在Provider配置后,Consumer不配置则会使用Provider的配置值,即Provider配置可以作为Consumer的缺省值。否则,Consumer会使用Consumer端的全局设置,这对于Provider不可控的,并且往往是不合理的
配置的覆盖规则:
1、方法级配置别优于接口级别,即小Scope优先 ,个别配置需要以官方文档为准,如provide的retries全局配置覆盖具体配置
2、Consumer端配置 优于 Provider配置 优于 全局配置
3、 最后是Dubbo Hard Code的配置值(见配置文档)
重试次数
失败自动切换,当出现失败,重试其它服务器,但重试会带来更长延迟。可通过 retries="2" 来设置重试次数(不含第一次)
重试次数配置如下:
<dubbo:service retries="2" />
或
<dubbo:reference retries="2" />
或
<dubbo:reference>
<dubbo:method name="findFoo" retries="2" />
</dubbo:reference>
超时时间
由于网络或服务端不可靠,会导致调用出现一种不确定的中间状态(超时)。为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间
<!-- 消费者 -->
<!--全局超时配置 -->
<dubbo:consumer timeout="5000" />
<!--指定接口以及特定方法超时配置 -->
<dubbo:reference interface="com.foo.BarService" timeout="2000">
<dubbo:method name="sayHello" timeout="3000" />
</dubbo:reference>
<!--全局超时配置 -->
<dubbo:provider timeout="5000" />
<!--指定接口以及特定方法超时配置-->
<dubbo:provider interface="com.foo.BarService" timeout="2000">
<dubbo:method name="sayHello" timeout="3000" />
</dubbo:provider>
版本号
当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。
可以按照以下的步骤进行版本迁移:在低压力时间段,先升级一半提供者为新版本,再将所有消费者升级为新版本,然后将剩下的一半提供者升级为新版本
老版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="1.0.0" />
新版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="2.0.0" />
老版本服务消费者配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
新版本服务消费者配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />
如果不需要区分版本,可以按照以下的方式配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />
dubbo-provide.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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="false">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="marketing-equity">
<!-- 线程池满时自动dump,路径与jvm参数配置一致 -->
<dubbo:parameter key="dump.directory" value="/data/dataLogs/dump" />
</dubbo:application>
<!-- 端口 -->
<dubbo:protocol name="dubbo" port="${dubbo.port}" threads="500" dispatcher="message"/>
<!-- 使用zookeeper广播注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.zookeeper.address}"/>
<!-- 监控 -->
<dubbo:monitor protocol="registry" />
<!-- retries重试次数,timeout超时时间-->
<dubbo:service interface="com.yaoyagang.TestService"
ref="testServiceImpl"
retries="0"
timeout="1000"/>
</beans>
dubbo-consume.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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="false">
<dubbo:consumer check="false"/>
<!-- 引入服务提供者-->
<dubbo:reference id="queryServiceImpl"
interface="com.yaoyagang.QueryService"
retries="0"
timeout="1000"/>
</beans>