routing zuul_Springcloud之Zuul的RibbonRoutingFilter

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

图1是RibbonRoutingFilter的继承图

图1 类继承图

RibbonRoutingFilter是个roue类型的ZuulFilter,如下List-1所示,它的filterOrder是10; 在run方法中首先构造commandContext,之后调用内部方法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构建RibbonCommand,之后调用RibbonCommand的execute(),这里的RibbonCommandFactory和RibbonCommand都是接口,具体实现有三个。

List-2

protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {

Map 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-3RibbonCommandFactory和RibbonCommand接口

public interface RibbonCommand extends HystrixExecutable {

}

public interface RibbonCommandFactory {

T create(RibbonCommandContext context);

}

RibbonCommandFactory的三个实现类如下图2

图2

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值