文章前言
上篇文章了解了 Spring Boot Actuator,引入后即可通過訪問不同的端點,來獲得相應(yīng)的監(jiān)控信息。
對應(yīng) HTTP 方式請求,返回的數(shù)據(jù)都是 JSON 格式,這對于運維或是其他人員來說當(dāng)然不是很方便直觀,特別是當(dāng)需要監(jiān)控的應(yīng)用越來越多時,如果還依舊通過地址欄來逐個訪問,就顯得過于繁瑣和低效了。下面,我們再來認(rèn)識下 Spring Boot Admin 這個 Spring Boot Application UI 監(jiān)控管理工具。
快速上手
Spring Boot Admin 由 Server 和 Client 兩個端組成,其中 Client 端通常是需要被監(jiān)控的應(yīng)用。先來配置下 Server 端的依賴,考慮到安全性方面的問題,這里還額外加入了 Security:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
</dependencies>
接著在 application.yml 文件中配置登錄用戶及密碼:
spring:
security:
user:
name: admin
password: admin
必要的 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();
// 設(shè)置redirectTo參數(shù)和登錄成功重定向地址
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
// 授予對靜態(tài)資源和登錄頁面的公共訪問權(quán)
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
// 除上面配置的其他請求都必須經(jīng)過身份驗證
.anyRequest().authenticated()
.and()
// 配置登錄和退出請求路徑
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
// 啟用HTTP-Basic,這是Spring Boot Admin Client注冊所必需的
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
最后,在啟動類上加入 @EnableAdminServer ,運行服務(wù)后訪問:http://localhost:8080 ,輸入用戶名密碼 admin 進入系統(tǒng):
@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServerApplication.class, args);
}
}

Server 端到此就搭建好了,下面再來處理 Client 端服務(wù),同樣的先加入相關(guān)依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
由于引入了 Security 模塊,為了可以正常訪問到 Actuator 的 Endpoints,這里還需要做相應(yīng)的配置處理:
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().ignoringAntMatchers("/actuator/**").disable();
}
}
在配置文件中加入必要信息:
server:
port: 8081
spring:
application:
name: spring-boot-admin-client
boot:
admin:
client:
# Spring Boot Admin Server 服務(wù)地址,可配置多個
url: http://localhost:8080
instance:
name: ${spring.application.name}
prefer-ip: true
# Spring Boot Admin Server 認(rèn)證信息
username: admin
password: admin
# 設(shè)置為true,客戶端將只注冊1個 Admin Server 服務(wù)(按定義的順序)
# 當(dāng)該 Admin Server 服務(wù)宕機,將自動注冊下個 Admin Server 服務(wù)器;
# 如果為false,將針對所有管理服務(wù)器進行注冊
register-once: true
# Actuator 配置
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
info:
version: @project.version@
name: @project.artifactId@
author: happyJared
blog: https://blog.mariojd.cn/
最后,啟動 Client 服務(wù),我們來查看 Admin Server 提供的 UI 監(jiān)控管理系統(tǒng):

點擊 Instances 進入,這時就可以方便的查看該應(yīng)用的所有監(jiān)控狀態(tài)信息。

此外,Spring Boot Admin 還支持動態(tài)更改 Logger Level、提供異常監(jiān)控告警等功能,這些姿勢可以自行解鎖。
參考閱讀
Spring Boot Admin
Spring Boot Admin Reference Guide
監(jiān)控管理之Spring Boot Admin使用
示例源碼
歡迎關(guān)注我的個人公眾號:超級碼里奧
如果這對您有幫助,歡迎點贊和分享,轉(zhuǎn)載請注明出處