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

版权声明:转载请注明出处 https://blog.csdn.net/cowbin2012/article/details/85251524

源码地址

添加依赖

<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
1 jvm.memory.max JVM最大内存
2 jvm.memory.committed JVM可用内存 展示并监控堆内存和Metaspace 重要
3 jvm.memory.used JVM已用内存 展示并监控堆内存和Metaspace 重要
4 jvm.buffer.memory.used JVM缓冲区已用内存
5 jvm.buffer.count 当前缓冲区数
6 jvm.threads.daemon JVM守护线程数 显示在监控页面
7 jvm.threads.live JVM当前活跃线程数 显示在监控页面;监控达到阈值时报警 重要
8 jvm.threads.peak JVM峰值线程数 显示在监控页面
9 jvm.classes.loaded 加载classes数
10 jvm.classes.unloaded 未加载的classes数
11 jvm.gc.memory.allocated GC时,年轻代分配的内存空间
12 jvm.gc.memory.promoted GC时,老年代分配的内存空间
13 jvm.gc.max.data.size GC时,老年代的最大内存空间
14 jvm.gc.live.data.size FullGC时,老年代的内存空间
15 jvm.gc.pause GC耗时 显示在监控页面
TOMCAT
16 tomcat.sessions.created tomcat已创建session数
17 tomcat.sessions.expired tomcat已过期session数
18 tomcat.sessions.active.current tomcat活跃session数
19 tomcat.sessions.active.max tomcat最多活跃session数 显示在监控页面,超过阈值可报警或者进行动态扩容 重要
20 tomcat.sessions.alive.max.second tomcat最多活跃session数持续时间
21 tomcat.sessions.rejected 超过session最大配置后,拒绝的session个数 显示在监控页面,方便分析问题
22 tomcat.global.error 错误总数 显示在监控页面,方便分析问题
23 tomcat.global.sent 发送的字节数
24 tomcat.global.request.max request最长时间
25 tomcat.global.request 全局request次数和时间
26 tomcat.global.received 全局received次数和时间
27 tomcat.servlet.request servlet的请求次数和时间
28 tomcat.servlet.error servlet发生错误总数
29 tomcat.servlet.request.max servlet请求最长时间
30 tomcat.threads.busy tomcat繁忙线程 显示在监控页面,据此检查是否有线程夯住
31 tomcat.threads.current tomcat当前线程数(包括守护线程) 显示在监控页面 重要
32 tomcat.threads.config.max tomcat配置的线程最大数 显示在监控页面 重要
33 tomcat.cache.access tomcat读取缓存次数
34 tomcat.cache.hit tomcat缓存命中次数
CPU…
35 system.cpu.count CPU数量
36 system.load.average.1m load average 超过阈值报警 重要
37 system.cpu.usage 系统CPU使用率
38 process.cpu.usage 当前进程CPU使用率 超过阈值报警
39 http.server.requests http请求调用情况 显示10个请求量最大,耗时最长的URL;统计非200的请求量 重要
40 process.uptime 应用已运行时间 显示在监控页面
41 process.files.max 允许最大句柄数 配合当前打开句柄数使用
42 process.start.time 应用启动时间点 显示在监控页面
43 process.files.open 当前打开句柄数 监控文件句柄使用率,超过阈值后报警 重要

猜你喜欢

转载自blog.csdn.net/cowbin2012/article/details/85251524