Zuul过滤器中的HttpHostConnectException

时间:2019-01-29 20:42:37

标签: java spring-boot netflix-zuul netflix

我正在使用SpringBoot 2.0.3spring-cloud-starter-netflix-zuulcloud Finchley.RELEASE。我有以下课程添加了headers

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class CorsRequestIdFilter extends OncePerRequestFilter {

    private static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {

        if (isCorsRequest(request) && isPreFlightRequest(request)) {

            Set<String> headers = new HashSet<>();
            headers.add("Authorization");
            headers.add("X");

            String requestHeader = request.getHeader("Access-Control-Request-Headers");
            if (requestHeader != null) {
                headers.addAll(Arrays.asList(requestHeader.split(",")));
            }

            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
            response.addHeader("Access-Control-Max-Age", "3600");
            response.addHeader("Access-Control-Allow-Headers", join(",", headers));

            return;
        }

        filterChain.doFilter(request, response);
    }

    private boolean isCorsRequest(HttpServletRequest request) {
        return (request.getHeader(HttpHeaders.ORIGIN) != null);
    }

    private boolean isPreFlightRequest(HttpServletRequest request) {
        return (isCorsRequest(request) && request.getMethod().equals(HttpMethod.OPTIONS.name()) &&
                request.getHeader(ACCESS_CONTROL_REQUEST_METHOD) != null);
    }

}

Netflix-starter给了spring-cloud-netflix-zuul版本的2.0.0我。 我发现了这个问题https://github.com/spring-cloud/spring-cloud-netflix/issues/3033

我有类似的问题,确切地说只有这部分匹配:

com.netflix.zuul.exception.ZuulException: Filter threw Exception
(...)
Caused by: java.lang.ClassCastException: org.apache.http.conn.HttpHostConnectException cannot be cast to com.netflix.zuul.exception.ZuulException
    at org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter.findZuulException(SendErrorFilter.java:104)
    at org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter.run(SendErrorFilter.java:73)
    at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:117)
    at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:193)
    ... 107 common frames omitted

但是我不知道这对我有什么帮助。 我想知道为什么会发生此错误,怎么办才能使其消失?

我暂时将spring-cloud-starter-netflix-zuul的版本更改为2.0.1,但这完全无济于事。

0 个答案:

没有答案