使用Hystrix Java Servlet& Servlet Filter在Jersey 2中

时间:2016-06-29 17:30:28

标签: java servlets spring-boot jersey-2.0 hystrix

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

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

那么,你如何使用Java Servlet Filter并使其作为Jersey Filter工作?如何使用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));
    }
}

2 个答案:

答案 0 :(得分:1)

不应在Jersey配置中注册Servlet和Servlet过滤器。他们将被忽略。您应该使用ServletRegistrationBeanFilterRegistrationBean s。

在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注册为过滤器以转发未找到的请求。请参阅this post

答案 1 :(得分:0)

另一种方法,也就是我最终选择的方法,就是使用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>

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