開篇
Dubbo路由規(guī)則在發(fā)起一次RPC調(diào)用前起到過濾目標(biāo)服務(wù)器地址的作用偶翅,過濾后的地址列表,將作為消費端最終發(fā)起RPC調(diào)用的備選地址碉渡。
目前支持的路由包括:
條件路由聚谁。支持以服務(wù)或Consumer應(yīng)用為粒度配置路由規(guī)則。
標(biāo)簽路由滞诺。以Provider應(yīng)用為粒度配置路由規(guī)則形导。這篇文章的分析是基于Dubbo-2.6.6版本的环疼,不同的版本實現(xiàn)方法會有些不一樣,官方的鏈接路由規(guī)則朵耕。
Dubbo標(biāo)簽路由
標(biāo)簽路由通過將某一個或多個服務(wù)的提供者劃分到同一個分組炫隶,約束流量只在指定分組中流轉(zhuǎn),從而實現(xiàn)流量隔離的目的阎曹,可以作為藍綠發(fā)布伪阶、灰度發(fā)布等場景的能力基礎(chǔ)。
標(biāo)簽主要是指對Provider端應(yīng)用實例的分組处嫌,目前有兩種方式可以完成實例分組栅贴,分別是動態(tài)規(guī)則打標(biāo)和靜態(tài)規(guī)則打標(biāo),其中動態(tài)規(guī)則相較于靜態(tài)規(guī)則優(yōu)先級更高熏迹,而當(dāng)兩種規(guī)則同時存在且出現(xiàn)沖突時檐薯,將以動態(tài)規(guī)則為準(zhǔn)。
請求標(biāo)簽的作用域為每一次 invocation注暗,使用 attachment 來傳遞請求標(biāo)簽坛缕,注意保存在 attachment 中的值將會在一次完整的遠程調(diào)用中持續(xù)傳遞,得益于這樣的特性捆昏,我們只需要在起始調(diào)用時祷膳,通過一行代碼的設(shè)置,達到標(biāo)簽的持續(xù)傳遞屡立。
降級約定
consumer攜帶request.tag=tag1 時優(yōu)先選擇 標(biāo)記了tag=tag1 的 provider直晨。若集群中不存在與請求標(biāo)記對應(yīng)的服務(wù),默認將降級請求 tag為空的provider膨俐;如果要改變這種默認行為勇皇,即找不到匹配tag1的provider返回異常,需設(shè)置request.tag.force=true焚刺。
comsumer側(cè)request.tag未設(shè)置時敛摘,只會匹配tag為空的provider。即使集群中存在可用的服務(wù)乳愉,若tag不匹配也就無法調(diào)用兄淫,這與約定1不同,攜帶標(biāo)簽的請求可以降級訪問到無標(biāo)簽的服務(wù)蔓姚,但不攜帶標(biāo)簽/攜帶其他種類標(biāo)簽的請求永遠無法訪問到其他標(biāo)簽的服務(wù)捕虽。
Dubbo標(biāo)簽路由選擇過程
public class TagRouter extends AbstractRouter {
@Override
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
// filter
List<Invoker<T>> result = new ArrayList<Invoker<T>>();
// Dynamic param
String tag = RpcContext.getContext().getAttachment(Constants.TAG_KEY);
// 處理consumer攜帶tag的情況
if (!StringUtils.isEmpty(tag)) {
// 優(yōu)先選擇攜帶標(biāo)簽的provider
for (Invoker<T> invoker : invokers) {
if (tag.equals(invoker.getUrl().getParameter(Constants.TAG_KEY))) {
result.add(invoker);
}
}
}
// 如果未指定標(biāo)簽tag或者攜帶了標(biāo)簽但是未找到匹配的provider的情況
if (result.isEmpty()) {
// 未強制指定FORCE_USE_TAG的邏輯
String forceTag = RpcContext.getContext().getAttachment(Constants.FORCE_USE_TAG);
if (StringUtils.isEmpty(forceTag) || "false".equals(forceTag)) {
for (Invoker<T> invoker : invokers) {
// 獲取沒有攜帶標(biāo)簽的provider對象
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) {
result.add(invoker);
}
}
}
}
return result;
}
}
攜帶標(biāo)簽的請求優(yōu)先訪問帶標(biāo)簽的provider,再不存在攜帶標(biāo)簽的情況下降級訪問到無標(biāo)簽的provider坡脐。
針對不攜帶標(biāo)簽的請求只能訪問無標(biāo)簽的provider泄私。
Dubbo標(biāo)簽路由的舉例
Provider
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
public class Provider {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" tag="zzzz"/>
</beans>
consumer
public class Consumer {
public static void main(String[] args) {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
// 攜帶tag路由
RpcContext.getContext().setAttachment("dubbo.tag", "zzzz");
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
dubbo://192.168.1.5:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true
&application=demo-provider
&bean.name=com.alibaba.dubbo.demo.DemoService&default.dubbo.tag=xxx&dubbo=2.0.2
&dubbo.tag=zzzzzz&generic=false
&interface=com.alibaba.dubbo.demo.DemoService
&methods=sayHello&pid=82561&side=provider×tamp=1577466134212
Dubbo標(biāo)簽路由的坑
@Activate(group = Constants.CONSUMER, order = -10000)
public class ConsumerContextFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
RpcContext.getContext()
.setInvoker(invoker)
.setInvocation(invocation)
.setLocalAddress(NetUtils.getLocalHost(), 0)
.setRemoteAddress(invoker.getUrl().getHost(),
invoker.getUrl().getPort());
if (invocation instanceof RpcInvocation) {
((RpcInvocation) invocation).setInvoker(invoker);
}
try {
RpcResult result = (RpcResult) invoker.invoke(invocation);
RpcContext.getServerContext().setAttachments(result.getAttachments());
return result;
} finally {
// 清空attachments
RpcContext.getContext().clearAttachments();
}
}
}
- dubbo的consumer側(cè)的Filter對象ConsumerContextFilter每次請求后都會清空Attachments,導(dǎo)致再次發(fā)起請求就無法找到tag,所以需要在整個生命周期內(nèi)保存tag晌端。一般通過線程的ThreadLocal進行實現(xiàn)捅暴。