Springboot2.x actuator

 一、Springboot2.x的actuator使用

1.1、添加依赖包

compile 'org.springframework.boot:spring-boot-starter-actuator:2.3.8.RELEASE'

Springcloud Gateway项目中的springboot是2.x的,访问actuator相关

1.2、浏览器测试

Spring Boot 新旧版本访问地址区别:端点基础路径由 / 调整到 /actuator 。如:/info 调整为 /actuator/info

二、常见actuator端点

Actuator endpoints 端点

路径省略前缀:/actuator

路径HTTP 动作描述
/conditions GET 提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
/configprops GET 描述配置属性(包含默认值)如何注入 Bean
/caches GET 获取所有的 Cachemanager
/caches/{cache} DELETE 移除某个 CacheManager
/beans GET 描述应用程序上下文全部 bean,以及他们的关系
/threaddump GET 获取线程活动快照
/env GET 获取全部环境属性
/env/{toMatch} GET 获取指定名称的特定环境的属性值
/health GET 报告应用长须的健康指标,由 HealthIndicator 的实现提供
/httptrace GET 提供基本的 HTTP 请求跟踪信息(时间戳、HTTP 头等)
/info GET 获取应用程序定制信息,由 Info 开头的属性提供
/loggers GET 获取 bean 的日志级别
/loggers/{name} GET 获取某个 包、类 路径端点的日志级别
/loggers/{name} POST 新增某个 包、类 路径端点的日志级别
/mappings GET 描述全部的 URL 路径,以及它们和控制器(包含Actuator端点)的映射关系
/metrics GET 报告各种应用程序度量信息,如:内存用量、HTTP 请求计数
/metrics/{name} GET 根据名称获取度量信息
/shutdown POST 关闭应用,要求 management.endpoint.shutdown.enabled 为 true
/scheduledtasks GET 获取所有定时任务信息

SpringCloud中默认默认开启了 /actuator/health/actuator/info 端点,其他端点都屏蔽掉了。如果需要开启,自定义开启 endpoints 即可

#暴露部分端点
management.endpoints.web.exposure.include: ['health','info','beans','env']

三、actuator配置说明

上面只是默认提供的 url(出于安全考虑,除/ health 和 / info 之外的所有执行器默认都是禁用的),可通过配置文件来配置哪些地址可以访问,列举几个例子:

#暴露部分端点
management.endpoints.web.exposure.include: ['health','info','beans','env']
#暴露所有端点
#management.endpoints.web.exposure.include: '*'
#不暴露beans端点
#management.endpoints.web.exposure.exclude=beans

建议:在设置 management.endpoints.web.exposure.include 之前,请确保暴露的执行器不包含敏感信息获取;或者将禁用的端点将从应用程序上下文中完全删除;或者通过将其放置在防火墙进行控制,不对外进行使用;或者用 Spring Boot Admin 进行管理;或者用自己编写脚本监控。

四、实现优雅关机

4.1、配置

#启用shutdown
management.endpoint.shutdown.enabled=true
#暴露所有端点
management.endpoints.web.exposure.include=*
# 自定义管理端点的前缀(保证安全)
management.endpoints.web.base-path=/MyActuator
# 自定义端口
management.server.port=12581
# 不允许远程管理连接(不允许外部调用保证安全)
management.server.address=127.0.0.1

4.2、测试

需要post请求才行

五、示例

/actuator/env显示来自 Spring 的 ConfigurableEnvironment 的属性

/actuator/health查看应用健康指标

/actuator/metrics: 查看应用基本指标列表

/actuator/metrics/{name}: 通过上述列表,查看具体指标

/actuator/threaddump:查看线程相关信息

/actuator/beans:查看bean装载信息

actuator/conditions: /conditions 端点可以看到为什么有这个 bean,SpringBoot 自动配置构建与 Spring 的条件化配置之上,提供了众多的 @Conditional 注解的配置类,根据 @Conditional 条件决定是否自动装配这些 bean。/conditions 端点提供了一个报告,列出了所有条件,根据条件是否通过进行分组

/gateway/routes:网关配置的路由信息

#暴露部分端点
management.endpoints.web.exposure.include: ['health','info','beans','conditions','env','metrics','threaddump','gateway']

 参考:https://blog.csdn.net/zh199609/article/details/91038873/

原文地址:https://www.cnblogs.com/duanxz/p/14794051.html