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