1. 程式人生 > >SpringBoot第二十二篇:應用監控之Actuator

SpringBoot第二十二篇:應用監控之Actuator

作者:追夢1819
原文:https://www.cnblogs.com/yanfei1819/p/11226397.html
版權宣告:本文為博主原創文章,轉載請附上博文連結!

引言

  很多文章都將 SpringBoot Actuator 的 Endpoint 翻譯為 "端點"。不過我認為這這翻譯失去了原有的意思。故本文中的 endpoint 依舊是 endpoint,不翻譯為"端點"。

  通過引入 spring-boot-starter-actuator ,可以使用 SpringBoot 為我們提供的準生產環境下的應用監控和管理功能。我們可以通過 HTTP、JMX、SSH協議進行操作。自動得到審計、監控和指標操作。


步驟:

  1. 引入maven依賴;
  2. 通過 HTTP 方式訪問監控端點;
  3. 可進行 shutdown(post提交,此端點預設關閉)。


原生endpoint

  SpringBoot 的 Actuator 有很多原生的端點,詳細檢視官網。Spring Boot 2.0 中的端點和之前的版本有較大不同,使用時需注意。啟動時不是可以直接訪問,需要先將其暴露出來。

本文中,我們講述幾個常用的端點。

  1. health

    主要用來檢查應用的執行狀態。如果應用有異常,同時給我們反饋異常原因。比如資料庫連線異常,磁碟空間過小等異常。

  2. info

    自定義應用程式的配置資訊。

    例如,在配置檔案中配置如下資訊:

    info.app.name=actuator
    info.app.versoin=1.0.0
    info.app.data=2019-06-25 12:00:00
    info.app.author=yanfei1819

    啟動專案,訪問http://localhost:8080/actuator/info,可以得到如下響應:

    {"app":{"name":"actuator","versoin":"1.0.0","data":"2019-06-25 12:00:00","author":"yanfei1819"}}
  3. beans

    該 endpoint 展示了 bean 的別名、型別、是否單例、類的地址、依賴等資訊。

  4. conditions

    Spring Boot 的自動配置功能非常便利,但有時候也意味著出問題比較難找出具體的原因。使用 conditions 可以在應用執行時檢視程式碼了某個配置在什麼條件下生效,或者某個自動配置為什麼沒有生效。

  5. heapdump

    展示Jvm 的堆檔案 heapdump。

  6. shutdown

    遠端關閉應用的端點,不過需要注意兩點:

    • 需要在配置檔案中配置 management.endpoint.shutdown.enabled=true
    • 只支援 POST 請求。
  7. mappings

    程式中所有的 URI 路徑,以及與控制器的關係。

  8. threaddump

    檢視執行緒資訊,例如執行緒名、執行緒ID、執行緒的狀態、是否等待鎖資源等。


使用

建立專案,引入 maven 依賴:

<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>

啟動專案,控制檯列印資訊:

可以看出此時只暴露了兩個 endpoint。

訪問 http://localhost:8080/actuator ,可以看到兩個端點是:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}

如果我們需要訪問所有的原生 endpoint,需要在配置檔案中加入:management.endpoints.web.exposure.include=*

重新啟動專案,控制檯日誌是:

訪問 http://localhost:8080/actuator ,可以看到所有端點是:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "auditevents": {
            "href": "http://localhost:8080/actuator/auditevents",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "conditions": {
            "href": "http://localhost:8080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8080/actuator/configprops",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/actuator/env/{toMatch}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        },
        "loggers": {
            "href": "http://localhost:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8080/actuator/threaddump",
            "templated": false
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "scheduledtasks": {
            "href": "http://localhost:8080/actuator/scheduledtasks",
            "templated": false
        },
        "httptrace": {
            "href": "http://localhost:8080/actuator/httptrace",
            "templated": false
        },
        "mappings": {
            "href": "http://localhost:8080/actuator/mappings",
            "templated": false
        }
    }
}

  讀者可以逐個訪問,檢視對應的返回資訊。

  當然,也可以通過配置 management.endpoints.web.exposure.exclude=info,trace 選擇部分 endpoint 暴露。

  同時,Actuator 預設所有的監控點路徑都在/actuator/*,當然如果有需要這個路徑也支援定製。management.endpoints.web.base-path=/manage


自定義endpoint

以下:

package com.yanfei1819.actuator.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 追夢1819 on 2019-06-25.
 */
@Configuration
@Endpoint(id = "customize-endpoint") // 構建 rest api 的唯一路徑
public class CustomizeEndPoint {
    @ReadOperation
    public Map<String, Object> endpoint() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("message", "this is customize endpoint");
        return map;
    }
}

在配置檔案中使其暴露:

management.endpoints.web.exposure.include=customize-endpoint

啟動程式,訪問 management.endpoints.web.exposure.include=customize-endpoint ,可以得到endpoint:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "customize-endpoint": {
            "href": "http://localhost:8080/actuator/customize-endpoint",
            "templated": false
        }
    }
}

再訪問返回的endpoint地址,得到相應:

{"message":"this is customize endpoint"}

可驗證自定義 endpoint 成功。


總結

  對於作者來說,這個功能核心是對 endpoints 的理解(我對該功能的使用總結,大部分時間也是耗在了這個上面)。理解了每一個 endpoint ,基本大的方向就掌握了。剩下的就是細節問題了(細節問題無非就是"慢工出細活",簡單)。

  另一個問題, Actuctor 的功能是實現了,可是大家有沒有覺得用起來很彆扭?檢視一個監控資訊,就訪問一個路徑,得到的就一連串的JSON,繁瑣、複雜、不夠直觀。這實屬讓運維同學抓狂的問題。有沒有好的解決方案呢?且聽下回分解。


參考

SpringBoot官網



相關推薦

SpringBoot第二應用監控Actuator

作者:追夢1819 原文:https://www.cnblogs.com/yanfei1819/p/11226397.html 版權宣告:本文為博主原創文章,轉載請附上博文連結! 引言   很多文章都將 SpringBoot Actuator 的 Endpoint 翻譯為 "端點"。不過

SpringBoot第二應用監控Admin

作者:追夢1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版權宣告:本文為博主原創文章,轉載請附上博文連結! 引言   前一章(SpringBoot第二十二篇:應用監控之Actuator)介紹了 SpringBoot 應用使用 Actuc

史上最簡單的SpringCloud教程 | 第 斷路器監控(Hystrix Dashboard)

詳細 pre 良好的 依次 alt ews 需要 ext 數據監控 最新Finchley版本,請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/或者http://blog.csdn.net

SpringBoot | 第二定時任務的使用

前言 上兩章節,我們簡單的講解了關於非同步呼叫和非同步請求相關知識點。這一章節,我們來講講開發過程也是經常會碰見的定時任務。比如每天定時清理無效資料、定時傳送簡訊、定時傳送郵件、支付系統中的定時對賬等等,往往都會定義一些定時器,進行此業務的開發。所以,本章節介紹下在Spri

一起來學SpringBoot | 第二輕鬆搞定安全框架(Shiro)

SpringBoot 是為了簡化 Spring 應用的建立、執行、除錯、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規範,引入相關的依賴就可以輕易的搭建出一個 WEB 工程 Shiro 是

SpringBoot整合jsp

作者:追夢1819 原文:https://www.cnblogs.com/yanfei1819/p/10953600.html 版權宣告:本文為博主原創文章,轉載請附上博文連結! 引言   SpringBoot 雖然官方推薦使用 thymelaf 模板引擎,但是也支援jsp,只不過需要做一些修改。本文將講解

SpringBoot第二整合ActiveMQ

作者:追夢1819 原文:https://www.cnblogs.com/yanfei1819/p/11190048.html 版權宣告:本文為博主原創文章,轉載請附上博文連結! 引言   前一章節中,我們詳細闡述了 ActiveMQ 的安裝和使用。其實在網站發展壯大的過程中,訊息中介軟體是無法忽視的技術框

SpringBoot | 第二一章非同步開發非同步呼叫

前言 上一章節,我們知道了如何進行非同步請求的處理。除了非同步請求,一般上我們用的比較多的應該是非同步呼叫。通常在開發過程中,會遇到一個方法是和實際業務無關的,沒有緊密性的。比如記錄日誌資訊等業務。這個時候正常就是啟一個新執行緒去做一些業務處理,讓主執行緒非同步的執行其他業

Spring Boot使用Spring RestTemplate訪問Rest服務

RestTemplate是Spring3.0後開始提供的用於訪問 Rest 服務的輕量級客戶端,相較於傳統的HttpURLConnection、Apache HttpClient、OkHttp等框架,RestTemplate大大簡化了發起HTTP請求以及處理響應的過程。這篇文章主要介紹怎

SpringBoot非官方教程 | 第二2小時學會springboot

  Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and i

SpringBoot | 第二八章監控管理Spring Boot Admin使用

前言 上一章節,我們介紹了Actuator的使用,知道了可通過訪問不同的端點路徑,獲取相應的監控資訊。但使用後也能發現,返回的監控資料都是以JSON串的形式進行返回的,對於實施或者其他人員來說,不是很直觀,而當需要監控的應用越來越多時,依次去訪問對應的應用也過於繁瑣和低效了。所以,本章節來介紹下Spring

一起來學SpringBoot | 第強大的 actuator 服務監控與管理

SpringBoot 是為了簡化 Spring 應用的建立、執行、除錯、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規範,引入相關的依賴就可以輕易的搭建出一個 WEB 工程 actuato

SpringBoot非官方教程 | 第二 springboot整合docker

這篇文篇介紹,怎麼為 springboot程式構建一個Docker映象。docker 是一個開源的應用容器引擎,基於 Go 語言 並遵從Apache2.0協議開源。Docker 可以讓開發者打包他們的應用以及依賴包到一個輕量級、可移植的容器中,然後釋出到任何流行的 L

SpringBoot度量指標監控與非同步呼叫(2020最新最易懂)

SpringBoot第十二集:度量指標監控與非同步呼叫(2020最新最易懂)   Spring Boot Actuator是spring boot專案一個監控模組,提供了很多原生的端點,包含了對應用系統的自省和監控的整合功能,比如應用程式上下文裡全部的Bean、執行狀況檢查、健康指標、環境變數及各類重要度量指

HTML基礎

項目 logs 瀏覽器 圖片 handle 標題欄 width light 標題 本篇內容 HTML概述 HTML常用基本標簽 CSS格式引入 一、 HTML概述 1.定義: HTML,超文本標記語言,寫給瀏覽器的語言,目前網絡上應用最廣泛的語言。HTML也在不斷

數據庫操作

mysq 查詢 alc mys clas 多對多 class 操作 連接查詢 一、數據庫簡紹 二、mysql 增刪改查 三、mysql 外鍵關聯 四、mysql 連接查詢 五、mysql 事務與索引 六、ORM 簡紹 七、sqlalchemy 常用語法 八、sqlalche

python全棧開發基礎【第二】進程池和回調函數

enc 並發執行 exce 核數 exc 為什麽 .py bsp urn 一、數據共享 1.進程間的通信應該盡量避免共享數據的方式 2.進程間的數據是獨立的,可以借助隊列或管道實現通信,二者都是基於消息傳遞的。 雖然進程間數據獨立,但可以用過Manager實現數據共享,事實

Django 【第二】ModelForm

post turn creat 自定義 固定 err edit ews 數據 一、ModelForm的介紹 ModelForm a. class Meta: model, # 對應Mo

第二JQuery

作者:java_wxid 點選:API文件下載 Jquery介紹 1.什麼是JQuery ? jQuery,顧名思義,也就是JavaScript和查詢(Query),它就是輔助JavaScript開發的js類庫。 2.JQuery核心思想: 它的核心思想是write less

第二spring怎麼做快取

 專案背景: 你可能遇情景: 1、一個做統計的頁面,每次重新整理需要調介面做查詢 ,是聯表查詢,查出來的資料還需要做一些計算或者加工,不算頁面上的圖表外掛,重新整理一次,延遲個幾秒鐘才出的來 2、 一個統計介面如此,一個頁面如果好幾個統計的介面查詢…… 3、你有很多介