Zuul错误过滤器:根据错误设置响应状态

时间:2018-11-27 11:54:10

标签: httpresponse netflix-zuul

我正在使用Netflix Zuul v2.0作为身份验证代理,即我正在使用“前置”过滤器将JWT令牌添加到请求标头中。

我遇到的问题如下:当请求资源不可用时,返回“内部服务器错误”(500),而不是“请求超时”(408)错误。我通过实现custom error filter并在发生错误时返回408错误来解决了这个问题。

但是,现在即使每个原因不同,我也会为每个错误返回“请求超时”(408)错误。例如当出现“错误请求”或“未授权”请求时。

问题是:我怎么知道错误过滤器内部的真正原因是什么?例如我只想在确实超时的情况下返回408。 Afaik仅有的信息是发生错误,因此ctx.getResponseStatusCode()等于500。还是我错过了什么?

这是自定义错误过滤器的实现:

@Component
public class ConnectionErrorFilter extends ZuulFilter {

  protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";

  @Override
  public String filterType() {
    return FilterConstants.ERROR_TYPE;
  }

  @Override
  public int filterOrder() {
    return FilterConstants.SEND_ERROR_FILTER_ORDER - 1; // Invoke before default error filter
  }

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.set(SEND_ERROR_FILTER_RAN); 
    ctx.setResponseStatusCode(408);  // Workaround because it is here IMO not detectable what the real cause was.
    // Thus, we assume that the cloud was not reachable.
    return null;
  }
}

1 个答案:

答案 0 :(得分:0)

升级到Zuul 2.0.1.RELEASE resolved the problem。当请求资源不可用时,Gateway timeout (504) error is returned

相关问题