使用?Spring?Boot?Admin?監(jiān)控應(yīng)用狀態(tài)的詳細(xì)過程
pring Boot Actuator 是 Spring Boot 提供的對應(yīng)用的自省和監(jiān)控功能,如健康檢查,審計,指標(biāo)收集,HTTP 跟蹤等,可以幫助開發(fā)和運(yùn)維人員監(jiān)控和管理 Spring Boot 應(yīng)用。該模塊采集應(yīng)用的內(nèi)部信息,并暴露給外部的模塊,支持 HTTP 和 JMX,并可以與一些第三方監(jiān)控系統(tǒng)(如 Prometheus)整合。
程序員優(yōu)雅哥 SpringBoot 2.7 實(shí)戰(zhàn)基礎(chǔ) - 11 - 使用 Spring Boot Admin 監(jiān)控應(yīng)用狀態(tài)
1 Spring Boot Actuator
Spring Boot Actuator 是 Spring Boot 提供的對應(yīng)用的自省和監(jiān)控功能,如健康檢查,審計,指標(biāo)收集,HTTP 跟蹤等,可以幫助開發(fā)和運(yùn)維人員監(jiān)控和管理 Spring Boot 應(yīng)用。該模塊采集應(yīng)用的內(nèi)部信息,并暴露給外部的模塊,支持 HTTP 和 JMX,并可以與一些第三方監(jiān)控系統(tǒng)(如 Prometheus)整合。
1.1 Actuator endpoint
端點(diǎn) Endpoint 是 Actuator 的核心組成部分,用來監(jiān)視應(yīng)用程序的各種狀態(tài)。 Spring Boot Actuator 內(nèi)置很多 Endpoint,總體上看分成三類:
應(yīng)用配置類:主要包括配置信息、Spring Bean 的信息、配置文件信息、環(huán)境信息等;度量指標(biāo)類:應(yīng)用在運(yùn)行期間的信息,包括堆棧、健康狀態(tài)、線程池信息、HTTP請求統(tǒng)計等;操作控制類:如 shutdown,提供了對應(yīng)用的關(guān)閉等操作類功能。
1.2 添加依賴
在 pom.xml 中添加 Actuator 的 starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>1.3 訪問端點(diǎn)
添加依賴后,啟動服務(wù),可通過如下請求查看暴露的端點(diǎn):
http://localhost:9099/actuator
該請求返回:
{
"_links": {
"self": {
"href": "http://localhost:9099/actuator",
"templated": false
},
"health-path": {
"href": "http://localhost:9099/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:9099/actuator/health",
"templated": false
}
}
}從返回的結(jié)果可以看出默認(rèn)只開放了 /actuator/health 端點(diǎn)。訪問該端點(diǎn) http://localhost:9099/actuator/health:
{"status":"UP"}其他未開放的端點(diǎn)可以獨(dú)立配置開啟或禁用。
在 application.yml 中添加如下配置,開放所有的端點(diǎn),并顯示詳細(xì)的 health:
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always重啟服務(wù),再次查看暴露的端點(diǎn),可以看出有如下端點(diǎn):

2 Spring Boot Admin
Spring Boot Actuator 提供了各種端點(diǎn),Spring Boot Admin 能夠?qū)?Actuator 中的信息進(jìn)行界面化的展示,并提供實(shí)時報警功能。
在微服務(wù)環(huán)境中,使用 Spring Boot Admin,通常包括服務(wù)端和客戶端,服務(wù)端只運(yùn)行 Spring Boot Admin Server,收集各個客戶端的數(shù)據(jù),并以可視化界面顯示出來??蛻舳诉\(yùn)行 Spring Boot Admin Client,或者通過服務(wù)發(fā)現(xiàn)與注冊獲取應(yīng)用的信息。
這里的 demo 我就不在 Spring Boot Admin Server了,將當(dāng)前 hero-springboot-demo 既作為 server、也作為 client 使用。在后面的實(shí)戰(zhàn)篇章中會獨(dú)立 Admin Server,同時客戶端也不使用 client,而是通過服務(wù)注冊與發(fā)現(xiàn)。
2.1 添加依賴
在 pom.xml 中添加 Spring Boot Admin Server 的依賴:
<!-- 實(shí)戰(zhàn)中該依賴只在獨(dú)立的 Admin Server 中使用,此處僅為測試 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.4</version>
</dependency>
<!-- 實(shí)戰(zhàn)中客戶端也不需要添加該依賴,而是通過服務(wù)發(fā)現(xiàn)與注冊,此處僅為測試 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.4</version>
</dependency>需要注意版本號,由于 Spring Boot 版本使用的是 2.7.x,Spring Boot Admin Server 的版本也要用 2.7.x,千萬別亂搞!
2.2 開啟 Admin Server
在啟動類 DemoApplication 上添加注解 @EnableAdminServer 開啟 Spring Boot Admin Server。
@EnableAdminServer
@EnableAsync
@MapperScan("com.yygnb.demo.mapper")
@SpringBootApplication
public class DemoApplication {
...
}2.3 配置客戶端
在 application.yml 中添加如下配置:
- 配置 Admin Server 的 context-path;
- 為客戶端配置 Admin Server 的地址。
spring:
application:
name: hero-springboot-demo
boot:
admin:
client:
url: 'http://localhost:9099/monitor'
context-path: '/monitor'2.4 訪問 Admin Server
重啟服務(wù),在瀏覽器中訪問 Spring Boot Admin Server:
可以看到當(dāng)前應(yīng)用的作為客戶端注冊到 Admin Server 上:

再次強(qiáng)調(diào),上述操作僅僅針對demo學(xué)習(xí),非真實(shí)的企業(yè)級開發(fā)!
3 自定義告警
當(dāng)應(yīng)用狀態(tài)異常時,Spring Boot Admin 會自動實(shí)時告警,而告警的方式可以由我們自定義。這里模擬日志的方式。
在 config 包下創(chuàng)建類 DemoNotifier,該類繼承自 AbstractEventNotifier:
@Slf4j
@Component
public class DemoNotifier extends AbstractEventNotifier {
protected DemoNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
return Mono.fromRunnable(() -> log.error("Instance info: {}, {}, {}",
instance.getRegistration().getName(), event.getInstance(),
event.getType()));
}
}此時,注冊到這個Admin Server的其他客戶端啟動、停止等,當(dāng)前應(yīng)用都會監(jiān)聽到事件,輸出日志。實(shí)戰(zhàn)中可以在這里面發(fā)送郵件、消息等。
4 登錄訪問
上面配置的 Admin Server 無需登錄就可以訪問,在真實(shí)開發(fā)中需要登錄后才能訪問。admin server 也提供了登錄頁面。
4.1 添加依賴
在 pom.xml 添加 Spring Security 的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>4.2 配置用戶名密碼
在 application.yml 中配置登錄的用戶名和密碼:
spring:
application:
name: hero-springboot-demo
boot:
admin:
client:
url: 'http://localhost:9099/monitor'
context-path: '/monitor'
security:
user:
name: admin
password: 111111上面的配置在之前的基礎(chǔ)上增加了:spring.security.user 的配置。
4.3 添加配置類
在 config 包下添加 Spring Security 的配置類 SecurityConfig:
@Configuration
public class SecurityConfig {
private final String adminContextPath;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler =
new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
return http.authorizeHttpRequests(auth -> auth.antMatchers(
adminContextPath + "/assets/**",
adminContextPath + "/login",
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
).permitAll()
.antMatchers(adminContextPath + "/**").authenticated()
.anyRequest().permitAll()
).formLogin(form -> form.loginPage(adminContextPath + "/login")
.successHandler(successHandler)
).logout(logout -> logout.logoutUrl(adminContextPath + "/logout"))
.csrf(AbstractHttpConfigurer::disable)
.build();
}
}上面配置文件中的 adminContextPath 就是前面配置的 spring.boot.admin.context-path,即 /monitor。
上面配置包括幾個部分:
- 僅對路徑 /monitor/** 請求權(quán)限控制;
- 登錄頁面和登錄成功后的默認(rèn)地址;
- 表單登錄配置;
- 禁用 CSRF。
4.4 測試運(yùn)行
重啟服務(wù),訪問之前開發(fā)的 computer 等接口,可以正常訪問;如果訪問 /monitor 等路徑,就會跳轉(zhuǎn) Spring Boot Admin 提供的登錄頁:

使用配置的用戶名密碼(admin/111111)登錄,登錄成功后進(jìn)入 Admin Server 頁面。
感謝你閱讀本文,如果本文給了你一點(diǎn)點(diǎn)幫助或者啟發(fā),還請三連支持一下,點(diǎn)贊、關(guān)注、收藏,作者會持續(xù)與大家分享更多干貨
相關(guān)文章
SpringBoot?@Configuration與@Bean注解使用介紹
這篇文章主要介紹了SpringBoot中的@Configuration與@Bean注解,在進(jìn)行項目編寫前,我們還需要知道一個東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們在之后使用才會更加得心應(yīng)手2022-10-10
IntelliJ IDEA像Eclipse一樣打開多個項目的圖文教程
這篇文章主要介紹了IntelliJ IDEA像Eclipse一樣打開多個項目的方法圖文教程講解,需要的朋友可以參考下2018-03-03
Java 處理超大數(shù)類型之BigInteger案例詳解
這篇文章主要介紹了Java 處理超大數(shù)類型之BigInteger案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

