SpringBoot开发案例之Actuator健康监控

spring 1年前 ⋅ 969 阅读


前言

秒杀案例进入实际生产环境中,需要实时或定期监控服务的可用性。Spring Boot 的 actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。

集成

pom.xml中引入以下:

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

application.properties配置如下:

#监控的HTTP端口 (如果不指定,则使用和Server相同的端口)
management.port=20886
#忽略拦截
management.security.enabled=false
#当前应用信息
info.app.version=v1.0.0
info.app.name=爪哇笔记
info.app.email=345849402@qq.com
info.app.url=https://blog.52itstyle.vip
#开启shutdown远程关闭功能
#访问:http://localhost:20886/shutdown 关闭服务
endpoints.shutdown.enabled=true

详细使用说明:

HTTP方法路径描述鉴权
GETautoconfig查看自动配置的使用情况true
GETconfigprops查看配置属性,包括默认配置true
GETbeans查看bean及其关系列表true
GETdump打印线程栈true
GETenv查看所有环境变量true
GETenv/{name}查看具体变量值true
GEThealth查看应用健康指标false
GETinfo查看应用信息false
GETmappings查看所有url映射true
GETmetrics查看应用基本指标true
GETmetrics/{name}查看具体指标true
POSTshutdown关闭应用true
GETtrace查看基本追踪信息true

举例 /info:

{
    "app": {
        "url": "https://blog.52itstyle.vip",
        "email": "345849402@qq.com",
        "name": "爪哇笔记",
        "version": "v1.0.0"
    }
}

actuator 还会对一些集成的第三方应用进行健康检查,比如秒杀系统中用到的 redis、MySql 等等。

举例 /health:

{
    "status": "UP",
    "jms": {
        "status": "UP",
        "provider": "ActiveMQ"
    },
    "diskSpace": {
        "status": "UP",
        "total": 150325182464,
        "free": 74917441536,
        "threshold": 10485760
    },
    "redis": {
        "status": "UP",
        "version": "3.2.8"
    },
    "db": {
        "status": "UP",
        "database": "MySQL",
        "hello": 1
    }
}

安全

最重要的安全问题,通过这些 endpoints 会暴露出很多应用的信息,这里总结了一些安全措施:

  • 关闭指定的endpoint,在application.properties中配置*.enable=false。
  • 通过设置management.port=-1关闭endpoint的HTTP访问接口,或者是设置其他的端口,供内部的admin服务访问。
  • 设置本地访问,management.address=127.0.0.1,通过设置management.context-path=/admin,可以设置指定的根路径,然后通过Nginx鉴权代理访问。

参考

Endpoints

Complete Guide for Spring Boot Actuator

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: