SpringBoot Actuator

SpringBoot Actuator

    • 一、簡介
    • 二、入門
      • 1、依賴
      • 2、默認監控指標
      • 3、查詢監控指標
      • 4、全量監控指標
    • 三、Spring Boot Admin
      • 1、主要功能
      • 2、Admin
      • 3、Client
      • 4、應用墻
      • 5、其他
    • 四、定制化
      • 1、定制Health端點
      • 2、定制Info端點
      • 3、定制Metrics端點
      • 4、定制Endpoint端點


一、簡介

SpringBoot自帶監控功能Actuator,可以幫助實現對程序內部運行情況監控,比如監控狀況、Bean加載情況、環境變 量、日志信息、線程信息等




二、入門

1、依賴

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

2、默認監控指標

需要啟動應用項目

訪問方式一:HTTP形式

訪問方式二:jconsole形式

在默認配置情況下,使用jconsole訪問會顯示暴露出來的其他接口


3、查詢監控指標

直接訪問對應的href地址即可

jconsole直接點擊可視化界面即可


4、全量監控指標

修改配置文件

management.endpoints.web.exposure.include=*

常用指標說明:

  • beans:Spring容器中的Bean信息
  • caches:應用中的緩存
  • health:現實健康檢查信息
  • info:顯示設置好的應用信息
  • conditions:Spring Boot的條件逐漸
  • configprops:Spring的配置類
  • env:返回當前環境屬性(Environment Properties)
  • loggers:能夠查詢和修改應用的日志級別。
  • heapdump:會構建并返回應用所用 JVM 的 Heap Dump。
  • threaddump:會 Dump 底層 JVM 的線程信息
  • metrics:詳細介紹了應用的指標。這可能包括通用指標和自定義指標。
  • scheduledtasks:提供了應用中每個計劃(定時)任務的詳細信息。
  • prometheus:返回可供Prometheus抓取的信息



三、Spring Boot Admin

Spring Boot Admin 是一個用于管理和監控 Spring Boot 應用程序的開源項目。它提供了一個簡單易用的 Web 界面,讓開發者可以方便地查看和管理多個 Spring Boot 應用實例的運行狀態、健康狀況、配置信息等。

1、主要功能

  1. 應用程序發現與注冊:Spring Boot Admin 支持通過 Spring Cloud Discovery(如 Eureka、Consul 等)自動發現注冊的 Spring Boot 應用程序,也可以通過 HTTP 進行手動注冊。
  2. 健康監控:實時顯示應用程序的健康狀態,包括磁盤空間、數據庫連接、緩存狀態等。可以通過 /actuator/health 端點獲取詳細的健康信息。
  3. 指標監控:收集和展示應用程序的各種指標,如內存使用情況、線程池狀態、HTTP 請求統計等。這些指標通過 /actuator/metrics 端點暴露。
  4. 日志管理:允許查看和管理應用程序的日志文件,支持實時日志查看和日志級別調整。可以通過 /actuator/logfile/actuator/loggers 端點進行操作。
  5. 環境與配置信息:顯示應用程序的環境變量、系統屬性和配置信息,方便開發者查看和調試。
  6. 事件與通知:當應用程序的狀態發生變化(如上線、下線、健康狀態改變等)時,Spring Boot Admin 可以發送通知,支持多種通知方式,如郵件、Slack、HipChat 等。

2、Admin

注意版本需要和項目的Spring Boot版本對齊,否則無法正常訪問

1)添加依賴

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.6.6</version>
</dependency>

2)啟動類添加@EnableAdminServer

3)訪問


3、Client

1)引入依賴

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>3.3.4</version>
</dependency>

2)配置注冊地址

## 地址是admin的訪問地址
spring.boot.admin.client.url=http://localhost:8088
management.endpoints.web.exposure.include=*

3)啟動項目

admin端會出現注冊者,這個注冊者就是我們的client端


4、應用墻

此時就可以查看到我們client端的數據指標了

當client端下線后,對應的應用塊會有顏色警示


5、其他

我們每個Client端都需要去這么配置會十分繁瑣,我們的admin端可以直接監控注冊中心,遍歷注冊中心所有的項目,獲取對應的監控數據。

而對應的client端只需要暴露對應的端點即可,即引入依賴&配置暴露端點

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
</dependencies>
management.endpoints.web.exposure.include=*



四、定制化

1、定制Health端點

  • CustomHealthIndicator 類實現了 HealthIndicator 接口。
  • health() 方法根據 isHealthy 變量的值返回不同的健康狀態。如果 isHealthy 為 true,則返回 Health.up() 表示健康;否則返回 Health.down() 表示不健康。
  • withDetail() 方法用于添加自定義的詳細信息到健康檢查結果中。
package pers.mobian.springbootactuatordemo.indicator;import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;@Component
public class CustomHealthIndicator implements HealthIndicator {private static final String CUSTOM_STATUS_KEY = "customStatus";private boolean isHealthy = false;// 模擬切換健康狀態的方法public void setHealthy(boolean healthy) {isHealthy = healthy;}@Overridepublic Health health() {if (isHealthy) {return Health.up().withDetail(CUSTOM_STATUS_KEY, "Everything is running smoothly.").build();} else {return Health.down().withDetail(CUSTOM_STATUS_KEY, "There is an issue with the custom service.").build();}}
}

此時訪問health接口,就會因為CustomHealthIndicator這個端點校驗不通過,而返回DOWN


2、定制Info端點

  • CustomInfoContributor 類實現了 InfoContributor 接口。
  • contribute 方法用于向 Info.Builder 中添加自定義信息,這里添加了當前時間和一個隨機事實。
package pers.mobian.springbootactuatordemo.indicator;import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Component
public class CustomInfoContributor implements InfoContributor {@Overridepublic void contribute(Info.Builder builder) {String currentTime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);builder.withDetail("currentTime", currentTime).withDetail("randomFact", "Spring Boot is awesome!");}
}

訪問info接口,就會出現我們添加的信息


3、定制Metrics端點

1)引入依賴

<!-- Micrometer 是 Spring Boot Actuator 底層的指標收集庫 -->
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-core</artifactId>
</dependency>

2)自定義端點

  • CustomMetricsService 類借助 MeterRegistry 創建了一個名為 custom.operation.count 的計數器。
  • incrementCustomCounter 方法用于增加計數器的值。
package pers.mobian.springbootactuatordemo.indicator;import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;@Service
public class CustomMetricsService {private final MeterRegistry meterRegistry;private Counter customCounter;public CustomMetricsService(MeterRegistry meterRegistry) {this.meterRegistry = meterRegistry;customCounter = meterRegistry.counter("custom.operation.count");}public void incrementCustomCounter() {// 每次調用該方法時,計數器加 1customCounter.increment();}
}

3)計數器累加接口

創建一個控制器來觸發自定義指標的更新

package pers.mobian.springbootactuatordemo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.mobian.springbootactuatordemo.indicator.CustomMetricsService;@RestController
public class CustomMetricsController {@Autowiredprivate CustomMetricsService customMetricsService;@GetMapping("/perform-operation")public String performOperation() {// 執行操作時,調用計數器增加方法customMetricsService.incrementCustomCounter();return "Operation performed and counter incremented.";}
}

4)瀏覽器訪問

增加計數器:http://127.0.0.1:8080/perform-operation

查詢計數指標::http://127.0.0.1:8080/actuator/metrics/custom.operation.count

5)定制指標過濾器

可以通過配置 MeterFilter`來過濾不需要的指標或者對指標進行重命名等操作。它會拒絕所有以 jvm. 開頭的指標,這樣這些指標就不會出現在 metrics 端點的響應中。

import io.micrometer.core.instrument.config.MeterFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MetricsConfig {@Beanpublic MeterFilter ignoreMetricsFilter() {return MeterFilter.denyNameStartsWith("jvm.");}
}

4、定制Endpoint端點

1)只讀customEndpoint

  • @Component:將該類注冊為 Spring Bean。
  • @Endpoint(id = “customEndpoint”):標記該類為一個 Actuator 端點,id 是端點的唯一標識符。
  • @ReadOperation:表示該方法是一個只讀操作,用于返回端點的數據
package pers.mobian.springbootactuatordemo.indicator;import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Map;@Component
@Endpoint(id = "customEndpoint")
public class CustomEndpoint {@ReadOperationpublic Map<String, String> customInfo() {Map<String, String> info = new HashMap<>();info.put("message", "This is a custom endpoint in Spring Boot 3.x.");info.put("version", "1.0");return info;}
}

2)讀寫端點advancedCustomEndpoint

package pers.mobian.springbootactuatordemo.indicator;import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.HashMap;
import java.util.Map;@Component
@Endpoint(id = "advancedCustomEndpoint")
public class AdvancedCustomEndpoint {private final Map<String, String> data = new HashMap<>();@ReadOperationpublic Map<String, String> getData() {return data;}@WriteOperation@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)public void addData(String key, String value) {data.put(key, value);}@DeleteOperationpublic void deleteData(String key) {data.remove(key);}
}

使用jconsole發送寫請求

瀏覽器訪問,查詢寫入的數據

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/72553.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/72553.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/72553.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python標識符

在 Python 中&#xff0c;標識符&#xff08;Identifier&#xff09;是指用來標識變量、函數、類、模塊等的名稱。標識符的命名規則如下&#xff1a; 1. 標識符的命名規則 字母、數字和下劃線&#xff1a;標識符可以由字母&#xff08;a-z, A-Z&#xff09;、數字&#xff08;…

06 HarmonyOS Next性能優化之LazyForEach 列表渲染基礎與實現詳解 (一)

溫馨提示&#xff1a;本篇博客的詳細代碼已發布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下載運行哦&#xff01; 目錄 一、代碼結構概覽二、詳細代碼解析1. 數據源管理實現2. 數據結構定義3. 優化的列表項組件4. 主列表組件實現 一、代碼結構概覽 本文將詳細解…

vscode 查看3d

目錄 1. vscode-3d-preview obj查看ok 2. vscode-obj-viewer 沒找到這個插件&#xff1a; 3. 3D Viewer for Vscode 查看obj失敗 1. vscode-3d-preview obj查看ok 可以查看obj 顯示過程&#xff1a;開始是綠屏&#xff0c;過了1到2秒&#xff0c;后來就正常看了。 2. vsc…

excel 斜向拆分單元格

右鍵-合并單元格 右鍵-設置單元格格式-邊框 在設置好分割線后&#xff0c;你可以開始輸入文字。 需要注意的是&#xff0c;文字并不會自動分成上下兩行。 為了達到你期望的效果&#xff0c;你可以通過 同過左對齊、上對齊 空格鍵或使用【AltEnter】組合鍵來調整單元格中內容的…

家政保潔維修行業有沒有必要做小程序?

【家政創業必看】家政行業小程序值得做嗎&#xff1f;4大核心優勢告訴你&#xff01; 隨時隨地下單&#xff1a;客戶手機一鍵預約&#xff0c;告別找電話/翻頁面的麻煩 品牌專業升級&#xff1a;精美界面服務詳情用戶評價&#xff0c;打造可信賴形象 營銷神器&#xff1…

利用Python爬蟲按圖搜索1688商品(拍立淘)

在電商領域&#xff0c;按圖搜索商品&#xff08;拍立淘&#xff09;已成為一種重要的功能&#xff0c;尤其適合用戶通過圖片快速查找相似商品。1688開放平臺提供了按圖搜索商品的API接口&#xff0c;允許開發者通過圖片獲取相關的商品信息。本文將詳細介紹如何使用Python爬蟲技…

20250305隨筆 HTML2Canvas 詳解與使用指南

1. 簡介 html2canvas 是一個用于將 HTML 頁面或特定 DOM 元素轉換為 Canvas 畫布的 JavaScript 庫。它通過解析 HTML 和 CSS&#xff0c;生成等效的 Canvas 圖像&#xff0c;從而實現網頁截圖功能。 2. 安裝 可以使用 npm 或 yarn 安裝 html2canvas&#xff0c;也可以通過 C…

【初探數據結構】鏈表OJ算法——哨兵位(合并兩個有序鏈表詳解)

文章目錄 哨兵位&#xff08;Sentinel Node&#xff09;的作用實戰演練思路講解詳細步驟1. **處理特殊情況&#xff08;邊界條件&#xff09;**2. **創建哨兵節點**3. **初始化兩個指針&#xff0c;遍歷兩個鏈表**4. **合并兩個鏈表**5. **處理剩余節點**6. **返回合并后的鏈表…

libcoap在Ubuntu下的編譯(基于CMake)

引言 libcoap 是一個開源的輕量級 C 語言庫&#xff0c;用于實現 CoAP&#xff08;Constrained Application Protocol&#xff0c;受限應用協議&#xff09;。CoAP 是一種專為資源受限設備設計的輕量級通信協議&#xff0c;適用于物聯網&#xff08;IoT&#xff09;和嵌入式系…

命名管道實現傳遞數據到二進制文件

一 前言&#xff1a; 在做項目的過程中&#xff0c;一般來說我們的信息輸入是有固定的端口/來源的&#xff0c;但是在當前的越來越快的開發節奏下&#xff0c;往往會出現輸入源還未完全確定的情況下需要我們先實現功能邏輯&#xff0c;信號接受端后面再對接。或者數據接受端和功…

VSCode知名主題帶毒 安裝量900萬次

目前微軟已經從 Visual Studio Marketplace 中刪除非常流行的主題擴展 Material Theme Free 和 Material Theme Icons&#xff0c;微軟稱這些主題擴展包含惡意代碼。 統計顯示這些擴展程序的安裝總次數近 900 萬次&#xff0c;在微軟實施刪除后現在已安裝這些擴展的開發者也會…

如何快速的解除oracle dataguard

有些時候&#xff0c;我們為了使oracle dg的standby庫另做他用&#xff0c;需要解除oracle dataguard數據同步。我本地因為standby庫存儲出現故障&#xff0c;導致dg存在問題&#xff0c;故需要解除。今天&#xff0c;我們通過使用部分命令&#xff0c;實現dg的快速解除。 1&a…

Windows系統編程(七)HotFixHook

InoolineHook需要讀寫兩次內存&#xff08;先HOOK&#xff0c;再還原&#xff09;&#xff0c;這種Hook方式&#xff0c;性能比較低&#xff0c;具有局限性。今天所講的HotFixHOOK&#xff08;熱補丁&#xff09;是InlineHook的升級版 Win32 API特殊性 Win32API的實現代碼有這…

Python Web應用開發之Flask框架——基礎

一、前言 在即將開啟的 Flask 學習之旅中,為了能夠順利掌握并運用 Flask 進行 Web 開發,您需要具備一定的基礎知識,同時了解相應的運行環境。 需要你具備的知識:Python 編程語言、HTML、CSS、HTTP協議、數據庫(如:MySQL、MongoDB) 本文所使用的環境:操作系統Windows…

TCP通訊與基于C#TCP通訊,跨窗收發消息Demo

TCP&#xff08;傳輸控制協議&#xff09;是一種面向連接的、可靠的、基于字節流的傳輸層通信協議。它廣泛應用于互聯網中的數據通信&#xff0c;如網頁瀏覽、文件傳輸、電子郵件等。以下是TCP通信的基本概念和工作原理&#xff1a; 1. TCP的特點 面向連接&#xff1a;通信前…

【有源碼】仿DeepSeek問答網站+SpringBoot+VUE3+對接DeepSeek API

今天帶來一款優秀的項目&#xff1a;仿DeepSeek問答網站。 功能和官網差不多&#xff0c;也有歷史上下文&#xff0c;流失對話等。 本文介紹了系統功能與部署安裝步驟&#xff0c;如果您有任何問題&#xff0c;也請聯系學姐&#xff0c;偶現在是經驗豐富的程序員&#xff01; …

Ubuntu20.04雙系統安裝及軟件安裝(七):Anaconda3

Ubuntu20.04雙系統安裝及軟件安裝&#xff08;七&#xff09;&#xff1a;Anaconda3 打開Anaconda官網&#xff0c;在右側處填寫郵箱&#xff08;要真實有效&#xff01;&#xff09;&#xff0c;然后Submit。會出現如圖示的Success界面。 進入填寫的郵箱&#xff0c;有一封Ana…

洛谷 P2142 高精度減法(詳解)c++

題目鏈接&#xff1a;P2142 高精度減法 - 洛谷 1.題目 2.算法原理 解法:模擬列豎式計算的過程 先用字符串讀入&#xff0c;然后拆分每一位&#xff0c;逆序放進數組中利用數組&#xff0c;模擬列豎式減法的過程 在這兩步之前要多加一步&#xff0c;在模擬解法的過程&#…

在 MyBatis 中,若數據庫字段名與 SQL 保留字沖突解決辦法

在 MyBatis 中&#xff0c;若數據庫字段名與 SQL 保留字沖突&#xff0c;可通過以下方法解決&#xff1a; 目錄 一、使用轉義符號包裹字段名二、通過別名映射三、借助 MyBatis-Plus 注解四、全局配置策略&#xff08;輔助方案&#xff09;最佳實踐與注意事項 一、使用轉義符號…

ThreadLocal解析

1. ThreadLocal的定義與核心作用 ThreadLocal是Java中用于實現線程局部變量的工具類。它為每個線程提供獨立的變量副本&#xff0c;使得每個線程訪問的是自己的數據&#xff0c;從而避免多線程環境下的資源共享問題&#xff0c;實現線程隔離。 例如&#xff0c;解決SimpleDate…