SpringBoot2.X性能監(jiān)控Actuator

title: SpringBoot2.X性能監(jiān)控Actuator
date: 2020-03-20
author: maxzhao
tags:
  - SpringBoot
  - Actuator
categories:
  - SpringBoot

一、前言

SpringBoot Actuator 服務(wù)監(jiān)控與管理**

其中包含了很多的服務(wù),比如我們常用的amqpJVMcache等等,下面是actuator包下的目錄

amqp,audit,beans,cache,cassandra,context,couchbase,elasticsearch,endpoint,env,flyway,health,influx,info,integration,jdbc,jms,ldap,liquibase,logging,mail,management,metrics,mongo,neo4j,redis,scheduling,security,session,solr,system,http,web

是不是感覺挺全面的。

二、服務(wù)監(jiān)控與管理

Maven 依賴

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

主要配置

Spring Boot2.x中,默認只開放了info、health兩個端點,開放其他端點需要配置

# 開啟所有端點
management:
  endpoints:  # 這里是 endpoints
    web:
      # 默認路徑
      base-path: /actuator
      exposure:
        #  Endpoint IDs that should be included or '*' for all.
        include: '*'
    # 顯示詳細的 health 信息
    jmx:
      # Whether unique runtime object names should be ensured.
      domain: org.springframework.boot
      exposure:
        # Endpoint IDs that should be included or '*' for all.
        include: '*'
  # 顯示詳細的 health 信息
  endpoint: # 這里是 endpoint
    health:
      show-details: always
    # 打開 shutdown 端點,通過 POST 訪問該端點可以關(guān)閉應(yīng)用
    shutdown:
      enabled: true

監(jiān)控狀態(tài)

啟動之后訪問 http://localhost:8062/boot/actuator/health 就可以看到對應(yīng)的項目監(jiān)控狀態(tài)。

訪問 http://localhost:8062/boot/actuator 可以查看有那些監(jiān)控。

健康指標(biāo) HealthIndicators 由 Spring Boot 自動配置,因此這里顯示監(jiān)控信息是由項目所使用的技術(shù)棧而決定的:

名稱 描述
CassandraHealthIndicator 檢查 Cassandra 數(shù)據(jù)庫是否啟動。
DiskSpaceHealthIndicator 檢查磁盤空間不足。
DataSourceHealthIndicator 檢查是否可以獲得連接 DataSource。
ElasticsearchHealthIndicator 檢查 Elasticsearch 集群是否啟動。
InfluxDbHealthIndicator 檢查 InfluxDB 服務(wù)器是否啟動。
JmsHealthIndicator 檢查 JMS 代理是否啟動。
MailHealthIndicator 檢查郵件服務(wù)器是否啟動。
MongoHealthIndicator 檢查 Mongo 數(shù)據(jù)庫是否啟動。
Neo4jHealthIndicator 檢查 Neo4j 服務(wù)器是否啟動。
RabbitHealthIndicator 檢查 Rabbit 服務(wù)器是否啟動。
RedisHealthIndicator 檢查 Redis 服務(wù)器是否啟動。
SolrHealthIndicator 檢查 Solr 服務(wù)器是否已啟動。

常用端點

查看常用接口

http://localhost:8062/boot/actuator/

env 端點,應(yīng)用獲取環(huán)境信息,包括:環(huán)境變量、JVM屬性、應(yīng)用的配置配置、命令行中的參數(shù)等等。
localhost:8080/actuator/env

mapping 端點,url 與 控制器映射關(guān)系信息
localhost:8080/actuator/info

metrics 端點,引用度量指標(biāo)端點,提供引用再運行時的信息,如內(nèi)存使用情況、HTTP請求統(tǒng)計、外部資源指標(biāo)等
查看所有度量指標(biāo) localhost:8080/actuator/metrics
查看度量指標(biāo)詳細信息 localhost:8080/actuator/metrics/jvm.gc.pause

loggers 端點,查看可配置 loggers 的列表及相關(guān)的等級信息
localhost:8080/actuator/loggers
查看特定的 logger 詳細信息localhost:8080/actuator/loggers/{name}

健康檢查

health 端點用于暴露程序運行的健康狀態(tài),暴露的信息的詳細程度由 management.endpoint.health.show-details 來控制,它具有以下三個可選值:

名稱 描述
never 細節(jié)永遠不會顯示。
when-authorized 詳細信息僅向授權(quán)用戶顯示。授權(quán)角色可以使用配置 management.endpoint.health.roles。
always 詳細信息顯示給所有用戶。

org.springframework.boot.actuate.health.ShowDetails中有詳細說明。

端點列表

  • info
    顯示應(yīng)用的基本信息
  • health
    顯示應(yīng)用的健康狀態(tài)
  • metrics
    顯示應(yīng)用多樣的度量信息
  • loggers
    顯示和修改配置的loggers
  • logfile
    返回log file中的內(nèi)容(如果logging.file或者logging.path被設(shè)置)
  • httptrace
    顯示HTTP足跡,最近100個HTTP request/repsponse
  • env
    顯示當(dāng)前的環(huán)境特性
  • flyway
    顯示數(shù)據(jù)庫遷移路徑的詳細信息
  • liquidbase
    顯示Liquibase 數(shù)據(jù)庫遷移的纖細信息
  • shutdown
    讓你逐步關(guān)閉應(yīng)用
  • mappings
    顯示所有的@RequestMapping路徑
  • scheduledtasks
    顯示應(yīng)用中的調(diào)度任務(wù)
  • threaddump
    執(zhí)行一個線程dump
  • heapdump
    返回一個GZip壓縮的JVM堆dump

三、自定義健康檢查

在啟動類中加入

@Bean
HealthIndicator customHealthIndicator() {
    return () -> Health.status("DOWN")
                .withDetail("error code", "某健康專項檢查失敗").build();
}
@Bean
HealthIndicator customUpHealthIndicator() {
    return () -> Health.up().withDetail("success code", "自定義檢查一切正常 UP").build();
}
@Bean
HealthIndicator customDownHealthIndicator() {
    return () -> Health.up().withDetail("success code", "自定義檢查一切正常 DOWN ").build();
}

訪問 http://localhost:8062/boot/actuator/health 的結(jié)果為:

這里我開啟了redis,數(shù)據(jù)庫為mysql

{
    "status": "DOWN",
    "details": {
        "custom": {
            "status": "FATAL",
            "details": {
                "error code": "某健康專項檢查失敗"
            }
        },
        "customUp": {
            "status": "UP",
            "details": {
                "success code": "自定義檢查一切正常 UP"
            }
        },
        "customDown": {
            "status": "DOWN",
            "details": {
                "success code": "自定義檢查一切正常 DOWN "
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": "471182741504",
                "free": "375580655616",
                "threshold": "10485760"
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "hello": "1"
            }
        },
        "redis": {
            "status": "UP",
            "details": {
                "version": "5.0.8"
            }
        }
    }
}

當(dāng)前details中有一個檢查statusDOWN時,Health檢查的status就為DOWN,否則為UP

如果把第一個FATAL改為DOWNHealth檢查結(jié)果同樣為DOWN

下表顯示了內(nèi)置狀態(tài)的默認映射:

Status Mapping
DOWN SERVICE_UNAVAILABLE (503)
OUT_OF_SERVICE SERVICE_UNAVAILABLE (503)
UP No mapping by default, so http status is 200
UNKNOWN No mapping by default, so http status is 200

四、自定義端點

Spring Boot 支持使用 @Endpoint 來自定義端點暴露信息。

@Endpoint(id = "customEndPoint")
@Component
public class CustomEndPoint {

    @ReadOperation
    public Map<String, Object> getInfo() {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        dataMap.put("自定義信息", "custom endpoint ");
        return dataMap;
    }
}

請求 http://localhost:8062/boot/actuator/customEndPoint 的結(jié)果為

{
    "自定義信息": "custom endpoint "
}

可用的方法注解由 HTTP 操作所決定:

operation HTTP 方法
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容