近期通过spring boot集成了redis、rabbitMQ及websocket服务,并对外提供相应的技术服务,整合完成之后,需要对这几个服务进行spring boot应用运行健康状况监控。之前了解过spring boot admin,但是没有系统的学习使用过,借着本次实践,记录下spring boot admin的配置及使用过程。
-
概述
Spring Boot Admin是一个Web应用,用于管理和监视Spring Boot应用程序的运行状态。每个Spring Boot应用程序都被视为客户端并注册到管理服务器。背后的数据采集是由Spring Boot Actuator端点提供。 -
设置管理服务器
- 国际惯例,添加依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加spring boot admin的server及mail依赖,mail用于进行监控邮件告警,spring security用于对spring boot admin的管理后台系统进行用户名密码验证,如果仅仅是spring boot admin的话,只需要spring-boot-admin-starter-server依赖即可。
-
application.yml(properties)配置信息
首先设置spring boot admin的用户名与密码,然后设置监控邮箱,其次为服务端口配置。 -
启动类修改
@EnableAdminServer
@SpringBootApplication
@ComponentScan(basePackages = {"com.ews.framework.*"})
@EnableSwagger2
public class EwsManagerApplication {
public static void main(String[] args) {
SpringApplication.run(EwsManagerApplication.class, args);
System.out.println("******************启动成功*****************");
}
}
- 安全配置
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecurityPermitAllConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
//静态资源和登录页面可以不用认证
http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
//其他请求必须认证
.anyRequest().authenticated()
//自定义登录和退出
.and().formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
.logoutUrl(adminContextPath + "/logout")
//启用HTTP-Basic, 用于Spring Boot Admin Client注册
.and().httpBasic().and().csrf().disable();
}
}
此时,服务器已准备好启动并可以注册客户端应用程序。
- 客户端配置
- 国际惯例
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- application.yml(properties)配置
首先设置spring boot admin管理服务器的url、用户名及密码。定义自身元数据用户名密码。设置暴漏所有监控点。 - 安全配置
@Slf4j
@Configuration
public class SpringSecurityActuatorConfig extends WebSecurityConfigurerAdapter {
public SpringSecurityActuatorConfig() {
log.info("SpringSecurityActuatorConfig... start");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//actuator请求
http.antMatcher("/actuator/**")
.authorizeRequests().anyRequest().authenticated()
//启用httpBasic认证
.and().httpBasic()
//禁用csrf
.and().csrf().disable();
}
}
- 启动管理服务及客户端
-
告警邮件
先后运行管理服务器与客户端后,停掉客户端服务后会受到告警邮件:
-
以上,整理完毕。