天天看點

springboot actuator_springboot-actuator應用背景監控

springboot actuator_springboot-actuator應用背景監控

一 前言

springboot 額外的特色是提供了背景應用監控,可以通過 HTTP 或者 JMX的方式管理監控應用,本文主講HTTP方式;其主要的功能是監控應用的健康狀态,檢視環境變量等;

二 pom.xml

springboot 2.1.1,主要引入 actuator 依賴,web依賴用于測試;

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

三 預設開啟端點

3.1 預設端點 health

直接編寫主程式入口,啟動;浏覽器輸入 http://localhost:8080/actuator/health;結果如下,狀态是UP;

springboot actuator_springboot-actuator應用背景監控

翻翻源碼heath狀态碼如下

public OrderedHealthAggregator() {
        this.setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN);
    }
           
  1. DOWN 服務無法獲得,狀态碼503;
  2. .OUT_OF_SERVICE 服務無法獲得,狀态碼503;
  3. UP 獲得服務,狀态碼200;
  4. UNKNOWN 獲得未知服務,狀态碼200;

在 application.yml 中配置 healthy 資訊 示例如下:

management:
  endpoint:
    health:
      show-details: always
           

列印詳細資訊:

springboot actuator_springboot-actuator應用背景監控

基本配置如下:

  1. never :預設,表示不顯示詳細資訊;
  2. when-authorized:詳細資訊顯示給 認證過的使用者;使用 management.endpoint.health.roles 配置
  3. always: 顯示詳細資訊給所有使用者

3.2 預設端點 info

浏覽器輸入 http://localhost:8080/actuator/info; 展示空資訊如下圖:

springboot actuator_springboot-actuator應用背景監控

在application.yml 中 配置工程 info 資訊 示例如下;

# 配置資訊
info:
  actuator:
    name: springboot-actutor
    version: 1.0.0
    author: zszxz
           

展示結果如下:

springboot actuator_springboot-actuator應用背景監控

四 HTTP端點說明

springboot actuator_springboot-actuator應用背景監控

五 配置開啟端點

application.yml 中配置需要開啟的端點,其中 * 表示開啟所有端點,示例如下:

management:
  endpoints:
    web:
      exposure:
        # 使用通配符 * 表示比對所有端點
        # 排除的端點
        exclude: caches
        # 包括的端點
        include: info,health,beans,env,shutdown,threaddump
           

5.1 threaddump示例

http://localhost:8080/actuator/threaddump ;用于傳回線程快照,分析線程阻塞,死鎖等,部分内容如下

{
    "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,部分内容如下;

{
    "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 關閉應用示例

普通情況下是沒有開啟這個配置,是比較危險的動作,會導緻應用停止;修改application.yml配置如下

management:
  endpoints:
    web:
      exposure:
        # 使用通配符 * 表示比對所有端點
        # 排除的端點
        exclude: caches
        # 包括的端點
        include: info,health,beans,env,shutdown
  endpoint:
    health:
      show-details: always
    # 開啟關閉應用 需要post請求
    shutdown:
      enabled: true
           

通路位址 http://localhost:8080/actuator/shutdown; 注意僅支援使用POST請求,否則 會 405錯誤;

六 CORS 支援

application.yml 修改配置如下, allowed-origins 中允許跨域的ip位址; allowed-methods 配置 允許通過的請求,還有支援時間等;

management:
  endpoints:
    web:
      exposure:
        # 使用通配符 * 表示比對所有端點
        # 排除的端點
        exclude: caches
        # 包括的端點
        include: info,health,beans,env,shutdown
      # 跨域處理
      cors:
        allowed-origins: http://localhost:8080/
        allowed-methods: post,delete,get,put
  endpoint:
    health:
      show-details: always
    # 開啟關閉應用 需要post請求
    shutdown:
      enabled: true
           

七 修改預設路徑

在 配置檔案中添加 base-path , 會修改掉預設路徑 actuator/endpoint;

management:
  endpoints:
    web:
      exposure:
        # 使用通配符 * 表示比對所有端點
        # 排除的端點
        exclude: caches
        # 包括的端點
        include: info,health,beans,env,shutdown
      # 自定義配置監控路徑
      base-path: /zszxz
      # 跨域處理
      cors:
        allowed-origins: http://localhost:8080/
        allowed-methods: post,delete,get,put
  endpoint:
    health:
      show-details: always
    # 開啟關閉應用 需要post請求
    shutdown:
      enabled: true
           

示例url: http://localhost:8080/zszxz/info

結果如下

springboot actuator_springboot-actuator應用背景監控

八 其他配置說明

  1. 還可以引入 security 依賴 配置 賬号密碼,角色資訊,達到通路控制,詳細的可以參照官網;
  2. 還可以使用注解進行配置,自定義端點,詳細參照官網;
  3. jmx支援,可以使用open jdk 自帶的工具 jconsole 進行監控;