前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springcloud之Zuul的RibbonRoutingFilter

Springcloud之Zuul的RibbonRoutingFilter

作者头像
克虏伯
发布2019-10-05 13:52:25
1.5K0
发布2019-10-05 13:52:25
举报
文章被收录于专栏:软件开发-青出于蓝

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

    图1是RibbonRoutingFilter的继承图

                     图1 类继承图

    RibbonRoutingFilter是个roue类型的ZuulFilter,如下List-1所示,它的filterOrder是10; 在run方法中首先构造commandContext,之后调用内部方法forward。

List-1

代码语言:javascript
复制
@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构建RibbonCommand,之后调用RibbonCommand的execute(),这里的RibbonCommandFactory和RibbonCommand都是接口,具体实现有三个。

List-2

代码语言:javascript
复制
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接口

代码语言:javascript
复制
public interface RibbonCommand extends HystrixExecutable<ClientHttpResponse> {

}

public interface RibbonCommandFactory<T extends RibbonCommand> {

	T create(RibbonCommandContext context);

}

    RibbonCommandFactory的三个实现类如下图2

                                                                                          图2

   HttpClientRibbonCommandFactory用于构建HttpClientRibbonCommand, OkHttpRibbonCommandFactory用于构建OkHttpRibbonCommand, RestClientRibbonCommandFactory用于构建RestClientRibbonCommand。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档