Master Spring Boot 3 Actuator: Custom Endpoints, Health Checks, and Monitoring
Explore comprehensive Spring Boot 3 Actuator capabilities—including enabling CORS, creating custom endpoints, configuring health indicators, HTTP tracing, security auditing, and process monitoring—through detailed explanations, YAML configurations, and full Java code examples, empowering developers to effectively monitor and manage production-ready applications.
Introduction
Spring Boot provides many additional features that help you monitor and manage applications in production. The Actuator module exposes built‑in endpoints (e.g., /health ) and allows you to add custom ones.
Maven dependency:
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency></code>Powerful Features
2.1 CORS Support
<code>management:
endpoints:
web:
cors:
allow-credentials: true
allowed-headers: '*'
allowed-origins: ''</code>2.2 Custom Endpoint
<code>@Component
@Endpoint(id = "pack")
public class PackEndpoint {
@ReadOperation
public CustomData getData() {
return new CustomData("Custom Endpoint");
}
@WriteOperation
public void writeData(String title) {
System.out.printf("修改标题:%s%n", title);
}
static class CustomData {
private String title;
public CustomData(String title) { this.title = title; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
}
}</code>2.3 Application Health Information
Default /health response shows basic status. To display detailed information, add:
<code>management:
endpoint:
health:
show-details: always</code>You can also enable or disable specific health indicators, e.g.:
<code>management:
endpoint:
health:
show-details: always
health:
db:
enabled: false
diskspace:
enabled: true</code>Role‑based detail exposure:
<code>management:
endpoint:
health:
show-details: when-authorized
roles:
- ROLE_ADMIN</code>2.4 Custom Health Indicator
<code>@Component
public class PackHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check();
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
private int check() {
return new Random().nextInt(2);
}
}</code>After adding this bean, /health will return {"status":"UP"} or {"status":"DOWN", "details":{...}} accordingly.
2.5 HTTP Request Tracing
Enable tracing by defining an HttpTraceRepository bean, e.g. the in‑memory implementation:
<code>@Bean
InMemoryHttpTraceRepository inMemoryHttpTraceRepository() {
return new InMemoryHttpTraceRepository();
}</code>Access traces via /httptrace . You can refine what is recorded:
<code>management:
trace:
http:
include:
- request-headers
- response-headers</code>2.6 Security Auditing
When Spring Security is on the classpath, Actuator provides an audit framework. Enable it by providing an AuditEventRepository bean:
<code>@Bean
SecurityFilterChain actuatorSecurityFilterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable());
http.authorizeHttpRequests().antMatchers("/ac/**").hasRole("ACTUATOR");
http.authorizeHttpRequests().anyRequest().permitAll();
http.formLogin(customizer -> Customizer.withDefaults());
return http.build();
}
@Bean
InMemoryAuditEventRepository auditEventRepository() {
return new InMemoryAuditEventRepository();
}</code>2.7 Process Monitoring
Spring Boot can write process‑related files:
ApplicationPidFileWriter : creates application.pid containing the JVM PID.
WebServerPortFileWriter : creates application.port with the server’s listening port.
Add the following line to META-INF/spring.factories :
<code>org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter</code>After starting the application, the files appear in the project’s root directory.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.