Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn)
一 前言
springboot 額外的特色是提供了后臺(tái)應(yīng)用監(jiān)控,可以通過(guò) HTTP 或者 JMX的方式管理監(jiān)控應(yīng)用,本文主講HTTP方式;其主要的功能是監(jiān)控應(yīng)用的健康狀態(tài),查看環(huán)境變量等;
二 pom.xml
springboot 2.1.1,主要引入 actuator 依賴,web依賴用于測(cè)試;
<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>
三 默認(rèn)開(kāi)啟端點(diǎn)
3.1 默認(rèn)端點(diǎn) health
直接編寫(xiě)主程序入口,啟動(dòng);瀏覽器輸入 http://localhost:8080/actuator/health;結(jié)果如下,狀態(tài)是UP;
翻翻源碼heath狀態(tài)碼如下
public OrderedHealthAggregator() { this.setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN); }
- DOWN 服務(wù)無(wú)法獲得,狀態(tài)碼503;
- .OUT_OF_SERVICE 服務(wù)無(wú)法獲得,狀態(tài)碼503;
- UP 獲得服務(wù),狀態(tài)碼200;
- UNKNOWN 獲得未知服務(wù),狀態(tài)碼200;
在 application.yml 中配置 healthy 信息 示例如下:
management: endpoint: health: show-details: always
打印詳細(xì)信息:
基本配置如下:
never :默認(rèn),表示不顯示詳細(xì)信息;when-authorized:詳細(xì)信息顯示給 認(rèn)證過(guò)的用戶;使用
management.endpoint.health.roles 配置always: 顯示詳細(xì)信息給所有用戶3.2 默認(rèn)端點(diǎn) info
瀏覽器輸入 http://localhost:8080/actuator/info; 展示空信息如下圖:
在application.yml 中 配置工程 info 信息 示例如下;
#配置信息info: actuator: name: springboot-actutor version: 1.0.0 author: zszxz
展示結(jié)果如下:
四 HTTP端點(diǎn)說(shuō)明
端點(diǎn) | 端點(diǎn)描述 | 默認(rèn)值 |
---|---|---|
auditevents | 當(dāng)前應(yīng)用的審計(jì)事件 | Yes |
beans | 顯示spring IOC 容器加載的所有bean | Yes |
caches | 顯示可獲得的緩存 | Yes |
conditions | 顯示自動(dòng)配置通過(guò)condition判斷匹配或者不匹配的配置信息 | Yes |
configprops | 顯示 通過(guò) @ConfigurationProperties 配置的屬性信息 | Yes |
env | spring環(huán)境變量屬性信息 | Yes |
flyway | 顯示flyway 配置數(shù)據(jù)庫(kù)已經(jīng)遷移的信息 | Yes |
health | 顯示應(yīng)用的健康信息 | Yes |
httptrace | 顯示 HTTP 軌跡信息默認(rèn)最新的100 HTTP request或response | Yes |
info | 顯示自定義的應(yīng)用信息 | Yes |
integrationgraph | 顯示spring 整合 graph 信息 | Yes |
loggers | 顯示配置文件中日志修改信息 | Yes |
liquibase | 顯示 任意的 Liquibase 數(shù)據(jù)庫(kù)已經(jīng)遷移的信息 | Yes |
metrics | 顯示當(dāng)前應(yīng)用的指標(biāo) | Yes |
mappings | 顯示 @RequestMapping paths. 配置的路徑信息 | Yes |
scheduledtasks | 顯示任務(wù)調(diào)度信息 | Yes |
sessions | 刪除或者恢復(fù)Spring Session會(huì)話,不支持web響應(yīng)式編程 | Yes |
shutdown | 關(guān)閉應(yīng)用 | No |
threaddump | 執(zhí)行一個(gè)線程轉(zhuǎn)儲(chǔ) | Yes |
五 配置開(kāi)啟端點(diǎn)
application.yml 中配置需要開(kāi)啟的端點(diǎn),其中 * 表示開(kāi)啟所有端點(diǎn),示例如下:
management: endpoints: web: exposure: # 使用通配符 * 表示匹配所有端點(diǎn) # 排除的端點(diǎn) exclude: caches # 包括的端點(diǎn) include: info,health,beans,env,shutdown,threaddump
5.1 threaddump示例
http://localhost:8080/actuator/threaddump ;用于返回線程快照,分析線程阻塞,死鎖等,部分內(nèi)容如下
{ "threads": [{ "threadName": "DestroyJavaVM", "threadId": 41, "blockedTime": -1, "blockedCount": 0, "waitedTime": -1, "waitedCount": 0, "lockName": null, "lockOwnerId": -1, "lockOwnerName": null, "inNative": false, "suspended": false, "threadState": "RUNNABLE", "stackTrace": [], "lockedMonitors": [], "lockedSynchronizers": [], "lockInfo": null }
5.2 beans示例
http://localhost:8080/actuator/beans ; 用于返回 spring 容器加載的所有bean,部分內(nèi)容如下;
{ "contexts": { "application": { "beans": { "endpointCachingOperationInvokerAdvisor": { "aliases": [], "scope": "singleton", "type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor", "resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]", "dependencies": ["environment"] }, "defaultServletHandlerMapping": { "aliases": [], "scope": "singleton", "type": "org.springframework.web.servlet.HandlerMapping", "resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]", "dependencies": [] }
5.3 關(guān)閉應(yīng)用示例
普通情況下是沒(méi)有開(kāi)啟這個(gè)配置,是比較危險(xiǎn)的動(dòng)作,會(huì)導(dǎo)致應(yīng)用停止;修改application.yml配置如下
management: endpoints: web: exposure: # 使用通配符 * 表示匹配所有端點(diǎn) # 排除的端點(diǎn) exclude: caches # 包括的端點(diǎn) include: info,health,beans,env,shutdown endpoint: health: show-details: always # 開(kāi)啟關(guān)閉應(yīng)用 需要post請(qǐng)求 shutdown: enabled: true
訪問(wèn)地址 http://localhost:8080/actuator/shutdown; 注意僅支持使用POST請(qǐng)求,否則 會(huì) 405錯(cuò)誤;
六 CORS 支持
application.yml 修改配置如下, allowed-origins 中允許跨域的ip地址; allowed-methods 配置 允許通過(guò)的請(qǐng)求,還有支持時(shí)間等;
management: endpoints: web: exposure: # 使用通配符 * 表示匹配所有端點(diǎn) # 排除的端點(diǎn) exclude: caches # 包括的端點(diǎn) include: info,health,beans,env,shutdown # 跨域處理 cors: allowed-origins: http://localhost:8080/ allowed-methods: post,delete,get,put endpoint: health: show-details: always # 開(kāi)啟關(guān)閉應(yīng)用 需要post請(qǐng)求 shutdown: enabled: true
七 修改默認(rèn)路徑
在 配置文件中添加 base-path , 會(huì)修改掉默認(rèn)路徑 actuator/endpoint;
management: endpoints: web: exposure: # 使用通配符 * 表示匹配所有端點(diǎn) # 排除的端點(diǎn) exclude: caches # 包括的端點(diǎn) include: info,health,beans,env,shutdown # 自定義配置監(jiān)控路徑 base-path: /zszxz # 跨域處理 cors: allowed-origins: http://localhost:8080/ allowed-methods: post,delete,get,put endpoint: health: show-details: always # 開(kāi)啟關(guān)閉應(yīng)用 需要post請(qǐng)求 shutdown: enabled: true
示例url: http://localhost:8080/zszxz/info
結(jié)果如下
八 其他配置說(shuō)明
還可以引入 security 依賴 配置 賬號(hào)密碼,角色信息,達(dá)到訪問(wèn)控制,詳細(xì)的可以參照官網(wǎng);
還可以使用注解進(jìn)行配置,自定義端點(diǎn),詳細(xì)參照官網(wǎng);
jmx支持,可以使用open jdk 自帶的工具 jconsole 進(jìn)行監(jiān)控;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot集成Actuator監(jiān)控功能詳解
- SpringBoot監(jiān)控模塊Actuator的用法詳解
- SpringBoot2開(kāi)啟Actuator端點(diǎn)監(jiān)控的方法
- SpringBoot 指標(biāo)監(jiān)控actuator的專題
- springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解
- 基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式
- 使用SpringBoot Actuator監(jiān)控應(yīng)用示例
- springboot 使用Spring Boot Actuator監(jiān)控應(yīng)用小結(jié)
- 詳解關(guān)于springboot-actuator監(jiān)控的401無(wú)權(quán)限訪問(wèn)
- SpringBoot Actuator監(jiān)控的項(xiàng)目實(shí)踐
相關(guān)文章
IDEA+Maven創(chuàng)建Spring項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了IDEA+Maven創(chuàng)建Spring項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解
這篇文章主要為大家介紹了AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10spring cloud gateway全局過(guò)濾器實(shí)現(xiàn)向request header中放數(shù)據(jù)
這篇文章主要介紹了spring cloud gateway全局過(guò)濾器實(shí)現(xiàn)向request header中放數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java中通過(guò)三級(jí)緩存解決Spring循環(huán)依賴詳解
這篇文章主要介紹了Java中通過(guò)三級(jí)緩存解決Spring循環(huán)依賴詳解,當(dāng)出現(xiàn)兩個(gè)或多個(gè) Bean 在初始化時(shí)相互依賴的情況時(shí),Spring Boot 會(huì)將其中一個(gè) Bean 提前暴露出來(lái),以便其他 Bean 能夠在初始化時(shí)正確地引用它,這一策略能有效避免循環(huán)依賴導(dǎo)致的問(wèn)題,需要的朋友可以參考下2023-09-09Java使用策略模式解決商場(chǎng)促銷商品問(wèn)題示例
這篇文章主要介紹了Java使用策略模式解決商場(chǎng)促銷商品問(wèn)題,簡(jiǎn)單描述了策略模式的概念、原理,并結(jié)合實(shí)例形式分析了Java基于策略模式解決商品促銷問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Java存儲(chǔ)過(guò)程調(diào)用CallableStatement的方法
這篇文章主要介紹了Java存儲(chǔ)過(guò)程調(diào)用CallableStatement的方法,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-11-11