Springcloud中Zuul的RibbonRoutingFilter有什么作用

本篇內(nèi)容介紹了“Springcloud中Zuul的RibbonRoutingFilter有什么作用”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站,網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:13518219792

    spring-cloud-netflix-zuul的版本是2.1.2.release.

    圖1是RibbonRoutingFilter的繼承圖

            Springcloud中Zuul的RibbonRoutingFilter有什么作用

          圖1 類繼承圖

    RibbonRoutingFilter是個(gè)roue類型的ZuulFilter,如下List-1所示,它的filterOrder是10; 在run方法中首先構(gòu)造commandContext,之后調(diào)用內(nèi)部方法forward。

    List-1

@Override
public String filterType() {
    return ROUTE_TYPE;
}

@Override
public int filterOrder() {
    return RIBBON_ROUTING_FILTER_ORDER;
}

@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    return (ctx.getRouteHost() == null && ctx.get(SERVICE_ID_KEY) != null
            && ctx.sendZuulResponse());
}

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    this.helper.addIgnoredHeaders();
    try {
        RibbonCommandContext commandContext = buildCommandContext(context);
        ClientHttpResponse response = forward(commandContext);
        setResponse(response);
        return response;
    }
    catch (ZuulException ex) {
        throw new ZuulRuntimeException(ex);
    }
    catch (Exception ex) {
        throw new ZuulRuntimeException(ex);
    }
}

    如下List-2所示,首先用ribbonCommandFactory構(gòu)建RibbonCommand,之后調(diào)用RibbonCommand的execute(),這里的RibbonCommandFactory和RibbonCommand都是接口,具體實(shí)現(xiàn)有三個(gè)。

    List-2

protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
    Map<String, Object> info = this.helper.debug(context.getMethod(),
            context.getUri(), context.getHeaders(), context.getParams(),
            context.getRequestEntity());

    RibbonCommand command = this.ribbonCommandFactory.create(context);
    try {
        ClientHttpResponse response = command.execute();
        this.helper.appendDebug(info, response.getRawStatusCode(),
                response.getHeaders());
        return response;
    }
    catch (HystrixRuntimeException ex) {
        return handleException(info, ex);
    }
}

    List-3 RibbonCommandFactory和RibbonCommand接口

public interface RibbonCommand extends HystrixExecutable<ClientHttpResponse> {

}

public interface RibbonCommandFactory<T extends RibbonCommand> {

	T create(RibbonCommandContext context);

}

    RibbonCommandFactory的三個(gè)實(shí)現(xiàn)類如下圖2

Springcloud中Zuul的RibbonRoutingFilter有什么作用

                                                                                          圖2

   HttpClientRibbonCommandFactory用于構(gòu)建HttpClientRibbonCommand, OkHttpRibbonCommandFactory用于構(gòu)建OkHttpRibbonCommand, RestClientRibbonCommandFactory用于構(gòu)建RestClientRibbonCommand。

“Springcloud中Zuul的RibbonRoutingFilter有什么作用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章標(biāo)題:Springcloud中Zuul的RibbonRoutingFilter有什么作用
文章URL:http://muchs.cn/article4/ighhoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)手機(jī)網(wǎng)站建設(shè)、App設(shè)計(jì)、響應(yīng)式網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)