【问题标题】:Routing multiple URLs to Spring Boot Actuator's health endpoint将多个 URL 路由到 Spring Boot Actuator 健康端点
【发布时间】:2015-12-06 19:13:59
【问题描述】:

我有一个应用程序配置为在 /manage/health 处为 Spring Boot Actuator 的健康端点提供服务。不幸的是,由于我要部署的基础架构的一些细节,我需要将 / 和 /health 都别名为 /manage/health。

我没有看到通过属性仅自定义健康端点 URL 的选项。我假设无法添加适用于我不拥有的控制器的额外 @RequestMapping 注释。

我更愿意明确定义所需的别名,而不是一些影响所有流量性能的流量拦截器。对 Spring 来说相对较新,我不确定最好的方法是什么,我的搜索并没有把我引向正确的方向。

谁能提供一些指导?

谢谢。

【问题讨论】:

  • 为这些 URL 配置一个视图控制器,然后转发或重定向到正确的 URL。

标签: spring-mvc spring-boot spring-boot-actuator


【解决方案1】:

将 bean 添加到配置中以添加视图控制器。这必须扩展 WebMvcConfigurerAdapter 并简单地覆盖 addViewControllers 方法。

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/manage/health");
        registry.addViewController("/health").setViewName("forward:/manage/health");
    }
}

或者,如果您想强制重定向,请使用 addRedirectViewController 而不是 addViewController

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry. addRedirectViewController("/", "/manage/health");
        registry.addRedirectViewController("/health","/manage/health");
    }
}

【讨论】:

  • @M. Deinum 是否可以替换 url 以重新启动?让我们说 /myRestart。我想先做一些动作然后重启
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
相关资源
最近更新 更多