1. 程式人生 > 實用技巧 >SpringBoot系列之actuator監控管理極速入門與實踐

SpringBoot系列之actuator監控管理極速入門與實踐

@目錄

    SpringBoot官方提供了spring-boot-starter-actuator場景啟動器用於系統的監控管理,可以通過HTTP,JMX,SSH協議來進行操作,自動得到審計、健康及指標資訊等

    環境準備:

    • JDK 1.8
    • SpringBoot2.2.1
    • Maven 3.2+
    • 開發工具
      • IntelliJ IDEA
      • smartGit

    建立一個SpringBoot Initialize專案,詳情可以參考我之前部落格:SpringBoot系列之快速建立專案教程

    要將執行器新增到基於Maven的專案中,請檢查新增以下“ Starter”依賴項:

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

    專案啟動成功後,如果沒設定context-path,專案會自動加入/actuator作為字首,大部分端點是預設啟動的,不過要通過web瀏覽器方式訪問的只有health、info端點

    可以通過配置修改預設字首

    management.endpoints.web.base-path=/actuator
    

    通用的端點(http、Jms、ssh方式都能訪問):

    ID 描述 預設啟用
    auditevents 暴露當前應用程式的審計事件資訊。
    beans 顯示應用程式中所有 Spring bean 的完整列表。
    caches 暴露可用的快取。
    conditions 顯示在配置和自動配置類上評估的條件以及它們匹配或不匹配的原因。
    configprops 顯示所有 @ConfigurationProperties 的校對清單。
    env 暴露 Spring ConfigurableEnvironment 中的屬性。
    flyway 顯示已應用的 Flyway 資料庫遷移。
    health 顯示應用程式健康資訊
    httptrace 顯示 HTTP 追蹤資訊(預設情況下,最後 100 個 HTTP 請求/響應交換)。
    info 顯示應用程式資訊。
    integrationgraph 顯示 Spring Integration 圖。
    loggers 顯示和修改應用程式中日誌記錄器的配置。
    liquibase 顯示已應用的 Liquibase 資料庫遷移。
    metrics 顯示當前應用程式的指標度量資訊。
    mappings 顯示所有 @RequestMapping 路徑的整理清單。
    scheduledtasks 顯示應用程式中的排程任務。
    sessions 允許從 Spring Session 支援的會話儲存中檢索和刪除使用者會話。當使用 Spring Session 的響應式 Web 應用程式支援時不可用。
    shutdown 正常關閉應用程式。POST請求方式
    threaddump 執行執行緒 dump。

    GET方式呼叫health端點,返回json資訊

    Web 應用程式(Spring MVC、Spring WebFlux 或 Jersey),則可以使用以下附加端點,這個應該是2.x版本才加上的

    ID 描述 預設啟用
    heapdump 返回一個 hprof 堆 dump 檔案。
    jolokia 通過 HTTP 暴露 JMX bean(當 Jolokia 在 classpath 上時,不適用於 WebFlux)。
    logfile 返回日誌檔案的內容(如果已設定 logging.filelogging.path 屬性)。支援使用 HTTP Range 頭來檢索部分日誌檔案的內容。
    prometheus 以可以由 Prometheus 伺服器抓取的格式暴露指標。

    啟用端點,修改配置,語法management.endpoint.[端點名稱].enabled=true

    management.endpoint.shutdown.enabled=true
    

    下表顯示了內建端點和預設暴露情況,以JMX、WEB(Http)做對比:

    ID JMX Web
    auditevents
    beans
    caches
    conditions
    configprops
    env
    flyway
    health
    heapdump N/A
    httptrace
    info
    integrationgraph
    jolokia N/A
    logfile N/A
    loggers
    liquibase
    metrics
    mappings
    prometheus N/A
    scheduledtasks
    sessions
    shutdown
    threaddump

    要更改暴露的端點,請使用以下特定的 includeexclude 屬性:

    屬性 預設
    management.endpoints.jmx.exposure.exclude
    management.endpoints.jmx.exposure.include *
    management.endpoints.web.exposure.exclude
    management.endpoints.web.exposure.include info, health

    include 屬性列出了暴露的端點的 ID。exclude 屬性列出了不應暴露的端點的 ID。exclude 屬性優先於 include 屬性。

    例子:
    關閉jmx訪問所有端點的許可權,只讓其能訪問health、info

    management.endpoints.jmx.exposure.include=health,info
    

    啟用web訪問所有端點,除env之外的許可權

    management.endpoints.web.exposure.include=*
    management.endpoints.web.exposure.exclude=env
    

    注意

    * 在 YAML 中具有特殊含義,因此如果要包含(或排除)所有端點,請務必新增引號,如下所示:

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

    自定義InfoContributor

    package com.example.springboot.actuator.actuate.health;
    
    import java.util.Collections;
    
    import org.springframework.boot.actuate.info.Info;
    import org.springframework.boot.actuate.info.InfoContributor;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExampleInfoContributor implements InfoContributor {
    
        @Override
        public void contribute(Info.Builder builder) {
            builder.withDetail("example",
                    Collections.singletonMap("key", "value"));
        }
    
    }
    

    可以在瀏覽器或者postman呼叫:

    跨域支援配置

    management.endpoints.web.cors.allowed-origins=http://localhost
    management.endpoints.web.cors.allowed-methods=GET,POST
    

    定置端點:

    management.endpoint.info.enabled=true
    management.endpoint.info.cache.time-to-live=10s
    

    ok,actuator的知識點比較多,詳情請參考官方文件,本部落格參考官方文件,做了簡單記錄,僅僅作為入門參考手冊

    程式碼例子下載:code download