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图片上传实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 如何使用Java统计gitlab代码行数

    如何使用Java统计gitlab代码行数

    这篇文章主要介绍了如何使用Java统计gitlab代码行数,实现方式通过git脚本将所有的项目拉下来并然后通过进行代码行数的统计,需要的朋友可以参考下
    2023-10-10
  • 如何用Spring发送电子邮件

    如何用Spring发送电子邮件

    这篇文章主要介绍了如何用Spring发送电子邮件,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2021-02-02
  • Java多线程中断机制三种方法及示例

    Java多线程中断机制三种方法及示例

    这篇文章主要介绍了Java多线程中断机制三种方法及示例,向大家分享了这三种方法的介绍几代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Java中临时文件目录的使用

    Java中临时文件目录的使用

    :Java提供了系统属性java.io.tmpdir来获取默认临时文件目录,适用于不同操作系统,使用Files.createTempFile方法创建临时文件,并在不需要时应及时删除,下面就来介绍一下
    2024-10-10
  • java的新特性反射机制应用及操作示例详解

    java的新特性反射机制应用及操作示例详解

    这篇文章主要为大家介绍了java的新特性反射机制的操作示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • SpringBoot项目集成Flyway详细过程

    SpringBoot项目集成Flyway详细过程

    今天带大家学习SpringBoot项目集成Flyway详细过程,文中有非常详细的介绍及代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • Spring Security实现5次密码错误触发账号自动锁定功能

    Spring Security实现5次密码错误触发账号自动锁定功能

    在现代互联网应用中,账号安全是重中之重,然而,暴力 破解攻击依然是最常见的安全威胁之一,攻击者通过自动化脚本尝试大量的用户名和密码组合,试图找到漏洞进入系统,所以为了解决这一问题,账号锁定机制被广泛应用,本文介绍了Spring Security实现5次密码错误触发账号锁定功能
    2024-12-12
  • idea插件生成jpa实体类的实现示例

    idea插件生成jpa实体类的实现示例

    本文主要介绍了idea插件生成jpa实体类的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01
  • Java消息队列RabbitMQ入门详解

    Java消息队列RabbitMQ入门详解

    这篇文章主要介绍了Java消息队列RabbitMQ入门详解,RabbitMQ是使用Erlang语言开发的开源消息队列系统,基于AMQP协议 来实现,AMQP的主要特征是面向消息、队列、路由(包括点对点和发布 /订阅)、可靠性、安全,需要的朋友可以参考下
    2023-07-07

最新评论