文章前言
上篇文章了解了 Spring Boot Actuator翘悉,引入后即可通過(guò)訪問(wèn)不同的端點(diǎn),來(lái)獲得相應(yīng)的監(jiān)控信息。
對(duì)應(yīng) HTTP 方式請(qǐng)求,返回的數(shù)據(jù)都是 JSON 格式围橡,這對(duì)于運(yùn)維或是其他人員來(lái)說(shuō)當(dāng)然不是很方便直觀,特別是當(dāng)需要監(jiān)控的應(yīng)用越來(lái)越多時(shí)缕贡,如果還依舊通過(guò)地址欄來(lái)逐個(gè)訪問(wèn)翁授,就顯得過(guò)于繁瑣和低效了。下面晾咪,我們?cè)賮?lái)認(rèn)識(shí)下 Spring Boot Admin 這個(gè) Spring Boot Application UI 監(jiān)控管理工具收擦。
快速上手
Spring Boot Admin 由 Server 和 Client 兩個(gè)端組成,其中 Client 端通常是需要被監(jiān)控的應(yīng)用谍倦。先來(lái)配置下 Server 端的依賴塞赂,考慮到安全性方面的問(wèn)題,這里還額外加入了 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()
// 授予對(duì)靜態(tài)資源和登錄頁(yè)面的公共訪問(wèn)權(quán)
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
// 除上面配置的其他請(qǐng)求都必須經(jīng)過(guò)身份驗(yàn)證
.anyRequest().authenticated()
.and()
// 配置登錄和退出請(qǐng)求路徑
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
// 啟用HTTP-Basic剂跟,這是Spring Boot Admin Client注冊(cè)所必需的
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
最后减途,在啟動(dòng)類(lèi)上加入 @EnableAdminServer
,運(yùn)行服務(wù)后訪問(wèn):http://localhost:8080 曹洽,輸入用戶名密碼 admin
進(jìn)入系統(tǒng):
@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServerApplication.class, args);
}
}
Server 端到此就搭建好了鳍置,下面再來(lái)處理 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 模塊送淆,為了可以正常訪問(wèn)到 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ù)地址,可配置多個(gè)
url: http://localhost:8080
instance:
name: ${spring.application.name}
prefer-ip: true
# Spring Boot Admin Server 認(rèn)證信息
username: admin
password: admin
# 設(shè)置為true偷崩,客戶端將只注冊(cè)1個(gè) Admin Server 服務(wù)(按定義的順序)
# 當(dāng)該 Admin Server 服務(wù)宕機(jī)辟拷,將自動(dòng)注冊(cè)下個(gè) Admin Server 服務(wù)器;
# 如果為false,將針對(duì)所有管理服務(wù)器進(jìn)行注冊(cè)
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/
最后阐斜,啟動(dòng) Client 服務(wù)衫冻,我們來(lái)查看 Admin Server 提供的 UI 監(jiān)控管理系統(tǒng):
點(diǎn)擊 Instances 進(jìn)入,這時(shí)就可以方便的查看該應(yīng)用的所有監(jiān)控狀態(tài)信息谒出。
此外隅俘,Spring Boot Admin 還支持動(dòng)態(tài)更改 Logger Level、提供異常監(jiān)控告警等功能笤喳,這些姿勢(shì)可以自行解鎖为居。
參考閱讀
Spring Boot Admin
Spring Boot Admin Reference Guide
監(jiān)控管理之Spring Boot Admin使用
示例源碼
歡迎關(guān)注我的個(gè)人公眾號(hào):超級(jí)碼里奧
如果這對(duì)您有幫助,歡迎點(diǎn)贊和分享杀狡,轉(zhuǎn)載請(qǐng)注明出處