Dubbo 標(biāo)簽路由

開篇

  • 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&timestamp=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)捅暴。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市咧纠,隨后出現(xiàn)的幾起案子蓬痒,更是在濱河造成了極大的恐慌,老刑警劉巖漆羔,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梧奢,死亡現(xiàn)場離奇詭異,居然都是意外死亡钧椰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門符欠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嫡霞,“玉大人,你說我怎么就攤上這事希柿≌锘Γ” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵曾撤,是天一觀的道長端姚。 經(jīng)常有香客問我,道長挤悉,這世上最難降的妖魔是什么渐裸? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮装悲,結(jié)果婚禮上昏鹃,老公的妹妹穿的比我還像新娘。我一直安慰自己诀诊,他們只是感情好洞渤,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著属瓣,像睡著了一般载迄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上抡蛙,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天护昧,我揣著相機與錄音,去河邊找鬼粗截。 笑死捏卓,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播怠晴,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遥金,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蒜田?” 一聲冷哼從身側(cè)響起稿械,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冲粤,沒想到半個月后美莫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡梯捕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年厢呵,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片傀顾。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡襟铭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出短曾,到底是詐尸還是另有隱情寒砖,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布嫉拐,位于F島的核電站哩都,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏婉徘。R本人自食惡果不足惜漠嵌,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盖呼。 院中可真熱鬧献雅,春花似錦、人聲如沸塌计。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽锌仅。三九已至章钾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間热芹,已是汗流浹背贱傀。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留伊脓,地道東北人府寒。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓魁衙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親株搔。 傳聞我的和親對象是個殘疾皇子剖淀,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容