This tutorial shows you how to change the port and path of the actuator in Spring Boot.
If you use Spring Boot framework, there is a feature called actuator which allows you to monitor and manage the Spring Boot application. You can simply use it by just adding a dependency. The actuator exposes some endpoints that you can access. For example, to get the application status or HTTP trace. This tutorial shows you how to set a custom port and path for the endpoints exposed by actuator.
Change Actuator Port
By default, if you don't explicitly specify the actuator port, Spring will use the same port used to run the application. In some cases, we may want to use a different port for the actuator. Below are the examples of how to do it.
Set Port in Application Properties File
Changing the actuator port is very simple. It can be done by changing the management.server.port
property. Below is the example if you use an .properties
file.
management.server.port=9999
Below is another example if you use a .yaml
file.
management:
server:
port: 9999
To verify that you've correctly set the port. You can try to access one of the endpoints. For example, try to access http://localhost:9999/actuator/health
.
Set Port Programmatically
If you prefer to set the port programmatically instead, it can be done by setting the value of the above property programmatically. This can be useful if you need a custom logic to determine which port to use for the actuator.
Below is an example that sets the property programmatically. You need to create a SpringApplication
instance and call the setDefaultProperties
method. Then, you can pass a Map
or Properties
object which contains the value of management.server.port
property.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.setDefaultProperties(
Collections.singletonMap("management.server.port", "8888")
);
app.run(args);
}
}
Change Actuator Path
The path of the actuator can be changed as well. By default, the URL of an actuator endpoint is /actuator/{id}
. It's possible to change both the base path (/actuator
) and the {id}
path.
Set Path in Application Properties File
The base path can be changed by setting the value of management.endpoints.web.base-path
property. Meanwhile, to change the path of a specific actuator ID, you have to set the value of management.endpoints.web.path-mapping.{id}
property. The example below shows the content of a .properties
file that uses a custom actuator base path and a custom path for the health
actuator.
management.endpoints.web.base-path=/app-actuator
management.endpoints.web.path-mapping.health=app-health
Below is another example with .yaml
file.
management:
endpoints:
web:
base-path: /app-actuator
path-mapping:
health: app-health
Set Path Programmatically
It's also possible to change the path properties programmatically, similar to the example that sets the actuator port property.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.setDefaultProperties(
Map.ofEntries(
Map.entry("management.endpoints.web.base-path", "/app-actuator"),
Map.entry("management.endpoints.web.path-mapping.health", "/app-health")
)
);
app.run(args);
}
}
Summary
To use a custom port for the actuator in Spring Boot, it can be done by setting the management.server.port
. For changing the base path, you need to set the management.endpoints.web.base-path
property. Spring also makes it possible to change the path of an actuator ID by using the management.endpoints.web.path-mapping.{id}
property.