1. What is Spring Boot Admin?
Spring Boot Admin is mainly used to manage and monitor spring boot applications. Applications can be registered with the Admin Server through the Spring Boot Admin Client or found using Spring Cloud registries such as Eureka and Consul.
Spring Boot Admin provides the following functions for registered applications:
- Show health status
- Display details, such as
- JVM and memory metrics
- micrometer.io index
- Data source indicators
- Cache index
- Display internal number
- Follow and download log files
- View JVM system and environment properties
- View Spring Boot configuration properties
- Publishable / env-& / refresh endpoint supporting Spring Cloud
- Easy log level management
- Interact with JMX beans
- View thread dump
- View HTTP traces
- View audit events
- View http endpoint
- View scheduled tasks
- View and delete active sessions (using spring session)
- View Flyway / Liquibase database migration
- Download heapdump
- Status change notification (via email, Slack, Hipchat, etc.)
- Event log of state changes (non persistent)
2. Quick start
2.1. Configure Spring Boot Admin Server
First, simply start spring. IO create a project. Since the Spring Boot Admin Server can run as a servlet or weblux application, you need to make a decision and add the corresponding Spring Boot Starter. In this example, we use Servlet Web Starter.
Add dependent
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency>
The Spring Boot Admin Server configuration is introduced by adding @ EnableAdminServer to the configuration
@SpringBootApplication @EnableAdminServer public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class, args); } }
Modify profile
# Application name spring.application.name=SpringBootAdmin # Application port server.port=8080
The first stage is completed, and the service can be started normally.
2.2. Configure Spring Boot Admin Client
Create a new project and add dependencies
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Modify profile
# Application name spring.application.name=admin-client # Application port server.port=9090 # The URL of the Spring Boot Admin Server to register with. spring.boot.admin.client.url=http://localhost:8080 # Like Spring Boot 2, by default, most endpoints are not exposed through http. We have exposed all endpoints. For production, you should carefully select the endpoint to expose. management.endpoints.web.exposure.include=*
The startup class does not need to be modified, and the project can be started directly
@SpringBootApplication public class AdminClientApplication { public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); } }
Visit 8080 again and register successfully
3. Used in conjunction with Eureka registry
Eureka server and client can be built for reference Spring cloud family bucket - built by Eureka Registration Center
3.1. Register Admin Server with Eureka Registration Center
Add Eureka Client related dependencies based on the previously configured Admin Server.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.6.RELEASE</version> </dependency>
Start the class, add @ EnableEurekaClient and let the registry find it
@SpringBootApplication @EnableAdminServer @EnableEurekaClient public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class, args); } }
Modify configuration item
# Application name spring.application.name=spring-boot-admin # Application port server.port=8080 management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # eureka client configuration eureka.client.service-url.defaultZone=http://eurekaServer1.com:18001/eureka/ eureka.instance.prefer-ip-address=true #The instance ID displayed in the registry can be distinguished by ip address and port eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port} eureka.instance.health-check-url-path=/actuator/health
Start the service, the registry list and admin have completed the registration
3.2. Admin Client registers with Eureka Registration Center
Based on the previous Admin Client, add Eureka Client related dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.6.RELEASE</version> </dependency>
Modify profile
# Application name spring.application.name=admin-client # Application port server.port=9090 # If you find it through the registration center, you don't need to register yourself #spring.boot.admin.client.url=http://localhost:8080 # Like Spring Boot 2, by default, most endpoints are not exposed through http. We have exposed all endpoints. For production, you should carefully select the endpoint to expose. management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # eureka client configuration eureka.client.service-url.defaultZone=http://eurekaServer1.com:18001/eureka/ eureka.instance.prefer-ip-address=true #The instance ID displayed in the registry can be distinguished by ip address and port eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port} eureka.instance.health-check-url-path=/actuator/health
Startup class
@SpringBootApplication @EnableEurekaClient public class AdminClientApplication { public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); } }
Start the service and complete the registration of Eureka and Admin
4. Configure Spring Security
Security can also be integrated based on security considerations.
Admin Server side, add Security related dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Startup class
import de.codecentric.boot.admin.server.config.AdminServerProperties; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; 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; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import java.util.UUID; @SpringBootApplication @EnableAdminServer @EnableEurekaClient public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class, args); } @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; public SecuritySecureConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.path("/")); http.authorizeRequests( (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() // Grant public access to all static assets and login pages. .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() //All other requests must be verified. ).formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() // Configure login and logout. ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) // Enable HTTP basic support. This is required for Spring Boot Admin Client registration. .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //Enable CSRF protection using Cookies .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()), // Disable CSRF protection for registered endpoints used by Spring Boot Admin Client. new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()), // Disable CSRF protection for registered endpoints used by Spring Boot Admin Client. new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // Disable CSRF protection for actuator endpoints. )) .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); } } }
Modify the Admin Server configuration file and add the following configuration.
spring.security.user.name=admin spring.security.user.password=admin eureka.instance.metadata-map.user.name=${spring.security.user.name} eureka.instance.metadata-map.user.password=${spring.security.user.password}
Access the Admin Server, enter the account and password, admin/admin.
5. Email notification
Add mail related dependencies in Admin Server service
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
Add relevant configuration items
spring.mail.host=smtp.qq.com # to and from should be configured, otherwise an error will be reported when sending an email spring.boot.admin.notify.mail.to=263527944@qq.com spring.boot.admin.notify.mail.from=263527944@qq.com # User name and password for the message spring.mail.username=263527944@qq.com spring.mail.password=wmdnczgokxvrcajc
Spring is not configured boot. admin. notify. mail. from
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as authorization user.
Wrong account or password
javax.mail.AuthenticationFailedException: 535 Login Fail. Please enter your authorization code to login
Successfully received the email of service online and offline