将多个 URL 路由到 Spring Boot Actuator 的健康端点 [英] Routing multiple URLs to Spring Boot Actuator's health endpoint

查看:40
本文介绍了将多个 URL 路由到 Spring Boot Actuator 的健康端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app configured to serve Spring Boot Actuator's health endpoint at /manage/health. Unfortunately due to some details of the infrastructure I'm deploying to I need to alias both / and /health over to /manage/health.

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

I don't see an option to customize just the health endpoint URL via properties. I'm assuming there's no way to add extra @RequestMapping annotations that apply to a controller I don't own.

我更愿意明确定义所需的别名,而不是一些影响所有流量性能的流量拦截器.作为 Spring 的新手,我不确定最好的方法是什么,而且我的搜索没有将我引向正确的方向.

I'd prefer to explicitly define the required aliases as opposed to some traffic interceptor that affects the performance of all traffic. Being relatively new to Spring, I'm unsure what the best way is to proceed and my searches aren't leading me in the right direction.

谁能提供一些指导?

谢谢.

推荐答案

在配置中添加一个 bean 以添加一个视图控制器.这必须扩展 WebMvcConfigurerAdapter 并简单地覆盖 addViewControllers 方法.

Add a bean to the configuration to add a view controller. This been must extend WebMvcConfigurerAdapter and simply override the addViewControllers method.

@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.

Or if you want to force a redirect use addRedirectViewController instead of addViewController.

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

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

这篇关于将多个 URL 路由到 Spring Boot Actuator 的健康端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆