[英]Java Spring Boot - Change the Port of Actuator Health Endpoint to a Custom Port
I have a Java Spring Boot Application, which is currently running on http://localhost:8080 .我有一个 Java Spring 引导应用程序,它目前正在http://localhost:8080上运行。 I will be containerising this application to be deployed in Kubernetes.
我将容器化这个应用程序以部署在 Kubernetes 中。 For this I have enabled the
actuator health
endpoint by adding the following to the pom.xml
file.为此,我通过将以下内容添加到
pom.xml
文件中来启用actuator health
端点。
<!-- Spring boot actuator to expose metrics endpoint -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Now, the application Health Endpoint works successfully on http://localhost:8080/actuator/health endpoint.现在,应用程序 Health Endpoint 在http://localhost:8080/actuator/health端点上成功运行。 I wish to change the PORT of the Health Endpoint URL to be 8081 .
我希望将Health Endpoint URL 的 PORT 更改为 8081 。 (This is to ensure that I can expose 8080 port to the public and expose 8081 port only to the load balancer. This is a personal preference)
(这是为了保证我可以将8080端口暴露给公众,而将8081端口只暴露给负载均衡器。这是个人喜好)
How can I change the port of the Health Endpoint so that the health endpoint URL becomes http://localhost:8081/actuator/health , and the application runs on http://localhost:8080/ .如何更改 Health Endpoint 的端口,以便Health Endpoint URL 变为http://localhost:8081/actuator/health ,并且应用程序在Z80791B3AE7002CB88C24687080998080D上运行。
By default, actuator will be accessible using the default http port configured (commonly 8080).默认情况下,可以使用配置的默认 http 端口(通常为 8080)访问执行器。
Actuator has a number of configuration properties you can configure that fall under the management.
Actuator 具有许多可以配置的配置属性,这些属性属于
management.
prefix.字首。 You can specifically set the
management.port
in application.properties
to have actuator endpoints to be served on the custom port:您可以在
application.properties
中专门设置management.port
以在自定义端口上提供执行器端点:
In application.properties
, set:在
application.properties
中,设置:
management.port=8081
For more details, check section 48.2 of the Spring Boot - Production Ready Monitoring documentation.有关更多详细信息,请查看Spring 启动 - 生产就绪监控文档的第 48.2 节。
As pointed out by GreenGiant, the Spring Boot 5+ way of doing this is through:正如 GreenGiant 所指出的,Spring Boot 5+ 的执行方式是:
management.server.port
If you follow the bellow 2 steps you can have 8081 private and 8080 as public.如果您遵循以下 2 个步骤,您可以将 8081 设为私有,将 8080 设为公开。
Step 1:步骤1:
Change your application server port to 8081 first.首先将您的应用程序服务器端口更改为 8081。 That means in your localhost it will run on 8081.
这意味着在您的本地主机中它将在 8081 上运行。
http://localhost:8081/actuator/health http://localhost:8081/actuator/health
http://localhost:8081/ http://localhost:8081/
To do that, in your application.properties file, add server.port=8081
Step 2:第2步:
However, when deployed in Kubernetes, 8081 port will be the private port.但是,当部署在 Kubernetes 中时,8081 端口将是私有端口。
In Kubernetes, expose 8080 port to the public.在 Kubernetes 中,公开 8080 端口。
Add a route that maps 8080 to your application 8081 port.添加将 8080 映射到应用程序 8081 端口的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.