Spring-Boot-Actuator入门

2021-12-21  本文已影响0人  我是晓梦啊

源码地址:https://gitee.com/xiaoyao777/spring-boot-actuator.git

1.首先创建一个SpringBoot工程,然后引入一下Jar

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
         <!--这个就是SpringBootActuator核心包了-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.启动项目

image.png

有这个说明Actuator算是加载上了
3.访问SpringBootActuator

http://127.0.0.1:8080/actuator
image.png

3.health端点

可以看到有很多端点我们看一下health

http://localhost:8080/actuator/health
image.png

health这个是健康检查,那么检查的是啥子东西呢?
检查的是资源,啥子资源。我们加一个配置。

#展示health的详情
management.endpoint.health.show-details=always
image.png

我们重新启动一下项目再次访问health。


image.png
{
    "status": "UP",
    "components": {
        //磁盘
        "diskSpace": {
            "status": "UP",
            "details": {
                //磁盘总容量
                "total": 510770802688,
                 //磁盘剩余容量
                "free": 428996915200,
                 //磁盘可用最小空间
                "threshold": 10485760,
                "exists": true
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}

更多想起的参数配置可以看官方文档

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.endpoints.enabling

如果看不懂英文文档也没有问题还有中文文档,而且中文文档还可以下载

https://www.bookstack.cn/read/springboot/spilt.2.pages-production-ready.md#8rnaz8
image.png

然后我们看一下status值代表的什么意思


image.png

也就是status有四个取值

UP:正常
DOWN:遇到问题,也就是所谓down机了
OUT_OF_SERVICE:资源未在使用,或者不能使用
UNKNOWN:我也不知道啥问题

4.Actuator 的 REST 接口

Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。Actuator 提供了 13 个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。

GET请求:

/autoconfig:提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
/configprops:描述配置属性(包含默认值)如何注入Bean
/beans:描述应用程序上下文里全部的Bean,以及它们的关系
/dump:获取线程活动的快照
/env:获取全部环境属性
/env/{name}:根据名称获取特定的环境属性值
/health:报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
/info:获取应用程序的定制信息,这些信息由info打头的属性提供
/mappings:描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
/metrics:报告各种应用程序度量信息,比如内存用量和HTTP请求计数
/metrics/{name}:报告指定名称的应用程序度量值
/trace:提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

POST请求:

/shutdown:关闭应用程序,要求endpoints.shutdown.enabled设置为true

5.激活所有的端点

#可以看到除了/shutdown之外的其他所有端点。
management.endpoints.web.exposure.include=*
image.png image.png

如果开放几个端点就用逗号隔开好了

#可以看到除了/shutdown之外的其他所有端点。
#management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=health,info
image.png

源码地址:

https://gitee.com/xiaoyao777/spring-boot-actuator.git
上一篇下一篇

猜你喜欢

热点阅读