How do you access actuator endpoints in Spring Boot?
Table of Contents
Introduction
Spring Boot Actuator provides several useful endpoints to monitor and manage your Spring Boot application. These endpoints offer vital information such as the health of your application, metrics, environment details, and other system statistics. By default, most actuator endpoints are not exposed for security reasons, but you can configure and access them as needed.
This guide explains how to access Spring Boot Actuator endpoints, configure them, secure them, and access them for monitoring purposes.
Steps to Access Actuator Endpoints
1. Add Spring Boot Actuator Dependency
First, ensure that Spring Boot Actuator is included in your project dependencies. You can add the dependency to your pom.xml
(for Maven) or build.gradle
(for Gradle).
For Maven:
For Gradle:
This will enable Actuator’s endpoints, which you can access once configured.
2. Expose Actuator Endpoints
By default, Spring Boot Actuator disables all web endpoints. You need to explicitly expose the endpoints you want to use, like /actuator/health
or /actuator/metrics
, via configuration in application.properties
or application.yml
.
In **application.properties**
:
In **application.yml**
:
After adding the above configuration, you can access the following endpoints:
/actuator/health
– Provides the health status of the application/actuator/metrics
– Displays metrics such as memory usage, garbage collection, and uptime/actuator/env
– Lists environment properties/actuator/beans
– Shows the list of all Spring beans
3. Accessing the Exposed Endpoints
Once you've configured the actuator, you can access the exposed endpoints through a browser, Postman, or curl
. The default URL pattern is http://localhost:8080/actuator/
.
-
Health Check: To check if the application is healthy, visit:
The response will show the health status, typically:
If any of the components like the database or external APIs are down, the status will be
DOWN
, along with additional details. -
Metrics: To view application metrics such as JVM memory usage or garbage collection, visit:
This will return a list of available metric names. To view a specific metric, such as memory usage:
The response will show the detailed metric data.
-
Environment Properties: To get environment properties, use:
This endpoint lists system properties, environment variables, and configuration properties in your application.
4. Securing Actuator Endpoints
In production environments, you might want to secure actuator endpoints to prevent unauthorized access. By default, these endpoints are accessible without authentication, which could be a security risk.
You can secure actuator endpoints using Spring Security. For example, to protect the health and metrics endpoints with basic authentication, you can configure a security configuration class:
With this configuration, only users with the ADMIN
role will be able to access /actuator/health
and /actuator/metrics
.
5. Customizing the Actuator Endpoints
You can customize the actuator endpoints to suit your needs. For example, if you want to add custom health checks or expose additional metrics, you can implement custom health indicators and expose them via the /actuator/health
endpoint.
Example: Custom Health Indicator
This custom health indicator will be automatically included in the /actuator/health
response.
6. Additional Actuator Endpoints
Besides the common /health
and /metrics
endpoints, Spring Boot Actuator offers other endpoints such as:
/actuator/info
– Provides arbitrary application info (like build version or custom data)./actuator/loggers
– Exposes logging levels and allows you to change them at runtime./actuator/threaddump
– Dumps thread stack information./actuator/heapdump
– Provides a heap dump.
You can configure which endpoints to expose based on your monitoring and management requirements.
Conclusion
Spring Boot Actuator is an essential tool for monitoring your application’s health, metrics, and other important details. By exposing actuator endpoints and securing them as needed, you can keep track of your application's state and make informed decisions. Customizing these endpoints allows you to tailor the monitoring process to your application’s specific needs, whether it’s adding new health checks or tweaking metrics exposure.