Enable Actuator in Spring boot application
To enable Spring Boot Actuator in your application, you need to follow a few simple steps:
1. Add the Actuator Dependency: Include the Spring Boot Actuator dependency in your build configuration (Maven or Gradle).
2. Configure Actuator Endpoints: Set up the configuration to specify which actuator endpoints are exposed and how they are exposed.
3. Access and Use Actuator Endpoints: Once enabled, you can access various actuator endpoints to monitor and manage your application.
Table of Contents
Steps to Enable Actuator
1. Add the Actuator Dependency
To include Spring Boot Actuator in your application, you need to add the `spring-boot-starter-actuator` dependency.
- For Maven:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```
- For Gradle:
```groovy
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
```
2. Configure Actuator Endpoints
By default, only a few actuator endpoints are exposed. You can configure which endpoints are enabled and their visibility in your application.properties
or application.yml
file.
Example `application.properties`:
```properties
Expose only specific actuator endpoints
management.endpoints.web.exposure.include=health,info
Configure detailed health information visibility
management.endpoint.health.show-details=always
Set server port for actuator endpoints (optional)
management.server.port=8081
```
- Example `application.yml`:
```yaml
management:
endpoints:
web:
exposure:
include: health, info
endpoint:
health:
show-details: always
server:
port: 8081
```
3. Access and Use Actuator Endpoints
Once you have added the dependency and configured the endpoints, you can start your Spring Boot application and access the actuator endpoints using HTTP requests.
- Health Endpoint:
- URL:
http://localhost:8080/actuator/health
- Description: Provides information about the application’s health status.
- Info Endpoint:
- URL:
http://localhost:8080/actuator/info
- Description: Provides general information about the application, such as build version, description, etc.
Here’s a basic setup and usage example of Spring Boot Actuator:
1. Create a Spring Boot Application
Main Application Class:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
2. Add Actuator Dependency
Ensure that your pom.xml
or build.gradle
file includes the Spring Boot Actuator dependency as shown above.
3. Configure Actuator Endpoints
In `application.properties`, add:
```properties
management.endpoints.web.exposure.include=health,info
management.endpoint.health.show-details=always
```
4. Run the Application
Start your Spring Boot application. You can then access actuator endpoints via the browser or tools like curl
:
- Health Check:
```bash
curl http://localhost:8080/actuator/health
```
- Info Endpoint:
```bash
curl http://localhost:8080/actuator/info
```
5. Custom Actuator Endpoints
If you want to add custom actuator endpoints, create a new component:
Custom Endpoint Class:
```java
package com.example.demo;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public String custom() {
return "This is a custom actuator endpoint";
}
}
```
Usage:
- URL: `http://localhost:8080/actuator/custom`
- Response:
```text
This is a custom actuator endpoint
```
Conclusion of Spring boot application
- Enable Actuator: Add
spring-boot-starter-actuator
dependency to your project. - Configure Endpoints: Set up configuration in
application.properties
orapplication.yml
. - Access Endpoints: Use HTTP requests to access built-in and custom actuator endpoints