gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:24 24 4
gpt4 key购买 nike

我正在尝试覆盖 zuul 代理的默认 senderorfilter,以便在我的网关后面的服务崩溃/关闭时,我可以提供更好的响应。这是我从 Customizing Zuul Exception 中的答案中提取的代码

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;
}

我已经用调试器检查了 run() 中的代码在抛出 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 自定义实现的一般逻辑即可。

关于java - 覆盖 zuul 错误过滤器导致转发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com