覆盖 zuul 错误过滤器导致转发错误

Overriding zuul error filter results in Forwarding error

我正在尝试覆盖 zuul 代理的默认 senderorfilter,这样当我的网关后面的服务 crashed/is 关闭时,我可以提供更好的响应。这是我从

中的答案中提取的代码
public class RabbrErrorFilter extends ZuulFilter {

private static final Logger log = LoggerFactory.getLogger(RabbrErrorFilter.class);
protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";

@Override
public String filterType() {
    return "post";
}

@Override
public int filterOrder() {
    return Integer.MIN_VALUE; // Needs to run before SendErrorFilter which has filterOrder == 0
}

@Override
public boolean shouldFilter() {
    // only forward to errorPath if it hasn't been forwarded to already
    RequestContext ctx = RequestContext.getCurrentContext();
    return ctx.containsKey("error.status_code") && !ctx.getBoolean(SEND_ERROR_FILTER_RAN,false);
}
@Override
public Object run() {
    try {
        RequestContext ctx = RequestContext.getCurrentContext();
        Object e = ctx.get("error.exception");

        if (e != null && e instanceof ZuulException) {
            ZuulException zuulException = (ZuulException)e;
            log.error("Zuul failure detected: " + zuulException.getMessage(), zuulException);

            // Remove error code to prevent further error handling in follow up filters
            ctx.remove("error.status_code");

            // Populate context with new response values
            ctx.setResponseBody("Overriding Zuul Exception Body");
            ctx.getResponse().setContentType("application/json");
            ctx.setResponseStatusCode(500); //Can set any error code as excepted
            ctx.setSendZuulResponse(false);
        }
    }
    catch (Exception ex) {
        log.error("Exception filtering in custom error filter", ex);
        ReflectionUtils.rethrowRuntimeException(ex);
    }
    return null;
}

我已经用调试器检查过 运行() 中的代码在抛出 zuul 异常时执行,但在控制台日志中我得到这个:

com.netflix.zuul.exception.ZuulException: Forwarding error
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.handleException(RibbonRoutingFilter.java:157) ~[spring-cloud-netflix-core-1.1.0.RC2.jar:1.1.0.RC2]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.forward(RibbonRoutingFilter.java:132) ~[spring-cloud-netflix-core-1.1.0.RC2.jar:1.1.0.RC2]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.run(RibbonRoutingFilter.java:78) ~[spring-cloud-netflix-core-1.1.0.RC2.jar:1.1.0.RC2]
    at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:112) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:197) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:161) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.FilterProcessor.route(FilterProcessor.java:120) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.ZuulRunner.route(ZuulRunner.java:96) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.http.ZuulServlet.route(ZuulServlet.java:116) ~[zuul-core-1.1.0.jar:1.1.0]
    at com.netflix.zuul.http.ZuulServlet.service(ZuulServlet.java:81) ~[zuul-core-1.1.0.jar:1.1.0]

网关后面的服务的响应超时,状态为 500。

--编辑* 更新 spring-cloud 依赖项(包含 zuul 1.3.0 核心)没有解决这个问题

请检查此解决方案:Spring Cloud: Zuul error handling。它对我有用,特别是针对 Zuul 的转发错误。

The Zuul error handling is being handled through RibbonRoutingFilter and SendErrorFilter and will forward any errored request to ${error.path}, which defaults to ‘/error’. In case you relay on the defaults that will be handled by Spring Boot’s BasicErrorController. You can overide this behaviour and implement your own ErrorController.

您不一定需要遵循 Vnd 错误样式,只需遵循编写 Spring 的 ErrorController.

自定义实现的一般逻辑即可