[英]Using a Hystrix Java Servlet & Servlet Filter in Jersey 2
I'm using Netflix' Hystrix libraries to act as a circuit breaker when connecting to remote services in a REST client I am building. 当我正在构建的REST客户端连接到远程服务时,我正在使用Netflix的Hystrix库作为断路器。 I would like to setup the event streaming and dashboard monitoring via the libraries they provide.
我想通过他们提供的库设置事件流和仪表板监控。 Looking at their example application here , it appears that I need to apply their servlet filters and servlet classes to my web application.
在这里查看他们的示例应用程序,似乎我需要将他们的servlet过滤器和servlet类应用于我的Web应用程序。
I'm using Spring Boot with Jersey 2 and wiring up my resources and filters in a JerseyConfig.java (no web.xml). 我正在使用Jersey Boot和Jersey 2,并在JerseyConfig.java(没有web.xml)中连接我的资源和过滤器。 I know that Jersey Filters are not the same as Servlet Filters and am struggling to integrate the two together.
我知道泽西过滤器与Servlet过滤器不同,我正在努力将两者整合在一起。
So, how do you take a Java Servlet Filter and make it work as a Jersey Filter and how do you take a Java Servlet and make it work as a Jersey Resource? 那么,你如何使用Java Servlet过滤器并使其作为Jersey过滤器工作?如何使用Java Servlet并使其作为Jersey资源工作?
My current strategy for the Servlets is to wrap them like so. 我目前的Servlets策略是将它们包装起来。 One for each.
每个一个。
@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);
}
}
This might be working, but the data is basically empty for some reason. 这可能有效,但由于某种原因,数据基本上是空的。 I am guessing that reason is because the Filters are not working.
我猜这个原因是因为过滤器不起作用。
data: {"type":"HystrixUtilization","commands":{},"threadpools":{}}
It is less clear to me how to wrap the Servlet Filters because they expect different inputs and outputs than a Jersey ContainerRequestFilter. 我不太清楚如何包装Servlet过滤器,因为它们期望输入和输出不同于Jersey ContainerRequestFilter。 The following implementation in my JerseyConfig seems to do nothing because the logs are not indicating that the filters are being registered and I cannot break on lines in these files in debug mode.
我的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));
}
}
Servlets and Servlet filters should not be registered in the Jersey config. 不应在Jersey配置中注册Servlet和Servlet过滤器。 They will simply be ignored.
他们将被忽略。 You should instead be registering them with Spring Boot with
ServletRegistrationBean
s and FilterRegistrationBean
s. 您应该使用带有
ServletRegistrationBean
和FilterRegistrationBean
的Spring Boot注册它们。
In you Spring configuration, you can do something like 在你的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;
}
Also: 也:
SpringComponentProvider
. SpringComponentProvider
。 This is automatically registered. /*
, which hogs up all the request. /*
,这会占用所有请求。 You can change the mapping or register Jersey as a filter to forward not found requests. An alternative route, and the one I ended up eventually going with, is to use the Spring cloud/boot starters if you're in a Spring Boot project. 另一条路线,也就是我最终选择的路线,就是使用Spring云/启动器,如果你在Spring Boot项目中。 This prevented me from having to explicitly define beans and filters as shown in the other answer.
这使我无法明确定义bean和过滤器,如另一个答案中所示。 Eventually basically worked out of the box.
最终基本上开箱即用。
<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>
Reference Circuit Breaker getting started guide. 参考断路器入门指南。 The one issue I faced was the Jackson 1 vs Jackson 2 conflict and was able to add the library exclusion.
我面临的一个问题是杰克逊1与杰克逊2的冲突,并且能够添加图书馆排除。 I basically had the Hystrix library jar before, but nothing wired up to make it work.
我以前基本上都有Hystrix库jar,但没有任何连线可以使它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.