Java Spring 引导 - 将执行器健康端点的端口更改为自定义端口

Java Spring Boot - Change the Port of Actuator Health Endpoint to a Custom Port

我有一个 Java Spring 启动应用程序,目前 运行 在 http://localhost:8080 上。我将把这个应用程序容器化以部署在 Kubernetes 中。为此,我通过将以下内容添加到 pom.xml 文件来启用 actuator health 端点。

<!-- Spring boot actuator to expose metrics endpoint -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

现在,应用程序 Health Endpoint 在 http://localhost:8080/actuator/health 端点上成功运行。我希望将 Health Endpoint URL 的端口更改为 8081(这是为了确保我可以将 8080 端口暴露给 public,而将 8081 端口仅暴露给负载均衡器。这是个人喜好)

如何更改 Health Endpoint 的端口,以便 Health 端点 URL 变为 http://localhost:8081/actuator/health, and the application runs on http://localhost:8080/.

如果您按照以下 2 个步骤操作,您可以将 8081 设为私有,并将 8080 作为 public。

第 1 步:

先把你的应用服务器端口改成8081。这意味着在您的本地主机上它将 运行 on 8081.

http://localhost:8081/actuator/health

http://localhost:8081/

To do that, in your application.properties file, add server.port=8081

第 2 步:

但是在Kubernetes中部署时,8081端口将是私有端口。

在 Kubernetes 中,将 8080 端口暴露给 public。

添加将 8080 映射到应用程序 8081 端口的路由。

默认情况下,可以使用配置的默认 http 端口(通常为 8080)访问执行器。

Actuator 有许多您可以配置的配置属性,它们位于 management. 前缀下。您可以在 application.properties 中专门设置 management.port 以在自定义端口上提供执行器端点:

application.properties中设置:

management.port=8081

有关详细信息,请查看 Spring Boot - Production Ready Monitoring 文档的第 48.2 节。

正如 GreenGiant 所指出的,Spring Boot 5+ 的实现方式是:

management.server.port