设置 server.context-path 的 Spring Boot Actuator 端点

Posted

技术标签:

【中文标题】设置 server.context-path 的 Spring Boot Actuator 端点【英文标题】:Spring Boot Actuator endpoints with set server.context-path 【发布时间】:2015-11-15 04:08:27 【问题描述】:

在 spring boot 应用程序中,我设置了例如server.context-path=/mymodule。这很方便,因为我不需要在@RequestMapping 中一遍又一遍地重复/mymodule 前缀。 此外,我希望将执行器端点组合在具有公共前缀的 URL 上,因此我设置了management.context-path=/actuator。 现在执行器端点映射到/mymodule/actuator

从安全角度来看,我希望将执行器端点映射到 /actuator。反向代理https://mydomain/api/mymodule -> http://oneofmyserver:port/mymodule 上的简单配置可保护最终用户无法访问执行器。

是否可以将执行器端点映射到/actuator

【问题讨论】:

代替server.context-path 设置server.servlet-path 设置server.servlet-path 会导致执行器端点仍然映射到/mymodule/actuator 【参考方案1】:

从安全角度来看,可能更好的解决方案是在完全不同的端口上导出执行器。为此,只需添加以下属性:

management.port=9080

您也可以使用更改执行器端点的上下文路径

management.context-path=/actuator

【讨论】:

谢谢,它有效!我之前已经考虑过这个解决方案,只有额外的端口是缺点。我唯一的问题是 Spring Cloud org.springframework.cloud.client.ServiceInstance 不暴露 management.port,例如spring-boot-admin 无法为我的微服务构建有效的管理 URL。 @ŁukaszIdkowiak 只需将此作为问题提交给 Spring 我们已经有这个github.com/spring-cloud/spring-cloud-netflix/issues/425的问题 @spencergibb 谢谢 :)【参考方案2】:

您可以使用management.endpoints.web.base-path 属性更改管理端点的前缀。 Reference from spring-boot document.

以下示例将 /actuator/health 重新映射到 /healthcheck

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

【讨论】:

【参考方案3】:

如果您无法使用不同的端口,并且您的环境中有例如 eureka,您可以使用以下内容:

    配置您发送到 eureka 的其他元数据。只需在其中添加一些带有上下文路径值的参数即可。 spring docs

    Configure Prometheus for using Eureka。 之后,您将可以访问您的自定义元数据

    最后您可以在relabeling step 中使用这些新参数。

【讨论】:

以上是关于设置 server.context-path 的 Spring Boot Actuator 端点的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 应用中server.context-path的作用

SpringBoot入门学习笔记

SpringBoot项目属性配置

SpringBoot配置嵌入式的Servlet

Spring Security 4.x -> 5.x 踩坑记录

spring boot 项目属性配置