SpringBoot揭密:spring-boot-starter-actuator与应用监控

328 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

系统上线后其稳定性是至关重要的,开发者做到对系统运行状态及各种信息了如指掌,才有底气保证系统没有问题,很多开发者连通过运行数据来监控系统状态的意识都没有,系统上线稳定后是否稳定全凭运气和被动接收用户反馈才知道。

SpringBoot提供了非常成熟的应用监控模块SpringBoot Actuator,其中Actuator可理解为探测器,通过spring-boot-starter-actuator开箱即用,即可获得通过HTTP或者JMX方式暴露的EndPoint,EndPoint可理解为Spring MVC中类似于Controller的Handler,引入以下依赖,即可获取该功能:

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

配置management.endpoints.web.exposure.include=*,通过访问basePath:/actuator能看到目前提供的endpoint,典型的如health(可用于健康检查)、info、metrics(可用于指标监控)

{
	"_links": {
		"self": {
			"href": "http://localhost:8080/actuator",
			"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-path": {
			"href": "http://localhost:8080/actuator/health/{*path}",
			"templated": true
		},
		"info": {
			"href": "http://localhost:8080/actuator/info",
			"templated": false
		},
		"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
		},
		"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
		},
		"mappings": {
			"href": "http://localhost:8080/actuator/mappings",
			"templated": false
		}
	}
}

这些信息均是可以自定义扩展的,特别是health和metrics,对于指标数据,SpringBoot Actuator对接了各种监控系统,支持将指标数据上报到监控系统可视化及配置告警,如监控系统prometheus和可视化grafana,Actuator采用了MicroMeter技术来采集指标,MicroMeter可理解为类似于SLF4J的门面,可以将监控指标数据Meter注册到各监控系统的注册表MeterRegister中,最终通过监控系统所需的格式上报上去,如果对接prometheus,可引入以下依赖:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Actuator中引入了诸多自动装配类,但发现满足prometheus endpoint自动装配条件(@ConditionalOnClass(PrometheusMeterRegistry.class)普罗米修斯这个类正是上面依赖引入的)后即提供新的接口:http://localhost:8080/actuator/prometheus