正确书写方式如下:
1 RequestContext ctx = RequestContext.getCurrentContext(); 2 ctx.setSendZuulResponse(false); 3 ctx.set("sendForwardFilter.ran", true); 4 ctx.setResponseBody("返回信息"); 5 return null;
书写过程中遇到了阻塞特此记录.
springBoot集成的route拦截器包含下面三种.
一.zuul的SimpleHostRoutingFilter主要用来转发不走eureka的proxy
通过设置 ctx.setSendZuulResponse(false);使不进行路由
org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter#shouldFilter
public boolean shouldFilter() { return RequestContext.getCurrentContext().getRouteHost() != null && RequestContext.getCurrentContext().sendZuulResponse(); }
二.zuul的SendForwardFilter处理路由规则中forward
的本地跳转
通过设置 ctx.set("sendForwardFilter.ran", true);使不进行路由
org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter#shouldFilter
public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); return ctx.containsKey("forward.to") && !ctx.getBoolean("sendForwardFilter.ran", false); }
三.zuul的RibbonRoutingFilter处理路由
通过设置 ctx.setSendZuulResponse(false);使不进行路由
org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter#shouldFilter
public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); return ctx.getRouteHost() == null && ctx.get("serviceId") != null && ctx.sendZuulResponse(); }