gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:22 25 4
gpt4 key购买 nike

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

我将 Spring Boot 与 Jersey 2 结合使用,并在 JerseyConfig.java(无 w​​eb.xml)中连接我的资源和过滤器。我知道 Jersey Filters 与 Servlet Filters 不同,并且正在努力将两者集成在一起。

那么,如何将 Java Servlet 过滤器用作 Jersey 过滤器,以及如何将 Java Servlet 用作 Jersey 资源?

我目前对 Servlet 的策略是像这样包装它们。一人一份。

@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 中的以下实现似乎什么都不做,因为日志没有表明正在注册过滤器,而且我无法在 Debug模式下中断这些文件中的行。

@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));
}
}

最佳答案

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

在你的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

关于java - 在 Jersey 2 中使用 Hystrix Java Servlet 和 Servlet 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com