SpringBoot

监控管理

通过引入spring-boot-starter-actuator,可以使用SpringBoot提供的准生产环境下的应用监控和管理功能。我们可以通过HTTP,JMX,SSH协议来进行操作,自动得到审计、健康等指标信息

SpringBoot整合监控管理

步骤

  1. 引入spring-boot-starter-actuator
  2. 开启访问端点
  3. 通过http方式访问监控端点
  4. 可以进行shutdown远程关闭(POST提交,此端点默认关闭)

测试运行

启动项目,先不进行配置,浏览器访问http://localhost:8080/actuator

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}

可以看出,默认只暴露了health和info两个端点

application.properties中添加配置

springboot 2.x版本配置暴露所有端点,http访问时默认需要加上/actuator前缀

management.endpoints.web.exposure.include=*

再次访问http://localhost:8080/actuator

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "auditevents": {
            "href": "http://localhost:8080/actuator/auditevents",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "conditions": {
            "href": "http://localhost:8080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8080/actuator/configprops",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/actuator/env/{toMatch}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        },
        "loggers": {
            "href": "http://localhost:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8080/actuator/threaddump",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://localhost:8080/actuator/scheduledtasks",
            "templated": false
        },
        "httptrace": {
            "href": "http://localhost:8080/actuator/httptrace",
            "templated": false
        },
        "mappings": {
            "href": "http://localhost:8080/actuator/mappings",
            "templated": false
        }
    }
}

显示了所有端点

修改默认根路径

management.endpoints.web.base-path=/

浏览器访问http://localhost:8080/health,显示如下

{
    "status": "UP"
}

health端点默认只显示"status":"UP",配置显示详细信息

management.endpoint.health.show-details=always

再次访问http://localhost:8080/health,显示如下

{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 64424505344,
                "free": 8456863744,
                "threshold": 10485760
            }
        }
    }
}

设置启用单个端点(/shutdown)

开启shutdown端点,可远程关闭应用,注意访问时需要post提交,除shutdown外其他端点默认启用

management.endpoint.shutdown.enabled=true

配置http访问端点的端口,如果改成-1会关闭所有端点

management.server.port=8081

现在需要访问http://localhost:8081/health才会显示端点信息

设置不暴露某个端点

management.endpoint.web.exposure.exclude=端点名

自定义健康健康状态指示器

  1. 编写一个指示器,实现HealthIndicator接口
  2. 指示器的名字是xxxHealthIndicator
  3. 加入容器中
@Component
public class MyAppHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return Health.down().withDetail("msg","服务异常").build();
    }
}

再次访问http://localhost:8081/health

{
    "status": "DOWN",
    "details": {
        "myApp": {
            "status": "DOWN",
            "details": {
                "msg": "服务异常"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 64424505344,
                "free": 8456863744,
                "threshold": 10485760
            }
        }
    }
}

health端点status变成了DOWN,也多了myApp的提示信息

端点详细信息

端点名 描述
autoconfig 所有自动配置信息
auditevents 审计事件
beans 所有Bean的信息
configprops 所有配置属性
dump 线程状态信息
env 当前环境信息
health 应用健康状况
info 当前应用信息
metrics 应用的各项指标
mappings 应用@RequestMapping映射路径
shutdown 关闭当前应用(默认不启用)
trace 追踪信息
原文地址:https://www.cnblogs.com/codeDD/p/12699591.html