Spring Boot Admin is a community project of the Spring Boot project to manage and monitor Spring Boot applications. The Spring Boot Admin application is divided into the Server application and the Client application. The Server application collects information about Cient applications and monitors them. The underlying data acquisition is provided by the Actuator endpoints. Data can be collected in two ways: Applications register information with the Spring Boot Admin Server through HTTP; Alternatively, the Sring Boot Admin Server collects Client information using service discovery (Eureka, Consul).
Spring Boot Admin Server
Example Create a Spring Boot Admin Server application
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
Copy the code
The reason why Tomcat is replaced with Jetty is that an error occurs after startup
2020-03-09 14:06:57.337 ERROR 6316 --- [nio-8072-exec-1] o.a.catalina.connector.CoyoteAdapter : Exception while processing an asynchronous request
java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
at org.apache.coyote.AsyncStateMachine.asyncError(AsyncStateMachine.java:440) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:512) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.coyote.Request.action(Request.java:430) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:401) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:239) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:241) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.19.jar:9.0.19]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_211]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.19.jar:9.0.19]
at java.lang.Thread.run(Thread.java:748) [na:1.8. 0 _211]Copy the code
Spring-boot-starter-security Cause The Spring Boot Admin server can access sensitive endpoints of applications. Therefore, you are advised to add security configurations and security configuration classes for administrators and client applications
@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 {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
// @formatter:on}}Copy the code
This is a simple security configuration. However, after the security configuration is added, the client also needs to be configured. Otherwise, the client information cannot be registered with the server
spring.boot.admin.username=admin
spring.boot.admin.password=admin
Copy the code
Start the class
The @enableadMinServer annotation is added to the Boot class to introduce the Spring Boot AdminServer configuration
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); }}Copy the code
The configuration file
server:
port: 8072
spring:
security:
user:
name: admin
password: 123456
Copy the code
test
The Spring Boot Admin Server project has been configured
Spring Boot Admin Client
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Copy the code
Start the class
No additional setup is required
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); }}Copy the code
The configuration file
server:
port: 8073
spring:
application:
name: spring-boot-admin-client
boot:
admin:
client:
# Spring Boot Admin Server address
url: http://localhost:8072
# server set user name
username: admin
# server set password
password: 123456
instance:
prefer-ip: true
service-url: http://localhost:8073
management:
endpoints:
web:
exposure:
include: The '*'
endpoint:
health:
show-details: always
Copy the code
Security Configuration Class
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.authorizeRequests().anyRequest().permitAll().and().csrf().disable(); }}Copy the code
test
After the Client application is started, the following figure is displayed
The sample code
spring-boot-admin
Related to recommend
Mybatis + Druid