您的位置:首页 > 编程语言 > Java开发

SpringBoot-Actuator应用监控以及优雅停机

2017-09-06 14:22 911 查看
Actuator是SpringBoot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


application.yml

management:
context-path: /management
health:
mail:
enabled: false


一、执行器端点(endpoints) 可用于监控应用及与应用进行交互,Spring Boot包含很

多内置的端点,你也可以添加自己的。例如, health 端点提供了应用的基本健康

信息。 端点暴露的方式取决于你采用的技术类型,大部分应用选择HTTP监控,端

点的ID映射到一个URL。例如, health 端点默认映射到 /management/health 。

下面的端点都是可用的:

HTTP方法 路径 描述 鉴权

GET /autoconfig 查看自动配置的使用情况 true

GET /configprops 查看配置属性,包括默认配置 true

GET /beans 查看bean及其关系列表 true

GET /dump 打印线程栈 true

GET /env 查看所有环境变量 true

GET /env/{name} 查看具体变量值 true

GET /health 查看应用健康指标 false

GET /info 查看应用信息 false

GET /mappings 查看所有url映射 true

GET /metrics 查看应用基本指标 true

GET /metrics/{name} 查看具体指标 true

POST/shutdown 关闭应用 true

GET /trace 查看基本追踪信息 true

如图



二、配置文件配置启用shutdown的HTTP访问

application.properties

#启用shutdown endpoint的HTTP访问
endpoints.shutdown.enabled=true
#不需要验证
endpoints.shutdown.sensitive=false


配置完成后可通过post请求,停止应用

返回结果:

{
"message": "Shutting down, bye..."
}


控制台:

734896 [Thread-12] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Close initiated...
734899 [Thread-12] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Closed.


增加安全验证

Maven依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


application.properties

endpoints.shutdown.sensitive=true
security.user.name=admin
security.user.password=123456


此时再次发送请求时,则需要进行登录操作后才能执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息