Spring Boot - Actuator 应用监控

在微服务架构中往往需要对服务各状态进行收集、监控、统计,否则整个整个集群将是一个可怕的黑盒。

Spring Boot提供了Actuator组件使我们可以通过restful API的方式获取服务运行中的各项指标,默认提供了以下监控指标:

我们也可以自己实现HealthIndicator接口自行提供监控指标。

同时Actuator还为我们的应用提供了优雅关机功能,相当于一个APP的管家了!


现在开始在自己的Spring Boot应用中使用Actuator。

开发环境

MAC \ STS \ JDK8.0 \ SpringBoot 2.1.3.RELEASE

起步依赖

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

配置

server.port=9001

# 允许通过actuator关闭此端点
management.endpoint.shutdown.enabled=true
# 对外暴露web端点,spring boot 2.x以后不配置则仅加载info、health
management.endpoints.web.exposure.include=*

启动应用程序

o.s.b.a.e.web.EndpointLinksResolver      : Exposing 16 endpoint(s) beneath base path '/actuator'

通过启动日志可以看到对外暴露了16个监控指标。

通过浏览器开打http://127.0.0.1:9001/actuator获得如下信息:

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

每个href属性可看到各个EndPoint提供的监控数据。具体内容可自行研究。


Actuator提供的监控信息是单个实例级别的,微服务架构中实例实例少则几十多则几百,想要管理这么多实例必然要搭建一个数据收集平台,可使用官方认可的第三方开源项目spring boot admin,详见:http://codecentric.github.io/spring-boot-admin/current/

跟着官方文档搭起来也是比较简单,有空再来写步骤...  搭好之后效果图如下,非常漂亮:

可直观地看到所有服务状态,可以进入某个服务查看单实例的配置数据、链路追踪、JVM数据、Web Mapping等等,实际上就是对Actuator数据做了收集及展示。

优雅下线

通过POST请求直接访问http://${IP}:${POST}/actuator/shutdown即可。

本文中为 curl -X POST 'http://127.0.0.1:9001/actuator/shutdown',执行后收到的信息为 {"message":"Shutting down, bye..."}。

查看进程,发现已经停止。查看日志,看到了一系列组件(JPA、HikariCP、定时任务...)的shutdown流程产生的日志,如果是注册在spring cloud中的服务,还能达到及时剔除的目的,相当优雅了!

猜你喜欢

转载自blog.csdn.net/t5721654/article/details/88798372