本篇内容介绍了“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<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的三个实现类如下图2
图2
HttpClientRibbonCommandFactory用于构建HttpClientRibbonCommand, OkHttpRibbonCommandFactory用于构建OkHttpRibbonCommand, RestClientRibbonCommandFactory用于构建RestClientRibbonCommand。
“Springcloud中Zuul的RibbonRoutingFilter有什么作用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/2518341/blog/3113599