天天看點

SpringBoot2.0 基礎案例(16):配置Actuator元件,實作系統監控

本文源碼:GitHub·點這裡 || GitEE·點這裡

一、Actuator簡介

1、監控元件作用

在生産環境中,需要實時或定期監控服務的可用性。Spring Boot的actuator(健康監控)功能提供了很多監控所需的接口,可以對應用系統進行配置檢視、相關功能統計等。

2、監控分類

Actuator 提供Rest接口,展示監控資訊。

接口分為三大類:

應用配置類:擷取應用程式中加載的應用配置、環境變量、自動化配置報告等與SpringBoot應用相關的配置類資訊。

度量名額類:擷取應用程式運作過程中用于監控的度量名額,比如:記憶體資訊、線程池資訊、HTTP請求統計等。

操作控制類:提供了對應用的關閉等操作類功能。

二、與SpringBoot2.0整合

1、核心依賴Jar包

<!-- 監控依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
           

2、Yml配置檔案

# 端口
server:
  port: 8016
spring:
  application:
    # 應用名稱
    name: node16-boot-actuator
management:
  endpoints:
    web:
      exposure:
        # 打開所有的監控點
        include: "*"
      # 自定義監控路徑 monitor
      # 預設值:http://localhost:8016/actuator/*
      # 配置後:http://localhost:8016/monitor/*
      base-path: /monitor
  endpoint:
    health:
      show-details: always
    shutdown:
      # 通過指定接口關閉 SpringBoot
      enabled: true
  # 可以自定義端口
  # server:
  #   port: 8089

# 描述項目基礎資訊
info:
  app:
    name: node16-boot-actuator
    port: 8016
    version: 1.0.0
    author: cicada
           

三、監控接口詳解

1、Info接口

Yml檔案中配置的項目基礎資訊

路徑:http://localhost:8016/monitor/info
輸出:
{
    "app": {
        "name": "node16-boot-actuator",
        "port": 8016,
        "version": "1.0.0",
        "author": "cicada"
    }
}
           

2、Health接口

health 主要用來檢查應用的運作狀态

路徑:http://localhost:8016/monitor/health
輸出:
{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 185496236032,
                "free": 140944084992,
                "threshold": 10485760
            }
        }
    }
}
           

3、Beans接口

展示了 bean 的類型、單例多例、别名、類的全路徑、依賴Jar等内容。

路徑:http://localhost:8016/monitor/beans
輸出:
{
    "contexts": {
        "node16-boot-actuator": {
        "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"]
            }
        }
    }
}
           

4、Conditions接口

檢視配置在什麼條件下有效,或者自動配置為什麼無效。

路徑:http://localhost:8016/monitor/conditions
輸出:
{
    "contexts": {
        "node16-boot-actuator": {
            "positiveMatches": {
                "AuditAutoConfiguration#auditListener": [{
                    "condition": "OnBeanCondition",
                    "message": "@ConditionalOnMissingBean"
                }],
    }
}
           

5、HeapDump接口

自動生成Jvm的堆轉儲檔案HeapDump,可以使用監控工具 VisualVM 打開此檔案檢視記憶體快照。

路徑:http://localhost:8016/monitor/heapdump
           

6、Mappings接口

描述 URI 路徑和控制器的映射關系

路徑:http://localhost:8016/monitor/mappings
輸出:
{
    "contexts": {
        "node16-boot-actuator": {
            "mappings": {
                "dispatcherServlets": {
                    "dispatcherServlet": [ {
                        "handler": "Actuator web endpoint 'auditevents'",
                        "predicate": "{GET /monitor/auditevents || application/json]}",
                        "details": {
                            "handlerMethod": {
                                "className": "org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping.Operat
                                "name": "handle",
                                "descriptor": "(Ljavax/servlet/http/HttpServletRequest;Ljava/util/Map;)Ljava/lang/Object;"
                            },
                            "requestMappingConditions": {
                                "consumes": [],
                                "headers": [],
                                "methods": ["GET"],
                                "params": [],
                                "patterns": ["/monitor/auditevents"],
                                "produces": [{
                                    "mediaType": "application/vnd.spring-boot.actuator.v2+json",
                                    "negated": false
                                }, {
                                    "mediaType": "application/json",
                                    "negated": false
                                }]
                            }
                        }
                    }
            }
    }
}
           

7、ThreadDump接口

展示線程名、線程ID、是否等待鎖、線程的狀态、線程鎖等相關資訊。

路徑:http://localhost:8016/monitor/threaddump
輸出:
{
    "threads": [{
        "threadName": "DestroyJavaVM",
        "threadId": 34,
        "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
    }
    ]
}
           

8、ShutDown接口

優雅關閉 Spring Boot 應用,預設隻支援POST請求。

路徑:http://localhost:8016/monitor/shutdown
           

四、源代碼位址

GitHub·位址
https://github.com/cicadasmile/spring-boot-base
GitEE·位址
https://gitee.com/cicadasmile/spring-boot-base
           
SpringBoot2.0 基礎案例(16):配置Actuator元件,實作系統監控