SpringBoot監(jiān)控模塊Actuator的用法詳解
1. Actuator 的入門及配置
1.1 安裝和使用 Actuator
我們可以通過 Maven 或 Gradle 將 Actuator 添加到我們的 Spring Boot 應(yīng)用程序中。Maven 的依賴如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Gradle 的依賴如下:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
添加依賴后,我們只需要啟動我們的應(yīng)用程序即可開始使用 Actuator。默認(rèn)情況下,Actuator 會向應(yīng)用程序暴露以下 URI:
/actuator/auditevents:顯示當(dāng)前配置的審核事件信息/actuator/beans:顯示應(yīng)用程序中所有 Bean 的完整名稱列表/actuator/conditions:顯示自動配置類及其應(yīng)用情況的信息/actuator/configprops:顯示所有 Spring Boot 配置文件及其對應(yīng)值的詳細(xì)信息/actuator/env:顯示應(yīng)用程序當(dāng)前環(huán)境變量和屬性的詳細(xì)信息/actuator/flyway:顯示 Flyway 數(shù)據(jù)庫遷移信息的詳細(xì)信息/actuator/health:顯示應(yīng)用程序的健康狀況/actuator/info:顯示應(yīng)用程序相關(guān)信息/actuator/mappings:顯示 Spring MVC 控制器映射的詳細(xì)信息/actuator/metrics:顯示各種 JVM 指標(biāo)、計(jì)數(shù)器和度量數(shù)據(jù)的詳細(xì)信息/actuator/prometheus:將指標(biāo)暴露為 Prometheus 格式,以便在 Grafana 或其他監(jiān)控系統(tǒng)中使用/actuator/scheduledtasks:顯示應(yīng)用程序中所有調(diào)度任務(wù)的詳細(xì)信息/actuator/sessions:允許查看和失效會話 ID/actuator/shutdown:關(guān)閉應(yīng)用程序(需要加入 actuator/shutdown 配置)
1.2 Actuator 的基本配置
Actuator 默認(rèn)是開啟的,并暴露所有端點(diǎn),我們甚至可以在瀏覽器中直接訪問上面提到的 URI。但是,在生產(chǎn)環(huán)境中,我們通常不希望所有端點(diǎn)都對外暴露。這時(shí),我們需要對 Actuator 進(jìn)行一些配置。
在 application.properties 或 application.yml 文件中添加如下配置:
# 關(guān)閉具體某個(gè)端點(diǎn),格式為management.endpoint.<id>.enabled=false management.endpoint.shutdown.enabled=true # 開啟所有端點(diǎn)的基礎(chǔ)功能,格式為management.endpoints.<id>.<option-name> management.endpoints.web.exposure.include=*
上述配置將關(guān)閉 shutdown 端點(diǎn),并開放所有端點(diǎn)。除了以上參量,還可以通過 management.server.port 配置 Actuator 的 HTTP 端口和 management.server.ssl 配置 SSL。
2. Actuator 的端點(diǎn)
Actuator 中各個(gè)端點(diǎn)描述如下:
2.1 /actuator/health
該端點(diǎn)提供有關(guān)應(yīng)用程序健康狀況的詳細(xì)信息。通過檢查數(shù)據(jù)庫、緩存、消息代理等檢查資源的狀態(tài),Health Indicators 能夠確定應(yīng)用程序是否正常運(yùn)行。默認(rèn)情況下,該端點(diǎn)顯示以下信息:
{
"status": "UP"
}如果應(yīng)用程序是正常運(yùn)行的,它會返回 UP,否則將返回 DOWN,并向用戶提供有關(guān)錯(cuò)誤的詳細(xì)信息。
2.2 /actuator/info
該端點(diǎn)展示與應(yīng)用程序相關(guān)的任何附加信息。如:版本,構(gòu)建時(shí)間等。在應(yīng)用程序訪問時(shí),返回一個(gè)自定義 JSON 格式的響應(yīng)。可以通過在 application.yml 或 application.properties 文件中添加鍵值對來定制信息。
例如:
info.app.name=My Application info.app.description=A demo application info.app.version=1.0.0
訪問該端點(diǎn),將返回如下響應(yīng):
{
"app": {
"description": "A demo application",
"name": "My Application",
"version": "1.0.0"
}
}2.3 /actuator/metrics
該端點(diǎn)提供了可以收集和監(jiān)控的度量指標(biāo)的詳細(xì)信息。Spring Boot 提供了一些默認(rèn)的度量指標(biāo),如系統(tǒng) CPU 使用率、內(nèi)存使用情況、HTTP 請求延遲等。此外,Spring Boot 還支持使用 Micrometer 框架創(chuàng)建自定義的度量指標(biāo)。
在 /actuator/metrics 端點(diǎn)下,有許多子路徑,每個(gè)子路徑中包含一些度量項(xiàng)。例如,我們可以查看 Tomcat 正在監(jiān)聽的連接數(shù)和活動線程數(shù)等:
{
"connections": {
"active": 13,
"max": 1000,
"total": 29
},
"threads": {
"daemon": 7,
"total": 22
}
}2.4 /actuator/beans
該端點(diǎn)提供了應(yīng)用程序所有 Bean 的完整列表,包括它們的相關(guān)信息。需要注意的是,Actuator 只顯示由 Spring IOC 容器管理的 Bean 信息。
2.5 /actuator/env
該端點(diǎn)展示應(yīng)用程序當(dāng)前環(huán)境變量和屬性的詳細(xì)信息??梢允褂?/actuator/env/<key> 端點(diǎn)獲取指定 key 的值,也可以通過 POST 請求,修改或添加屬性。
2.6 /actuator/mappings
該端點(diǎn)提供了所有 Spring MVC 控制器映射的詳細(xì)信息,包括請求 URL、處理器方法、HTTP 方法等等,并按照優(yōu)先級遞減的順序進(jìn)行排序。例如,如下代碼:
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/world")
public String greeting() {
return "Hello, world!";
}
}訪問 /actuator/mappings ,將返回:
{
"contexts": {
"application": {
"mappings": {
"dispatcherServlets": {
"dispatcherServlet": {
"mvcHandlerMappings": {
"/hello": {
"methods": [],
"params": {},
"headers": {},
"consumes": [],
"produces": [],
"custom": {}
},
"/hello/world": {
"bean": "helloController",
"method": "greeting",
"beanType": "com.example.demo.HelloController",
"args": [],
"responseDetails": {
"status": 200,
"headers": {},
"content": {
"id": "1",
"name": "Tom"
}
},
"methods": [
"GET"
],
"params": {},
"headers": {},
"consumes": [],
"produces": [],
"custom": {}
},
...
}
}
}
}
}
}
}2.7 /actuator/auditevents
該端點(diǎn)提供了所有 Spring Boot 應(yīng)用程序所執(zhí)行的審核事件的詳細(xì)信息,包括事件名稱、類型、時(shí)間戳和調(diào)用者等。此外,還支持通過 Spring Security 的安全審核事件等。
2.8 /actuator/flyway
該端點(diǎn)展示 Flyway 數(shù)據(jù)庫遷移信息的詳細(xì)信息,包括已應(yīng)用遷移的版本和狀態(tài)等。
2.9 /actuator/scheduledtasks
該端點(diǎn)展示應(yīng)用程序中所有調(diào)度任務(wù)的詳細(xì)信息。
2.10 /actuator/sessions
該端點(diǎn)允許查看和失效會話 ID。
3. 自定義 Actuator 端點(diǎn)
除了使用 Actuator 默認(rèn)端點(diǎn)之外,我們還可以根據(jù)自己的業(yè)務(wù)需求自定義 Actuator 端點(diǎn)。
3.1 編寫自定義 Endpoints
自定義 Actuator 端點(diǎn)需要實(shí)現(xiàn) Endpoint 接口,并重寫 getId() 和 invoke() 方法。例如,以下代碼實(shí)現(xiàn)了一個(gè)名為 MyEndpoint 的自定義端點(diǎn):
@Component
public class MyEndpoint implements Endpoint<Map<String, Object>> {
@Override
public String getId() {
return "my-endpoint";
}
@Override
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>();
result.put("name", "My name is Actuator");
return result;
}
}3.2 暴露自定義 Endpoints
實(shí)現(xiàn)自定義 Endpoints 后,我們需要通過設(shè)置來公開它們。在 application.yml 中添加以下內(nèi)容:
management:
endpoints:
web:
exposure:
include: my-endpoint這將允許我們通過訪問 /actuator/my-endpoint 端點(diǎn)來查看自定義的端點(diǎn)信息。
4. Actuator 的監(jiān)控和管理功能
4.1 配置度量指標(biāo)
Actuator 提供了很多度量指標(biāo),但默認(rèn)只包含一些 JVM 層面的指標(biāo)。如果我們需要檢測自己應(yīng)用程序的度量指標(biāo),那么可以使用 Micrometer 庫來幫助我們完成。它是一個(gè)跨度量系統(tǒng)和時(shí)間序列數(shù)據(jù)庫的工具庫,支持多種后端存儲如 Prometheus、Graphite 和 InfluxDB。使用 Micrometer,我們可以在 Actuator 中創(chuàng)建自定義度量指標(biāo):
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;
@Component
public class CustomMetrics {
private final MeterRegistry meterRegistry;
private final Counter requestsCounter;
public CustomMetrics(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
this.requestsCounter = Counter.builder("custom_requests_counter")
.description("A counter for counting custom requests.")
.register(meterRegistry);
}
public void incrementRequests() {
requestsCounter.increment();
}
}在上述代碼中,我們創(chuàng)建了一個(gè)名為 CustomMetrics 的類,它是一個(gè) Spring bean,并使用 MeterRegistry 注冊了一個(gè)名為 custom_requests_counter 的度量計(jì)數(shù)器。此外,還定義了一個(gè) incrementRequests() 方法,以便我們可以在代碼中增加請求計(jì)數(shù)器。我們還可以添加其他類型的指標(biāo),如 Gauge、Timer 和 DistributionSummary。
4.2 監(jiān)控和管理應(yīng)用程序
Actuator 還提供了一些有用的監(jiān)控和管理功能,讓我們可以更方便地跟蹤和管理應(yīng)用程序。
4.2.1 健康檢查
Actuator 的 /actuator/health 端點(diǎn)提供了應(yīng)用程序的健康狀況,我們可以通過檢查該端點(diǎn)來確定應(yīng)用程序是否正常運(yùn)行。如果應(yīng)用程序出現(xiàn)故障,該端點(diǎn)將顯示 DOWN 狀態(tài),并提供有關(guān)錯(cuò)誤的詳細(xì)信息。
4.2.2 角色和權(quán)限
Actuator 支持基于角色的訪問控制,可以限制某些用戶或組的訪問??梢酝ㄟ^設(shè)置 management.endpoint.<id>.roles 屬性配置端點(diǎn)的角色。例如,以下代碼將 /actuator/shutdown 端點(diǎn)限制為 ADMIN 角色:
management.endpoint.shutdown.roles=ADMIN
4.2.3 外部配置
Actuator 可以通過外部配置文件進(jìn)行配置。可以在外部配置文件中設(shè)置以下鍵來控制 Actuator 的行為:
management.endpoints.enabled-by-default:是否啟用所有端點(diǎn)(默認(rèn)為 true)management.endpoints.jmx.exposure.exclude:要排除的端點(diǎn) JMX MBean 名稱management.endpoint.<id>.enabled:指定是否啟用特定端點(diǎn)management.endpoint.<id>.sensitive:指定特定端點(diǎn)是否包含敏感信息
4.2.4 采樣周期
Actuator 還支持將某些端點(diǎn)的采樣周期設(shè)置為非默認(rèn)值,以在一定時(shí)間間隔內(nèi)定期輪詢信息??梢酝ㄟ^ management.endpoint.<id>.time-to-live 屬性配置采樣周期。例如,以下代碼將 /actuator/metrics 端點(diǎn)的采樣周期設(shè)置為 30 秒:
management.endpoint.metrics.time-to-live=30s
5. Actuator 的安全保護(hù)
在生產(chǎn)環(huán)境中,Actuator 的安全保護(hù)非常重要。Actuator 支持基于 Spring Security 的安全保護(hù),可以使用 Spring Security 配置來授權(quán) Actuator 端點(diǎn)的訪問。
5.1 添加 Spring Security 依賴
要使用 Spring Security,我們需要在項(xiàng)目中添加以下 Maven 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>5.2 配置 Spring Security
完成依賴添加后,我們需要配置 Spring Security,以保護(hù) Actuator 端點(diǎn)。以下是一個(gè)簡單的 Spring Security 配置示例:
@Configuration
@EnableWebSecurity
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(EndpointRequest.to("health", "info")).permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
.and().httpBasic();
}
}在上述代碼中,我們創(chuàng)建了一個(gè)名為 ActuatorSecurityConfig 的 Spring Security 配置類,并配置了 /actuator/health 和 /actuator/info 端點(diǎn)的公開訪問權(quán)限。對于其他端點(diǎn),則需要用戶具有 ACTUATOR 角色才能訪問。此外,還啟用了 HTTP 基本身份驗(yàn)證,以便在調(diào)用受保護(hù)的端點(diǎn)時(shí)要求用戶進(jìn)行身份驗(yàn)證。
5.3 添加用戶和角色
完成 Spring Security 配置后,我們還需要為 Actuator 添加用戶和角色??梢酝ㄟ^在 application.properties 或 application.yml 文件中設(shè)置以下鍵來配置用戶名、密碼和角色:
spring.security.user.name=actuator spring.security.user.password=actuator-password spring.security.user.roles=ACTUATOR
在上述配置中,我們創(chuàng)建了一個(gè)名為 actuator、密碼為 actuator-password,且擁有 ACTUATOR 角色的用戶。
以上就是SpringBoot監(jiān)控模塊Actuator的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot監(jiān)控模塊Actuator的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot應(yīng)用監(jiān)控Actuator使用隱患及解決方案
- SpringBoot+actuator和admin-UI實(shí)現(xiàn)監(jiān)控中心方式
- SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法
- Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
- SpringBoot中的Actuator詳解
- SpringBoot Actuator未授權(quán)訪問漏洞解決方案
- 關(guān)于SpringBoot Actuator漏洞補(bǔ)救方案
- SpringBoot Actuator未授權(quán)訪問漏洞修復(fù)詳解
- Spring Boot Actuator入門指南
相關(guān)文章
關(guān)于IDEA無法預(yù)覽Markdown文件的解決思路
在IntelliJ IDEA中,有時(shí)Markdown文件無法預(yù)覽可能是因?yàn)槲募P(guān)聯(lián)設(shè)置不正確或配置信息錯(cuò)誤,首先,檢查IDE的File Types設(shè)置,確保.md和.markdown后綴已正確注冊,其次,對照官方配置信息,調(diào)整Markdown設(shè)置2024-09-09
java 在Jetty9中使用HttpSessionListener和Filter
這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關(guān)資料,需要的朋友可以參考下2017-06-06
SpringBoot結(jié)合mockito測試實(shí)戰(zhàn)
與集成測試將系統(tǒng)作為一個(gè)整體測試不同,單元測試更應(yīng)該專注于某個(gè)類。所以當(dāng)被測試類與外部類有依賴的時(shí)候,尤其是與數(shù)據(jù)庫相關(guān)的這種費(fèi)時(shí)且有狀態(tài)的類,很難做單元測試。但好在可以通過“Mockito”這種仿真框架來模擬這些比較費(fèi)時(shí)的類,從而專注于測試某個(gè)類內(nèi)部的邏輯2022-11-11
java.sql.SQLException:com.mysql.cj.jdbc.Driver報(bào)錯(cuò)問題解決
這篇文章主要給大家介紹了關(guān)于java.sql.SQLException:com.mysql.cj.jdbc.Driver報(bào)錯(cuò)問題解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
springboot項(xiàng)目以jar包運(yùn)行的操作方法
公司一個(gè)springboot項(xiàng)目本來是打war包的,突然要改為打jar包,不知所措了,糾結(jié)該如何操作呢,折騰半天終于搞定了,下面把解決方案分享給大家,對springboot打jar包方式感興趣的朋友一起看看吧2021-06-06

