将多个URL路由到Spring Boot Actuator的运行状况端点


问题内容

我有一个配置为在/ manage / health中提供Spring Boot
Actuator的运行状况端点的应用程序。不幸的是,由于我要部署到的基础结构的一些细节,我需要将/和/ health都别名为/ manage /
health。

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

我宁愿显式定义所需的别名,而不是一些会影响所有流量性能的流量拦截器。作为Spring的新手,我不确定最好的方法是什么,而我的搜索并没有引导我正确的方向。

谁能提供指导?

谢谢。


问题答案:

将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");
    }
}