SpringBoot Monitoring: HTTP Endpoints, JMX, and Custom Actuator Endpoints
This article explains how to monitor SpringBoot applications using built‑in HTTP actuator endpoints and JMX, demonstrates enabling and exposing specific endpoints, shows how to customize health, loggers, and metrics, and provides step‑by‑step code examples for creating custom monitoring endpoints and manually registering JMX MBeans.
SpringBoot Monitoring
Monitoring is essential for any service; without it you cannot see the current state or handle anomalies. Most micro‑service applications are built with SpringBoot, so understanding its monitoring features is crucial.
HTTP Endpoints Monitoring
Actuator endpoints let you observe and interact with the application. SpringBoot provides many built‑in endpoints and allows custom ones. Each endpoint has a unique ID and can be accessed via HTTP or JMX.
In SpringBoot 1.x the URL is http://ip:port/{id} . In SpringBoot 2.x a base path /actuator is added, so the URL becomes http://ip:port/actuator/{id} .
To enable HTTP monitoring, add the actuator starter dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>After starting the application you can access http://localhost:8080/actuator/health and see a JSON response.
SpringBoot provides many default endpoints, but for security only health and info are exposed by default. To expose additional endpoints, configure:
management:
endpoints:
web:
exposure:
include: [health,info,mappings] # or "*" to expose allYou can also enable or disable a specific endpoint dynamically:
management.endpoint.
.enabled=trueHealth Endpoint
The health endpoint shows basic health information. To include details (e.g., Redis status) add:
management:
endpoint:
health:
show-details: alwaysLoggers Endpoint
Access http://localhost:8080/actuator/loggers to view current logger levels. You can change a logger level at runtime (e.g., from INFO to DEBUG) via a POST request.
Metrics Endpoint
The metrics endpoint provides JVM, memory, class‑loading, CPU, and Tomcat metrics. Each metric can be queried individually.
Custom Monitoring Endpoints
SpringBoot allows you to create custom endpoints using annotations such as @Endpoint , @WebEndpoint , @JmxEndpoint , @ReadOperation , @WriteOperation , @DeleteOperation , and @Selector .
Example Custom Endpoint
@Endpoint(id="myEndpoint")
@Component
public class MyEndpoint {
private String STATUS = "up";
private String DETAIL = "一切正常";
@ReadOperation
public JSONObject test3() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("status", STATUS);
jsonObject.put("detail", DETAIL);
return jsonObject;
}
@ReadOperation
public JSONObject test3_1(@Selector String name) {
JSONObject jsonObject = new JSONObject();
if ("status".equals(name)) {
jsonObject.put("status", STATUS);
} else if ("detail".equals(name)) {
jsonObject.put("detail", DETAIL);
}
return jsonObject;
}
@WriteOperation // dynamic modification
public void test4(@Selector String name, @Nullable String value) {
if (!StringUtils.isEmpty(value)) {
if ("status".equals(name)) {
STATUS = value;
} else if ("detail".equals(name)) {
DETAIL = value;
}
}
}
}After starting the application, the custom endpoint is reachable at http://localhost:8080/actuator/myEndpoint . Using @Selector you can query sub‑paths, and @WriteOperation allows POST‑based updates.
JMX Monitoring
JMX (Java Management Extensions) provides monitoring of JVM resources. You can use the JDK tool jConsole to connect to a running SpringBoot application.
Manually Registering a JMX MBean
Define an interface ending with MBean :
public interface SystemInfoMBean {
int getCpuCore();
long getTotalMemory();
void shutdown();
}Implement the interface:
public class SystemInfo implements SystemInfoMBean {
@Override
public int getCpuCore() { return Runtime.getRuntime().availableProcessors(); }
@Override
public long getTotalMemory() { return Runtime.getRuntime().totalMemory(); }
@Override
public void shutdown() { System.exit(0); }
}Register the MBean:
public class JmxRegisterMain {
public static void main(String[] args) throws Exception {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("com.lonely.wolf.note.springboot.actuator.jmx:type=SystemInfo");
SystemInfo systemInfo = new SystemInfo();
mBeanServer.registerMBean(systemInfo, objectName); // register
System.in.read(); // keep the program alive
}
}Running this main method and opening jConsole shows the newly registered MBean.
When you annotate a class with @Endpoint or @JmxEndpoint , SpringBoot automatically registers it as an MBean.
Other Monitoring Solutions
Beyond SpringBoot's built‑in monitoring, you can integrate third‑party systems like Prometheus. Add the Micrometer Prometheus registry dependency:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>Prometheus is often paired with Grafana for a complete monitoring dashboard.
Summary
This article covered the use of SpringBoot Actuator for monitoring, introduced HTTP and JMX monitoring types, demonstrated how to expose and customize endpoints, showed how to create custom actuator endpoints, and explained manual JMX MBean registration.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.