5
votes

I have a Java Spring Boot Application, which is currently running on http://localhost:8080. I will be containerising this application to be deployed in Kubernetes. For this I have enabled the actuator health endpoint by adding the following to the pom.xml file.

<!-- 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. I wish to change the PORT of the Health Endpoint URL to be 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)

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

3

3 Answers

8
votes

By default, actuator will be accessible using the default http port configured (commonly 8080).

Actuator has a number of configuration properties you can configure that fall under the management. prefix. You can specifically set the management.port in application.properties to have actuator endpoints to be served on the custom port:

In application.properties, set:

management.port=8081

For more details, check section 48.2 of the Spring Boot - Production Ready Monitoring documentation.

3
votes

As pointed out by GreenGiant, the Spring Boot 5+ way of doing this is through:

management.server.port
1
votes

If you follow the bellow 2 steps you can have 8081 private and 8080 as public.

Step 1:

Change your application server port to 8081 first. That means in your localhost it will run on 8081.

http://localhost:8081/actuator/health

http://localhost:8081/

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

Step 2:

However, when deployed in Kubernetes, 8081 port will be the private port.

In Kubernetes, expose 8080 port to the public.

Add a route that maps 8080 to your application 8081 port.