SpringBoot配置Actuator组件,实现系统监控

 更新时间:2021年06月19日 09:21:57   作者:知了一笑  
在生产环境中,需要实时或定期监控服务的可用性。Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。

一、Actuator简介

监控分类

  • 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
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base

以上就是SpringBoot配置Actuator组件,实现系统监控的详细内容,更多关于SpringBoot配置Actuator的资料请关注脚本之家其它相关文章!

相关文章

  • java多线程实现文件下载

    java多线程实现文件下载

    这篇文章主要为大家详细介绍了java多线程实现文件下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Java并发编程之同步容器

    Java并发编程之同步容器

    这篇文章主要介绍了Java并发编程之同步容器,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • Java的PriorityBlockingQueue优先级阻塞队列代码实例

    Java的PriorityBlockingQueue优先级阻塞队列代码实例

    这篇文章主要介绍了Java的PriorityBlockingQueue优先级阻塞队列代码实例,PriorityBlockingQueue顾名思义是带有优先级的阻塞队列,为了实现按优先级弹出数据,存入其中的对象必须实现comparable接口自定义排序方法,需要的朋友可以参考下
    2023-12-12
  • SpringBoot整合mybatis-generator-maven-plugin的方法

    SpringBoot整合mybatis-generator-maven-plugin的方法

    这篇文章主要介绍了SpringBoot整合mybatis-generator-maven-plugin,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Java如何使用Optional与Stream取代if判空逻辑(JDK8以上)

    Java如何使用Optional与Stream取代if判空逻辑(JDK8以上)

    这篇文章主要给大家介绍了关于Java如何使用Optional与Stream取代if判空逻辑(JDK8以上)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • java实现把一个List集合拆分成多个的操作

    java实现把一个List集合拆分成多个的操作

    这篇文章主要介绍了java实现把一个List集合拆分成多个的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Spring中的Aware接口及应用场景详解

    Spring中的Aware接口及应用场景详解

    这篇文章主要介绍了Spring中的Aware接口及应用场景,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • SpringBoot实现定时任务的三种方式小结

    SpringBoot实现定时任务的三种方式小结

    这篇文章主要介绍了SpringBoot实现定时任务的三种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot实战之处理异常案例详解

    SpringBoot实战之处理异常案例详解

    这篇文章主要介绍了SpringBoot实战之处理异常案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • 使用Swing绘制动态时钟

    使用Swing绘制动态时钟

    这篇文章主要为大家详细介绍了使用Swing绘制动态时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12

最新评论