Spring Boot Admin: Complete Setup Guide for Service Monitoring & Alerting
This guide walks through setting up Spring Boot Admin for monitoring Spring Boot applications, covering server and client configuration, security setup, custom alerting, and microservices integration with Nacos service discovery.
Introduction
Spring Boot Admin is used for managing and monitoring Spring Boot applications. It can monitor service health, online status, and JVM metrics. The tool consists of a server ( spring-boot-admin-server) and a client ( spring-boot-admin-client) communicating via HTTP. In monolithic projects, each application must integrate the client to be monitored. In Spring Cloud projects, the server pulls application information directly from the service registry, so individual microservices do not need the client dependency.
Display application monitoring status
Application online/offline monitoring
View JVM and thread information
Visualize and download log files
Dynamically change log levels
Track HTTP request traces
Server Setup
1. Dependencies
<!--Used to check system monitoring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Spring Boot Admin Server monitoring server-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
<!--Add security protection to prevent unauthorized access-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>2. Enable Admin Server
Add @EnableAdminServer on the main application class.
3. Security Configuration
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// Login success handler
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
// Allow static resources
.antMatchers(adminContextPath + "/assets/**").permitAll()
// Allow login page
.antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
// All other requests require authentication
.anyRequest().authenticated()
.and()
// Custom login page
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
// Custom logout
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/instances",
"/actuator/**"
);
}
}4. YAML Configuration
server:
port: 9111
spring:
boot:
admin:
ui:
title: HMB Service Monitoring Center
client:
instance:
metadata:
tags:
environment: local
# Endpoints to probe on clients
probed-endpoints: health,env,metrics,httptrace:trace,threaddump:dump,jolokia,info,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents
monitor: # Monitoring request timeout
default-timeout: 20000
security: # Set username/password
user:
name: admin
password: admin
# Detailed endpoint monitoring info
management:
trace:
http:
enabled: true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always5. Custom Status Change Notifier
Implement a notifier to send alerts when service status changes. The example builds a message with service name, URL, status, details, and timestamp, then returns a Mono<Void> where the actual notification logic (e.g., SMS) would be placed.
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Component
public class WarnNotifier extends AbstractStatusChangeNotifier {
public WarnNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
// Service name
String serviceName = instance.getRegistration().getName();
// Service URL
String serviceUrl = instance.getRegistration().getServiceUrl();
// Service status
String status = instance.getStatusInfo().getStatus();
// Details
Map<String, Object> details = instance.getStatusInfo().getDetails();
// Current time
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(date);
// Build message
StringBuilder str = new StringBuilder();
str.append("Service Name: [" + serviceName + "]
");
str.append("Service Status: [" + status + "]
");
str.append("Address: [" + serviceUrl + "]
");
str.append("Time: " + format + "
");
return Mono.fromRunnable(() -> {
// Write your notification method here
// e.g., send SMS when service goes offline
});
}
}Client Setup
1. Dependencies
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>2. YAML Configuration
server:
port: 9222
spring:
application:
name: client
boot:
admin:
client: # Spring Boot Admin client config
url: http://localhost:9111 # Server connection address
username: admin # Server username
password: admin # Server password
instance:
prefer-ip: true # Register with IP
# Detailed endpoint monitoring info
management:
trace:
http:
enabled: true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
logfile: # Log file (only if you want to view logs online)
external-file: ./logs/client-info.log # Log file pathAfter starting the client, it registers with the server. The UI shows the application list; clicking an instance reveals JVM metrics, thread dumps, HTTP traces, and logs. Logs can be viewed and downloaded directly in the browser.
Microservices Integration with Nacos
Server Configuration
Add the Nacos discovery dependency:
<!-- Nacos registry configuration-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>Configure the Nacos server address in YAML:
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
# namespace: # Must match your services' namespaceClient Configuration
Microservice clients do not need the spring-boot-admin-starter-client dependency; the admin server discovers them via the service registry. If a service has a context-path, add metadata so the monitor can reach the actuator endpoints:
spring:
cloud:
nacos:
discovery:
metadata: # Monitor context-path config
management:
context-path: ${server.servlet.context-path}/actuatorCustom Alerting for Cluster Health
Problem: The default status change notifier only provides the current service's status. In a clustered or multi-instance deployment, you may also want to know the health of other instances of the same service or other services.
Solution: Before sending an alert, call the health check endpoints of other services (e.g., http://localhost:7050/attendance/actuator/nacos-discovery). A successful response means the service is up; an error indicates it is down. Extract the relevant information and include it in the notification.
Result: The alert message now contains the current service status, remaining healthy instances of that service, and the count of other healthy services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
