SpringBoot2.X性能监控Actuator

title: SpringBoot2.X性能监控Actuator
date: 2020-03-20
author: maxzhao
tags:
  - SpringBoot
  - Actuator
categories:
  - SpringBoot

一、前言

SpringBoot Actuator 服务监控与管理**

其中包含了很多的服务,比如我们常用的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

是不是感觉挺全面的。

二、服务监控与管理

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 访问该端点可以关闭应用
    shutdown:
      enabled: true

监控状态

启动之后访问 http://localhost:8062/boot/actuator/health 就可以看到对应的项目监控状态。

访问 http://localhost:8062/boot/actuator 可以查看有那些监控。

健康指标 HealthIndicators 由 Spring Boot 自动配置,因此这里显示监控信息是由项目所使用的技术栈而决定的:

名称 描述
CassandraHealthIndicator 检查 Cassandra 数据库是否启动。
DiskSpaceHealthIndicator 检查磁盘空间不足。
DataSourceHealthIndicator 检查是否可以获得连接 DataSource。
ElasticsearchHealthIndicator 检查 Elasticsearch 集群是否启动。
InfluxDbHealthIndicator 检查 InfluxDB 服务器是否启动。
JmsHealthIndicator 检查 JMS 代理是否启动。
MailHealthIndicator 检查邮件服务器是否启动。
MongoHealthIndicator 检查 Mongo 数据库是否启动。
Neo4jHealthIndicator 检查 Neo4j 服务器是否启动。
RabbitHealthIndicator 检查 Rabbit 服务器是否启动。
RedisHealthIndicator 检查 Redis 服务器是否启动。
SolrHealthIndicator 检查 Solr 服务器是否已启动。

常用端点

查看常用接口

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

env 端点,应用获取环境信息,包括:环境变量、JVM属性、应用的配置配置、命令行中的参数等等。
localhost:8080/actuator/env

mapping 端点,url 与 控制器映射关系信息
localhost:8080/actuator/info

metrics 端点,引用度量指标端点,提供引用再运行时的信息,如内存使用情况、HTTP请求统计、外部资源指标等
查看所有度量指标 localhost:8080/actuator/metrics
查看度量指标详细信息 localhost:8080/actuator/metrics/jvm.gc.pause

loggers 端点,查看可配置 loggers 的列表及相关的等级信息
localhost:8080/actuator/loggers
查看特定的 logger 详细信息localhost:8080/actuator/loggers/{name}

健康检查

health 端点用于暴露程序运行的健康状态,暴露的信息的详细程度由 management.endpoint.health.show-details 来控制,它具有以下三个可选值:

名称 描述
never 细节永远不会显示。
when-authorized 详细信息仅向授权用户显示。授权角色可以使用配置 management.endpoint.health.roles。
always 详细信息显示给所有用户。

org.springframework.boot.actuate.health.ShowDetails中有详细说明。

端点列表

  • info
    显示应用的基本信息
  • health
    显示应用的健康状态
  • metrics
    显示应用多样的度量信息
  • loggers
    显示和修改配置的loggers
  • logfile
    返回log file中的内容(如果logging.file或者logging.path被设置)
  • httptrace
    显示HTTP足迹,最近100个HTTP request/repsponse
  • env
    显示当前的环境特性
  • flyway
    显示数据库迁移路径的详细信息
  • liquidbase
    显示Liquibase 数据库迁移的纤细信息
  • shutdown
    让你逐步关闭应用
  • mappings
    显示所有的@RequestMapping路径
  • scheduledtasks
    显示应用中的调度任务
  • threaddump
    执行一个线程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 的结果为:

这里我开启了redis,数据库为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"
            }
        }
    }
}

当前details中有一个检查statusDOWN时,Health检查的status就为DOWN,否则为UP

如果把第一个FATAL改为DOWNHealth检查结果同样为DOWN

下表显示了内置状态的默认映射:

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 的结果为

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

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

operation HTTP 方法
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,513评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,312评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,124评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,529评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,937评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,913评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,084评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,816评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,593评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,788评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,267评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,601评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,265评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,158评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,953评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,066评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,852评论 2 277

推荐阅读更多精彩内容