1.初识SpringBootAdmin
SpringBoot Admin是开源社区孵化的项目,用于对SpringBoot应用的管理和监控。SpringBoot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用http通讯方式实现数据交互;单体项目中需要整合spring-boot-admin-client才能让应用被监控。在SpringCloud项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服务应用整合spring-boot-admin-client就可以实现应用的管理和监控。
本文同样只叙述SpringBoot Admin 管理和监控单体应用 ,不涉及SpringCloud相关的内容 。
2.服务状态监控-服务端搭建
以spring-boot项目为底
新建maven项目cjqmonitor,带服务下线邮件通知功能
引入pom文件相关依赖
spring-boot-starter-web,spring-boot-starter,spring-boot-admin-starter-server,spring-boot-starter-security,spring-boot-starter-mail
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>cjqAdmin</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.2.2.RELEASE</version> </dependency> <!--监控服务端--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.2.2.RELEASE</version> </dependency> </dependencies> </project>
配置服务端配置文件-application.yml

server: port: 8081 spring: application: name: cjq-monitor profiles: active: dev # 配置登录用户名和密码 security: user: name: admin password: admin #配置邮件通知 mail: host: smtp.qq.com default-encoding: UTF-8 subject: cjq通知 username: 2829692427@qq.com password: qq邮箱授权码 boot: admin: ui: title: 'CJQ 服务状态监控' brand: 'HELLO WORLD CJQ' notify: mail: from: 2829692427@qq.com to: 978491323@qq.com enabled: true management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS #显示详细信息
编写启动类

package cjq; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAdminServer @SpringBootApplication public class CjqMonitorApplication { public static void main(String[] args) { SpringApplication.run(CjqMonitorApplication.class, args); } }
编写安全配置类-SecuritySecureConfig

package cjq.config; 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; /** * security配置 */ @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 { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests() //1.配置所有静态资源和登录页可以公开访问 .antMatchers(adminContextPath + "/assets/**").permitAll() .antMatchers(adminContextPath + "/login").permitAll() .anyRequest().authenticated() .and() //2.配置登录和登出路径 .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + "/logout").and() //3.开启http basic支持,admin-client注册时需要使用 .httpBasic().and() .csrf() //4.开启基于cookie的csrf保护 .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //5.忽略这些路径的csrf保护以便admin-client注册 .ignoringAntMatchers( adminContextPath + "/instances", adminContextPath + "/actuator/**" ); } }
浏览器访问测试
浏览器访问 http://localhost:8081/ 出现以下页面说明SpringBoot Admin服务端搭建成功
3.服务状态监控-客户端搭建
引入pom依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml配置
- 应用端口
- 开放端点用于SpringBootAdmin 监控
- 配置应用名称(该名称会在SpringBoot Admin的管理页面显示)
- 配置Admin Server的地址
server: port: 8082 spring: application: name: cjq boot: admin: client: url: http://localhost:8081 #这里配置admin server 的地址 # 配置 admin-server的账号和密码 username: admin password: admin #开放端点用于SpringBoot Admin的监控 management: endpoints: web: exposure: include: '*'
启动应用展示效果
下线之后短信通知
4.参考文献
https://www.cnblogs.com/zeng1994/p/de3232ab0e1ce857b57eebb8e8922f0f.html