Spring Boot Actuator 使用介绍

近期在看《Spring Cloud 微服务实战》,由于时间过去几年,对于Actuator监控端点的介绍过时,故作此文更新一下。

Spring Boot 版本:2.5.3

初识 Actuator

在现有的Spring Boot应用中引入该模块非常简单,只需要在pom.xmldependencies节点中,新增spring-boot-starter-actuator的依赖即可,具体如下:

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

通过增加该依赖之后,重新启动应用。此时,我们可以在控制台中看到如下图所示的输出:
在这里插入图片描述
上图显示在路径/actuator暴露了一个端点,端点并非我们自己在程序中创建,而是由spring-boot-starter-actuator模块根据应用依赖和配置自动创建出来的监控和管理端点。通过这些端点,我们可以实时的获取应用的各项监控指标,比如:访问/actuator/health端点,我们可以获得如下返回的应用健康信息:

{
	status: "UP"
}

原文中列出的大量的端点哪去了呢?经查找是因为在web情景下为了安全没有暴露出来,因此我们需要在application.properties文件中设置:

management.endpoints.web.exposure.include=*

再次启动应用,可有如下输出:
在这里插入图片描述
通过访问 http://localhost:8080/actuator,可能看到所有端点:

{
	_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
		},
		configprops-prefix: {
			href: "http://localhost:8080/actuator/configprops/{prefix}",
			templated: true
		},
		env: {
			href: "http://localhost:8080/actuator/env",
			templated: false
		},
		env-toMatch: {
			href: "http://localhost:8080/actuator/env/{toMatch}",
			templated: true
		},
		loggers-name: {
			href: "http://localhost:8080/actuator/loggers/{name}",
			templated: true
		},
		loggers: {
			href: "http://localhost:8080/actuator/loggers",
			templated: false
		},
		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
		}
	}
}

通过点击具体的链接,获取更详细的描述。

原生端点

通过在前一节中添加 spring-boot-starter-actuator模块,我们已经对它有了一个初步的认识。接下来,我们详细介绍一下spring-boot-starter-actuator模块中已经实现的一些原生端点。如果根据端点的作用来说,我们可以原生端点分为三大类:

  • 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
  • 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
  • 操作控制类:提供了对应用的关闭等操作类功能。
    下面我们来详细了解一下这三类端点都分别可以为我们提供怎么样的有用信息和强大功能,以及我们如何去扩展和配置它们。

应用配置类

由于Spring Boot为了改善传统Spring应用繁杂的配置内容,采用了包扫描和自动化配置的机制来加载原本集中于xml文件中的各项内容。虽然这样的做法,让我们的代码变得非常简洁,但是整个应用的实例创建和依赖关系等信息都被离散到了各个配置类的注解上,这使得我们分析整个应用中资源和实例的各种关系变得非常的困难。而这类端点就可以帮助我们轻松的获取一系列关于Spring 应用配置内容的详细报告,比如:自动化配置的报告、Bean创建的报告、环境属性的报告等。

  • /conditions(原/autoconfig):该端点用来获取应用的自动化配置报告,其中包括所有自动化配置的候选项。同时还列出了每个候选项自动化配置的各个先决条件是否满足。所以,该端点可以帮助我们方便的找到一些自动化配置为什么没有生效的具体原因。该报告内容将自动化配置内容分为两部分:
    • positiveMatches 中返回的是条件匹配成功的自动化配置
    • negativeMatches 中返回的是条件匹配不成功的自动化配置
    • unconditionalClasse 未设置条件的自动化配置
{
	contexts: {
		application: {
			positiveMatches: {
				AuditEventsEndpointAutoConfiguration: [
					{
						condition: "OnAvailableEndpointCondition",
						message: "@ConditionalOnAvailableEndpoint no property management.endpoint.auditevents.enabled found so using endpoint default; @ConditionalOnAvailableEndpoint marked as exposed by a 'management.endpoints.web.exposure' property"
					},
					...
				],
				...
			},
			negativeMatches: {
				RabbitHealthContributorAutoConfiguration: {
					notMatched: [
						{
							condition: "OnClassCondition",
							message: "@ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate'"
						}
					],
					...
			},
			unconditionalClasses: [
				"org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.availability.AvailabilityHealthContributorAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration",
				"org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration",
				"org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.metrics.integration.IntegrationMetricsAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration",
				"org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration",
				"org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration",
				"org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration"
			]
	],

从如上示例中我们可以看到,每个自动化配置候选项中都有一系列的条件,比如上面没有成功匹配的RabbitHealthContributorAutoConfiguration配置,它的先决条件就是需要在工程中包含org.springframework.amqp.rabbit.core.RabbitTemplate类,由于我们没有引入相关的依赖,它就不会执行自动化配置内容。所以,当我们发现有一些期望的配置没有生效时,就可以通过该端点来查看没有生效的具体原因。

  • /beans:该端点用来获取应用上下文中创建的所有Bean。
{
	contexts: {
		application: {
			beans: {
				endpointCachingOperationInvokerAdvisor: {
					aliases: [ ],
					scope: "singleton",
					type: "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
					resource: "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
					dependencies: [
						"org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration",
						"environment"
					]
				},
				...
			},
			parentId: null
		}
	}
}

如上示例中,我们可以看到在每个bean都以键值对出现,key为Bean的名称,value是个集合,都包含了下面这几个信息:

- scope:Bean的作用域
- type:Bean的Java类型
- reource:class文件的具体路径
- dependencies:依赖的Bean名称
  • /configprops:该端点用来获取应用中配置的属性信息报告。从下面该端点返回示例的片段中,我们看到返回了关于该短信的配置信息,prefix属性代表了属性的配置前缀,properties代表了各个属性的名称和值。所以,我们可以通过该报告来看到各个属性的配置路径,比如我们要关闭该端点,就可以通过使用endpoints.configprops.enabled=false来完成设置。
  • /env:该端点与/configprops不同,它用来获取应用所有可用的环境属性报告。包括:环境变量、JVM属性、应用的配置配置、命令行中的参数。从下面该端点返回的示例片段中,我们可以看到它不仅返回了应用的配置属性,还返回了系统属性、环境变量等丰富的配置信息,其中也包括了应用还没有没有使用的配置。所以它可以帮助我们方便地看到当前应用可以加载的配置信息,并配合@ConfigurationProperties注解将它们引入到我们的应用程序中来进行使用。另外,为了配置属性的安全,对于一些类似密码等敏感信息,该端点都会进行隐私保护,但是我们需要让属性名中包含:password、secret、key这些关键词,这样该端点在返回它们的时候会使用*来替代实际的属性值。
  • /mappings:该端点用来返回所有Spring MVC的控制器映射关系报告。从下面的示例片段中,我们可以看该报告的信息与我们在启用Spring MVC的Web应用时输出的日志信息类似,其中bean属性标识了该映射关系的请求处理器,method属性标识了该映射关系的具体处理类和处理函数。
  • /info:该端点用来返回一些应用自定义的信息。默认情况下,该端点只会返回一个空的json内容。我们可以在application.properties配置文件中通过info前缀来设置一些属性

度量指标类

  • /metrics:该端点用来返回当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息等。
{
	names: [
		"http.server.requests",
		"jvm.buffer.count",
		"jvm.buffer.memory.used",
		"jvm.buffer.total.capacity",
		"jvm.classes.loaded",
		"jvm.classes.unloaded",
		"jvm.gc.live.data.size",
		"jvm.gc.max.data.size",
		"jvm.gc.memory.allocated",
		"jvm.gc.memory.promoted",
		"jvm.gc.pause",
		"jvm.memory.committed",
		"jvm.memory.max",
		"jvm.memory.used",
		"jvm.threads.daemon",
		"jvm.threads.live",
		"jvm.threads.peak",
		"jvm.threads.states",
		"logback.events",
		"process.cpu.usage",
		"process.files.max",
		"process.files.open",
		"process.start.time",
		"process.uptime",
		"system.cpu.count",
		"system.cpu.usage",
		"system.load.average.1m",
		"tomcat.sessions.active.current",
		"tomcat.sessions.active.max",
		"tomcat.sessions.alive.max",
		"tomcat.sessions.created",
		"tomcat.sessions.expired",
		"tomcat.sessions.rejected"
	]
}

以上列出了key,通过将key加在链接后面,即可查看详情数据,例如想了解jvm内存使用,访问http://localhost:8080/actuator/metrics/jvm.memory.used,结果如下:

{
	name: "jvm.memory.used",
	description: "The amount of used memory",
	baseUnit: "bytes",
	measurements: [
		{
			statistic: "VALUE",
			value: 108388088
		}
	],
	availableTags: [
		{
			tag: "area",
			values: [
				"heap",
				"nonheap"
			]
		},
		{
			tag: "id",
			values: [
				"Compressed Class Space",
				"PS Survivor Space",
				"PS Old Gen",
				"Metaspace",
				"PS Eden Space",
				"Code Cache"
			]
		}
	]
}

在自定义标量值时,Springboot1.5.x支持的org.springframework.boot.actuate.metrics.CounterServiceorg.springframework.boot.actuate.metrics.GaugeService已不再支持,改用注解。即:
@Endpoint@WebEndpoint@EndpointWebExtension 上的操作将使用 Jersey、Spring MVC 或 Spring WebFlux 通过 HTTP 自动暴露。
写代码如下:

import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
@WebEndpoint(id = "testEndpoint")
public class HelloController {

    @ReadOperation
    public HashMap<String, Object> index() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "sky");
        hashMap.put("age", 24);
        return hashMap;
    }
}

就可以访问http://localhost:8080/actuator/testEndpoint,得出结果如下:

{
	name: "sky",
	age: 24
}
  • /health:该端点在一开始的示例中我们已经使用过了,它用来获取应用的各类健康指标信息。在spring-boot-starter-actuator模块中自带实现了一些常用资源的健康指标检测器。

操作控制类

在原生端点中,只提供了一个用来关闭应用的端点:/shutdown。我们可以通过如下配置开启它:

management.endpoint.shutdown.enabled=true

在配置了上述属性之后,只需要访问该应用的/shutdown端点就能实现关闭该应用的远程操作。由于开放关闭应用的操作本身是一件非常危险的事,所以真正在线上使用的时候,我们需要对其加入一定的保护机制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot Actuator是一组可插拔的管理端点,它提供了有关应用程序运行状况的信息,例如健康状况、内存使用情况、日志记录等。下面是Spring Boot Actuator使用方法和案例。 1. 添加Spring Boot Actuator依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 2. 配置Spring Boot Actuator 在application.properties文件中添加以下配置: ```properties # 启用所有端点 management.endpoints.web.exposure.include=* # 启用health和info端点 management.endpoints.web.exposure.include=health,info # 配置端点访问路径 management.endpoints.web.base-path=/actuator ``` 3. 访问Spring Boot Actuator端点 启动Spring Boot应用程序后,可以通过以下URL访问Actuator端点: - /actuator/health:应用程序的健康状况。 - /actuator/info:应用程序的信息。 - /actuator/metrics:应用程序的度量指标。 - /actuator/loggers:应用程序的日志记录器。 - /actuator/httptrace:应用程序的HTTP跟踪信息。 - /actuator/env:应用程序的环境变量。 例如,访问/actuator/health端点可以得到应用程序的健康状况: ```json { "status": "UP" } ``` 访问/actuator/metrics端点可以得到应用程序的度量指标: ```json { "counter.status.200.root": 1, "gauge.response.root": 50, "gauge.response.sample": 50 } ``` 访问/actuator/loggers端点可以得到应用程序的日志记录器: ```json { "configuredLevel": "INFO", "effectiveLevel": "INFO", "name": "root" } ``` 访问/actuator/env端点可以得到应用程序的环境变量: ```json { "profiles": [ "default" ], "systemProperties": { "java.runtime.name": "Java(TM) SE Runtime Environment", "user.timezone": "Asia/Shanghai", "java.version": "1.8.0_241", "os.name": "Mac OS X", "user.country": "CN", "user.language": "zh", "java.vm.specification.version": "1.8", "user.home": "/Users/username", "java.awt.headless": "true", "file.separator": "/", "java.specification.vendor": "Oracle Corporation", "java.library.path": "/Users/username/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.", "java.vm.name": "Java HotSpot(TM) 64-Bit Server VM", "user.dir": "/Users/username/workspace/demo", "java.runtime.version": "1.8.0_241-b07", "os.version": "10.15.6", "java.vendor.url": "http://java.oracle.com/", "java.vm.vendor": "Oracle Corporation", "spring.output.ansi.enabled": "ALWAYS", "file.encoding": "UTF-8", "java.vm.specification.vendor": "Oracle Corporation", "java.vendor": "Oracle Corporation", "java.vm.version": "25.241-b07", "line.separator": "\n", "user.name": "username", "java.class.version": "52.0", "java.vendor.url.bug": "http://bugreport.sun.com/bugreport/", "sun.cpu.endian": "little", "sun.io.unicode.encoding": "UnicodeBig", "spring.datasource.password": "******", "spring.datasource.url": "jdbc:mysql://localhost:3306/demo", "spring.datasource.username": "root", "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver", "spring.jpa.database-platform": "org.hibernate.dialect.MySQL5InnoDBDialect", "spring.jpa.hibernate.ddl-auto": "create-drop", "spring.jpa.show-sql": "true", "spring.jpa.properties.hibernate.format_sql": "true" } } ``` 4. 自定义Spring Boot Actuator端点 除了使用默认的Actuator端点外,还可以自定义自己的端点。只需要实现Endpoint接口即可。 例如,以下是一个自定义的端点,它返回当前时间: ```java @Component public class TimeEndpoint implements Endpoint<Long> { @Override public String getId() { return "time"; } @Override public boolean isEnabled() { return true; } @Override public boolean isSensitive() { return false; } @Override public Long invoke() { return System.currentTimeMillis(); } } ``` 启动应用程序后,可以通过访问/actuator/time端点来获取当前时间。 以上就是Spring Boot Actuator使用方法和案例,它可以帮助我们更好地管理和监控Spring Boot应用程序的运行状况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值