繁体   English   中英

在Jersey 2中使用Hystrix Java Servlet和Servlet过滤器

[英]Using a Hystrix Java Servlet & Servlet Filter in Jersey 2

当我正在构建的REST客户端连接到远程服务时,我正在使用Netflix的Hystrix库作为断路器。 我想通过他们提供的库设置事件流和仪表板监控。 这里查看他们的示例应用程序,似乎我需要将他们的servlet过滤器和servlet类应用于我的Web应用程序。

我正在使用Jersey Boot和Jersey 2,并在JerseyConfig.java(没有web.xml)中连接我的资源和过滤器。 我知道泽西过滤器与Servlet过滤器不同,我正在努力将两者整合在一起。

那么,你如何使用Java Servlet过滤器并使其作为Jersey过滤器工作?如何使用Java Servlet并使其作为Jersey资源工作?

我目前的Servlets策略是将它们包装起来。 每个一个。

@Path("/hystrix.stream")
public class HystrixResource extends HystrixUtilizationSseServlet {

    @Context
    HttpServletRequest httpRequest;

    @Context
    HttpServletResponse httpResponse;

    //This returns void because it is a text/stream output that must remain open, 
    //so the httpResponse is continually written to until the conenction is closed
    @GET
    public void doGet() throws ServletException, IOException {
        doGet(httpRequest, httpResponse);
    }
}

这可能有效,但由于某种原因,数据基本上是空的。 我猜这个原因是因为过滤器不起作用。

data: {"type":"HystrixUtilization","commands":{},"threadpools":{}}

我不太清楚如何包装Servlet过滤器,因为它们期望输入和输出不同于Jersey ContainerRequestFilter。 我的JerseyConfig中的以下实现似乎什么都不做,因为日志没有指示过滤器正在注册,我不能在调试模式下在这些文件中的行中断。

@Component
@ApplicationPath("/")
public class JerseyConfig extends ResourceConfig {
    private static final Logger LOGGER = Logger.getLogger("JerseyConfig");
    public JerseyConfig(){
        //filter to provide a bridge between JAX-RS and Spring request attributes
        register(RequestContextFilter.class);
        register(SpringComponentProvider.class);
        //handles custom serialization
        register(new ObjectMapperContextResolver());
        //try to register the filters - which doesn't work because these aren't Jersey Filters
        register(HystrixRequestContextServletFilter.class);
        register(HystrixRequestLogViaResponseHeaderServletFilter.class);
        registerResources();

        /*
         * Enable the logging filter to see the HTTP response for each request.
         */
        register(new LoggingFilter(LOGGER, true));
    }
}

不应在Jersey配置中注册Servlet和Servlet过滤器。 他们将被忽略。 您应该使用带有ServletRegistrationBeanFilterRegistrationBean的Spring Boot注册它们。

在你的Spring配置中,你可以做类似的事情

@Bean
public ServletRegistrationBean someServlet() {
    ServletRegistrationBean registration = ServletRegisrationBean(
            new HystrixMetricsStreamServlet(), "/hystrix.stream");
    registration.setName("HystrixMetricsStreamServlet");
    return registration;
}

@Bean
public FilterRegistrationBean someFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new HystrixRequestContextServletFilter());
    registration.setUrlPatterns(Arrays.asList("/*"));
    registration.setName("HystrixRequestContextServletFilter");
    // you can also set the order of filters if you need to 
    return registration;
}

也:

  • 您不需要注册SpringComponentProvider 这是自动注册的。
  • 如果在尝试访问以这种方式注册的servlet时获得404,那将是因为您使用默认的Jersey映射/* ,这会占用所有请求。 您可以更改映射或将Jersey注册为过滤器以转发未找到的请求。 这篇文章

另一条路线,也就是我最终选择的路线,就是使用Spring云/启动器,如果你在Spring Boot项目中。 这使我无法明确定义bean和过滤器,如另一个答案中所示。 最终基本上开箱即用。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
        <exclusions>
            <!--We're running our Jersey server w/ Jackson 2. This import uses Jackson 1.x and creates a breaking conflict.-->
            <exclusion>
                <groupId>javax.ws.rs</groupId>
                <artifactId>jsr311-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

参考断路器入门指南。 我面临的一个问题是杰克逊1与杰克逊2的冲突,并且能够添加图书馆排除。 我以前基本上都有Hystrix库jar,但没有任何连线可以使它工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM