Springboot2(16)运行状态监控使用Actuator

源码地址

添加依赖

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

springboot2.0 的配置

#actuator端口
management.server.port: 9001
#修改访问路径  2.0之前默认是/   2.0默认是 /actuator  可以通过这个属性值修改
management.endpoints.web.base-path: /actuator
#开放所有页面节点  默认只开启了health、info两个节点
management.endpoints.web.exposure.include: '*'
#显示健康具体信息  默认不会显示详细信息
management.endpoint.health.show-details: always

默认情况下,shutdown启用除以外的所有端点。要配置端点的启用,请使用其management.endpoint.<id>.enabled属性。以下示例启用shutdown端点:

management.endpoint.shutdown.enabled = true

如果您希望端点启用是选择加入而不是选择退出,请将该management.endpoints.enabled-by-default属性设置 为false并使用各个端点 enabled属性重新加入。以下示例启用info端点并禁用所有其他端点:

management.endpoints.enabled-by-default = false
management.endpoint.info.enabled = true

配置完成启动项目后就可以通过postman或者直接在预览器输入路径等方式来查看应用的运行状态了。

例如使用postman发送 localhost:9001/actuator/health GET请求 (除了shutdown请求为post,其他的皆为GET请求)

当项目启动时,访问http://127.0.0.1:9001/actuator地址,如果看到类似下面的内容,说明actuator已经生效了

{
    "_links": {
        "self": {
            "href": "http://localhost:8000/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8000/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8000/actuator/info",
            "templated": false
        }
        ...
        ...
    }
}

在这里插入图片描述

可配置端点

可以使用以下与技术无关的端点:

ID描述默认情况下启用
auditevents公开当前应用程序的审核事件信息。
beans显示应用程序中所有Spring bean的完整列表。
caches暴露可用的缓存。
conditions显示在配置和自动配置类上评估的条件以及它们匹配或不匹配的原因。
configprops显示所有的整理列表@ConfigurationProperties
env露出Spring的属性ConfigurableEnvironment
flyway显示已应用的任何Flyway数据库迁移。
health显示应用健康信息。
httptrace显示HTTP跟踪信息(默认情况下,最后100个HTTP请求 - 响应交换)。
info显示任意应用信息。
integrationgraph显示Spring Integration图。
loggers显示和修改应用程序中记录器的配置。
liquibase显示已应用的任何Liquibase数据库迁移。
metrics显示当前应用程序的“指标”信息。
mappings显示所有@RequestMapping路径的整理列表。
scheduledtasks显示应用程序中的计划任务。
sessions允许从Spring Session支持的会话存储中检索和删除用户会话。使用Spring Session对响应式Web应用程序的支持时不可用。
shutdown允许应用程序正常关闭。没有
threaddump执行线程转储。

如果您的应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点:

ID描述默认情况下启用
heapdump返回hprof堆转储文件。
jolokia通过HTTP公开JMX bean(当Jolokia在类路径上时,不适用于WebFlux)。
logfile返回日志文件的内容(如果已设置logging.filelogging.path属性)。支持使用HTTP Range标头来检索部分日志文件的内容。
prometheus以可以由Prometheus服务器抓取的格式公开指标。

个别接口讲解

health

访问http://127.0.0.1:9001/actuator/health 可以看到

{
  "status": "UP",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 64756318208,
        "free": 62741700608,
        "threshold": 10485760
      }
    }
  }
}

自动配置的HealthIndicators

下面的HealthIndicators会被Spring Boot自动配置(在合适的时候):

名字描述
CassandraHealthIndicator检查Cassandra database是否正常
DiskSpaceHealthIndicator低磁盘空间检测
DataSourceHealthIndicator检查数据库连接是否正常
ElasticsearchHealthIndicator检查Elasticsearch cluster是否正常
JmsHealthIndicator检查JMS broker是否正常
MailHealthIndicator检查mail server是否正常
MongoHealthIndicator检查Mongo database是否正常
RabbitHealthIndicator检查Rabbit server是否正常
RedisHealthIndicator检查Redis server是否正常
SolrHealthIndicator检查Solr server是否正常
编写自定义HealthIndicators

想提供自定义健康信息,你可以注册实现了HealthIndicator接口的Spring beans。你需要提供一个health()方法的实现,并返回一个Health响应。Health响应需要包含一个status和可选的用于展示的详情。比如上面/health接口中的hello就是再下面定义的

@Component
public class HelloHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode)  .build();
        }
        return Health.up().build();
    }

    int check(){
        return 0;
    }
}

在这里插入图片描述

metrics

访问/actuator/metrics接口中,会返回actuator提供的所有metric的name

"names": [
    ...
    "jvm.classes.loaded",
    "jvm.classes.unloaded",
    "tomcat.global.error",
    "tomcat.sessions.active.current",
    "tomcat.sessions.alive.max",
    "jvm.gc.live.data.size",
    "tomcat.servlet.request.max",
    "http.server.requests",
    "tomcat.threads.current",
    ...
  ]

在/actuator/metrics接口后面直接加上metric的name,则可以访问该metric的信息。我比较关心的是http.server.requests这个metric,因为利用它可以完成对接口的监控

访问/actuator/metrics/http.server.requests接口,可以看到类似下面的内容

{
  "name": "http.server.requests",
  "description": null,
  "baseUnit": "seconds",
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 12
    },
    {
      "statistic": "TOTAL_TIME",
      "value": 0.045945588
    },
    {
      "statistic": "MAX",
      "value": 0.003358018
    }
  ],
  "availableTags": [
    {
      "tag": "exception",
      "values": [
        "None"
      ]
    },
    {
      "tag": "method",
      "values": [
        "GET"
      ]
    },
    {
      "tag": "uri",
      "values": [
        "/clog"
      ]
    },
    {
      "tag": "outcome",
      "values": [
        "SUCCESS"
      ]
    },
    {
      "tag": "status",
      "values": [
        "200"
      ]
    }
  ]
}

在measurements下面可以看到所有接口一共被访问了多少次,返回所有结果一共耗时了多久,返回最慢的接口耗时了多久等。单单是这些信息,并不觉得多么重要。但后面还有一个属性是availableTags,它给出了所有可用的tag key和tag value,根据这些可以进一步筛选你想要监控的内容,来看如何根据tag进一步获取我们想要的监控信息。

根据tag进行筛选

可以用下面的语法来对tag进行筛选

/actuator/metrics/http.server.requests?tag=uri:/actuator/metrics

上面的地址可以只关注uri=/actuator/metrics的指标,可以看到该接口一共被访问了多少次,最慢的情况下耗时了多久等。

SpringBoot2.0 Actuator监控指标分析
序号参数参数说明是否监控监控手段重要度
JVM
1jvm.memory.maxJVM最大内存
2jvm.memory.committedJVM可用内存展示并监控堆内存和Metaspace重要
3jvm.memory.usedJVM已用内存展示并监控堆内存和Metaspace重要
4jvm.buffer.memory.usedJVM缓冲区已用内存
5jvm.buffer.count当前缓冲区数
6jvm.threads.daemonJVM守护线程数显示在监控页面
7jvm.threads.liveJVM当前活跃线程数显示在监控页面;监控达到阈值时报警重要
8jvm.threads.peakJVM峰值线程数显示在监控页面
9jvm.classes.loaded加载classes数
10jvm.classes.unloaded未加载的classes数
11jvm.gc.memory.allocatedGC时,年轻代分配的内存空间
12jvm.gc.memory.promotedGC时,老年代分配的内存空间
13jvm.gc.max.data.sizeGC时,老年代的最大内存空间
14jvm.gc.live.data.sizeFullGC时,老年代的内存空间
15jvm.gc.pauseGC耗时显示在监控页面
TOMCAT
16tomcat.sessions.createdtomcat已创建session数
17tomcat.sessions.expiredtomcat已过期session数
18tomcat.sessions.active.currenttomcat活跃session数
19tomcat.sessions.active.maxtomcat最多活跃session数显示在监控页面,超过阈值可报警或者进行动态扩容重要
20tomcat.sessions.alive.max.secondtomcat最多活跃session数持续时间
21tomcat.sessions.rejected超过session最大配置后,拒绝的session个数显示在监控页面,方便分析问题
22tomcat.global.error错误总数显示在监控页面,方便分析问题
23tomcat.global.sent发送的字节数
24tomcat.global.request.maxrequest最长时间
25tomcat.global.request全局request次数和时间
26tomcat.global.received全局received次数和时间
27tomcat.servlet.requestservlet的请求次数和时间
28tomcat.servlet.errorservlet发生错误总数
29tomcat.servlet.request.maxservlet请求最长时间
30tomcat.threads.busytomcat繁忙线程显示在监控页面,据此检查是否有线程夯住
31tomcat.threads.currenttomcat当前线程数(包括守护线程)显示在监控页面重要
32tomcat.threads.config.maxtomcat配置的线程最大数显示在监控页面重要
33tomcat.cache.accesstomcat读取缓存次数
34tomcat.cache.hittomcat缓存命中次数
CPU…
35system.cpu.countCPU数量
36system.load.average.1mload average超过阈值报警重要
37system.cpu.usage系统CPU使用率
38process.cpu.usage当前进程CPU使用率超过阈值报警
39http.server.requestshttp请求调用情况显示10个请求量最大,耗时最长的URL;统计非200的请求量重要
40process.uptime应用已运行时间显示在监控页面
41process.files.max允许最大句柄数配合当前打开句柄数使用
42process.start.time应用启动时间点显示在监控页面
43process.files.open当前打开句柄数监控文件句柄使用率,超过阈值后报警重要
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值