SpringBootAdmin
是一個針對 Spring Boot 的 Actuator 介面進行 UI 美化封裝的監控工具,它可以在列表中瀏覽所有被監控 spring-boot 專案的基本資訊、詳細的 Health 資訊、記憶體資訊、JVM 資訊、垃圾回收資訊、各種配置資訊(比如資料來源、快取列表和命中率)等。可分為服務端(spring-boot-admin-server
)和客戶端(spring-boot-admin-client
),服務端和客戶端之間採用http通訊方式實現資料互動。服務端server需要單獨啟動一個服務,而客戶端client只需要整合到各個微服務中。
1、初識SpringBootAdmin
首先我們需要了解到Spring Boot Admin應用程式是能夠提供以下功能供我們使用:
- 顯示健康狀況
- 顯示詳細資訊
- JVM和記憶體指標
- micrometer.io指標
- 資料來源指標
- 快取指標
- 顯示內部編號
- 關注並下載日誌檔案
- 檢視JVM系統和環境屬性
- 檢視Spring Boot配置屬性
- 支援Spring Cloud的可釋出/ env-&/ refresh-endpoint
- 輕鬆的日誌級別管理
- 與JMX-beans互動
- 檢視執行緒轉儲
- 檢視http-traces
- 檢視稽核事件
- 檢視http端點
- 檢視預定的任務
- 檢視和刪除活動會話(使用spring-session)
- 檢視Flyway / Liquibase資料庫遷移
- 下載heapdump
- 狀態更改通知(通過電子郵件,Slack,Hipchat等)
- 狀態更改的事件日誌(非永續性)
2、搭建服務端--POM檔案中新增相關依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.1</version>
</dependency>
3、修改服務端application啟動類
在咱們啟動類上面新增@EnableAdminServer註解,進行啟用SpringBootAdminServer服務端
@SpringBootApplication
@EnableAdminServer
public class BootAdminServerApplication { public static void main(String[] args) {
SpringApplication.run(BootAdminServerApplication.class, args);
}
}
4、配置security安全資訊
在application.properties檔案中新增以下配置資訊。
# 應用程式埠
server.port=8085
# 配置一個賬號和密碼
spring.security.user.name=admin
spring.security.user.password=admin
初始化SecuritySecureConfig配置(如未初始化是看不到帶SpringBootAdmin Logo登入頁面的)
@Configuration
public static 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"); 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().disable();
}
}
5、啟動server服務端
服務啟動後,在瀏覽器中輸入以下地址。我們是可以看見對應登入頁面,對應賬號密碼就是咱們在properties檔案中配置的。
http://127.0.0.1:8085/login
登入後可以看到應用列表數量是空的,此時咱們需要開始搭建咱們的Client客戶端了。
6、搭建client客戶端
在pom檔案中新增以下依賴資訊。(注意版本要與server端保持一致)
<!-- SpringBootAdmin管控臺 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.1</version>
</dependency>
修改properties檔案
spring.boot.admin.client.url=http://127.0.0.1:8085
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
spring.application.name=spring-boot-application
management.endpoints.web.exposure.include=*
spring.boot.admin.client.url 指向我們上面服務端的專案介面路徑。management.endpoints.web.exposure.include 表示將所有埠都暴露出來,可以被監控到。spring.application.name 表示改專案在spring-boot-admin 上的的顯示名稱。spring.boot.admin.client.username 和password 就是設定的使用者名稱和密碼了,這裡需要注意的是,如果admin-server 中沒有整合 security 的話,不用配置使用者名稱和密碼也可以註冊進去,在服務端可以監控到,但如果admin-server 集成了security,就需要保證client 中配置的使用者名稱和server 中配置的使用者名稱密碼保持一致。
把client客戶端啟動後,會自動註冊到咱們server服務端,咱們可以通過server服務端應用牆找到對應服務檢視詳細指標資訊。(題外話:期間博主是有遇到客戶端啟動後,服務端無法採集到對應指標資訊。原因是由於client客戶端有配置security,沒有給對應探針介面放行。如大家客戶端有用到security的話,需要在security配置中放行以下兩個介面資訊。)
// 對應匿名+已授權均可訪問
.antMatchers("/actuator/**","/instances").permitAll()