Spring Boot Admin 实现应用监控配置指南
单体应用监控配置
父项目依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
搭建监控服务端
Maven 依赖
<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>
</dependencies>
配置文件
server.port=9000
server.servlet.context-path=/admin
spring.application.name=Application-Monitor-Server
启动类
@Slf4j
@SpringBootApplication
@EnableAdminServer
public class MonitorServerApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorServerApplication.class, args);
log.info("监控服务端启动成功");
}
}
访问界面
启动服务后,访问 http://localhost:9000/admin 可看到监控管理界面。
客户端配置
Maven 依赖
<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>
</dependencies>
配置文件
server.port=8080
spring.application.name=Business-Service
spring.boot.admin.client.url=http://localhost:9000/admin
management.endpoints.web.exposure.include=*
配置说明:
spring.boot.admin.client.url:指定监控服务端地址management.endpoints.web.exposure.include=*:开放所有 Actuator 端点
应用信息展示配置
info.app.name="@project.name@"
info.app.description="@project.description@"
info.app.version="@project.version@"
info.app.java-version="@java.version@"
安全认证配置
添加安全依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置认证信息
spring.security.user.name=admin
spring.security.user.password=admin123
自定义安全配置
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServerProperties;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminServerProperties = adminServerProperties;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setTargetUrlParameter("redirectTo");
http.authorizeRequests()
.antMatchers(adminServerProperties.getContextPath() + "/assets/**").permitAll()
.antMatchers(adminServerProperties.getContextPath() + "/login").permitAll()
.antMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage(adminServerProperties.getContextPath() + "/login")
.successHandler(handler)
.and()
.logout()
.logoutUrl(adminServerProperties.getContextPath() + "/logout")
.and()
.httpBasic()
.and()
.csrf().disable();
}
}
客户端认证配置
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin123
内置异常处理
运行过程中可能出现以下异常信息:
java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
该异常不影响系统正常运行。如需消除,可将嵌入式容器从 Tomcat 替换为 Jetty:
<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-jetty</artifactId>
</dependency>
多服务监控方案
对于需要监控多个应用场景,推荐结合注册中心使用。监控服务端可从注册中心自动发现并注册客户端应用,实现统一管理。