【Spring Boot】29.監控管理

簡介

監控是咱們開發服務網站沒法繞開的話題。springboot也爲咱們帶來了關於監控的解決方案。html

經過引入spring-boot-starter-actuator,可使用Spring Boot爲咱們提供的準生產環境下的應用監控和管理功能。咱們能夠經過HTTP,JMX,SSH協議來進行操做,自動獲得審計、健康及指標信息等。java

引入步驟也很簡單。react

  1. 引入spring-boot-starter-actuator
  2. 經過http方式訪問監控端點
  3. 可進行shutdown(POST 提交,此端點默認關閉)

官方參考文檔git

快速體驗

  1. 搭建基本環境 建立項目,選擇的模塊有 web,core-DevTools,Ops-Actuator.web

  2. 查閱官方文檔咱們能夠知道,web應用和JMX不同。默認狀況下除了health,其餘的可視化接口都是關閉的,所以咱們須要配置開啓這些終端。redis

management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. 啓動項目,咱們就能夠經過http://localhost:8080/actuator/終端名稱等方式訪問本身想知道的具體終端的信息了,例如目標設置爲beans獲取咱們容器中全部的bean信息。如下是對照表
端點名 描述
auditevents Exposes audit events information for the current application.
beans Displays a complete list of all the Spring beans in your application.
caches Exposes available caches.
conditions Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
configprops Displays a collated list of all @ConfigurationProperties
env Exposes properties from Spring’s ConfigurableEnvironment.
flyway Shows any Flyway database migrations that have been applied.
health Shows application health information.
httptrace Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).
info Displays arbitrary application info.
integrationgraph Shows the Spring Integration graph.
loggers Shows and modifies the configuration of loggers in the application.
metrics Shows ‘metrics’ information for the current application.
mappings Displays a collated list of all @RequestMapping paths.
scheduledtasks Displays the scheduled tasks in your application.
sessions Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.
shutdown Lets the application be gracefully shutdown.
threaddump Performs a thread dump.

若是是web應用,還會有以下更多的可查看終端 |端點名|描述| |-|-| |heapdump|Returns an hprof heap dump file.| |jolokia|Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux).| |logfile|Returns the contents of the logfile (if logging.file or logging.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.| |prometheus|Exposes metrics in a format that can be scraped by a Prometheus server.| |||spring

注意不一樣的actuator版本可能會有不一致的狀況;json

Metrics是一個給JAVA服務的各項指標提供度量工具的包,在JAVA代碼中嵌入Metrics代碼,能夠方便的對業務代碼的各個指標進行監控;springboot

threaddump在1.x版本爲dumpsession

其中,info信息默認是沒有輸出的,可是咱們能夠經過配置文件填寫,例如咱們能夠在配置文件中添加這樣的配置

info:
  app:
    id: hello-1
    name: hello

重啓項目以後,咱們查看info信息,就能夠獲得以下的輸出了:

{"app":{"id":"hello-1","name":"hello"}}

固然不少繼承了InfoProperties的類的相關配置都會讀取到該輸出中,例如git.xxx.xxx等,你們能夠本身去測試。

咱們還能夠遠程關閉應用,即經過shutdown終端,該終端默認是不啓用的,咱們要經過配置開啓。

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    shutdown:
      enabled: true

重啓服務,訪問shutdown終端,注意須要提供的是post請求,也就是說,直接經過url是沒辦法關閉的,能夠藉助一些http客戶端工具輔助完成,好比咱們以前提到的postman等。咱們就能夠遠程關閉應用了,應用程序以前還會爲咱們打招呼,很可愛。

{
    "message": "Shutting down, bye..."
}

定製端點

咱們能夠修改某些端點的訪問路徑,經過 management.endpoints.端點.屬性=xxx 例如management.endpoint.beans.cache.time-to-live=10s能夠配置bean端口的響應時間等。

您能夠經過management.endpoint.web相關的屬性進行配置,例如修改默認的context地址(base-dir=/actuator)等,均可以參考官方文檔一一測試。

自定義健康狀態組件

能夠從spring-boot-actuator中查看這些爲咱們提供的監控組件。官網文檔告訴咱們,若是咱們引入了某個組件,例如redis,系統就會在health之中添加相關的監控結果。

若是咱們配置了這個選項。

management.endpoint.health.show-details=always

可取值never(默認)、when-authorized(只容許認證用戶查看,能夠經過management.endpoint.health.roles.配置相應的角色)、always,全部用戶均可以查看。

這樣,系統就會爲咱們監控咱們提供的組件的監控信息,不配置detail的話看不到這些詳細信息。

{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":141966430208,"free":2797314048,"threshold":10485760}},"redis":{"status":"UP","details":{"version":"3.0.503"}}}}

You can disable them all by setting the management.health.defaults.enabled property.

如今咱們開始自定義本身的j健康指示器

  1. 編寫一個指示器,實現HealthIndicator接口
  2. 指示器的名字必須爲xxxHealthIndicator;
  3. 加入到容器中。

添加類health.MyHealthIndicator

package com.example.demo.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 自定義的檢查方法

        // 返回up表明健康,down表明異常
        return Health.up().withDetail("key","this is we need to say message").build();
    }
}

訪問actuator/health便可看到咱們的輸出。

{"status":"UP","details":{"my":{"status":"UP","details":{"key":"this is we need to say message"}},"diskSpace":{"status":"UP","details":{"total":141966430208,"free":3067961344,"threshold":10485760}},"redis":{"status":"UP","details":{"version":"3.0.503"}}}}

能夠看到,輸出結構的第一層鍵值是咱們的健康指示器的前綴,例如my,而status則取決於咱們返回的是up仍是down,detail中包含的數據就是咱們定義在withDetail方法中的數據。

具體的實現,有興趣的同窗能夠查看源碼瞭解。

至此,有關springboot的基礎內容就到此爲止了。

—— 但願你們之後的開發道路一帆風順,同時感謝這些孜孜不倦的授課的教師們。

相關文章
相關標籤/搜索