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。
(adsbygoogle = window.adsbygoogle || []).push({});