对于最新的稳定版本,请使用 Spring Boot 3.4.0spring-doc.cadn.net.cn

端点

Actuator 终端节点允许您监控应用程序并与之交互。 Spring Boot 包含许多内置端点,并允许您添加自己的端点。 例如,healthendpoint 提供基本的应用程序运行状况信息。spring-doc.cadn.net.cn

您可以启用或禁用每个单独的终端节点,并通过 HTTP 或 JMX 公开它们(使其可远程访问)。 当终端节点同时启用和公开时,该终端节点被视为可用。 仅当内置终端节点可用时,才会自动配置这些终端节点。 大多数应用程序选择通过 HTTP 进行公开,其中终端节点的 ID 和前缀/actuator映射到 URL。 例如,默认情况下,healthendpoint 映射到/actuator/health.spring-doc.cadn.net.cn

要了解有关 Actuator 的终端节点及其请求和响应格式的更多信息,请参阅 API 文档

以下与技术无关的终端节点可用:spring-doc.cadn.net.cn

身份证 描述

auditeventsspring-doc.cadn.net.cn

公开当前应用程序的审计事件信息。 需要一个AuditEventRepository豆。spring-doc.cadn.net.cn

beansspring-doc.cadn.net.cn

显示应用程序中所有 Spring bean 的完整列表。spring-doc.cadn.net.cn

cachesspring-doc.cadn.net.cn

公开可用的缓存。spring-doc.cadn.net.cn

conditionsspring-doc.cadn.net.cn

显示在 configuration 和 auto-configuration 类上评估的条件,以及它们匹配或不匹配的原因。spring-doc.cadn.net.cn

configpropsspring-doc.cadn.net.cn

显示所有@ConfigurationProperties. 需进行消毒spring-doc.cadn.net.cn

envspring-doc.cadn.net.cn

暴露 Spring 的ConfigurableEnvironment. 需进行消毒spring-doc.cadn.net.cn

flywayspring-doc.cadn.net.cn

显示已应用的任何 Flyway 数据库迁移。 需要一个或多个Flyway豆。spring-doc.cadn.net.cn

healthspring-doc.cadn.net.cn

显示应用程序运行状况信息。spring-doc.cadn.net.cn

httpexchangesspring-doc.cadn.net.cn

显示 HTTP 交换信息(默认情况下,最后 100 个 HTTP 请求-响应交换)。 需要一个HttpExchangeRepository豆。spring-doc.cadn.net.cn

infospring-doc.cadn.net.cn

显示任意应用程序信息。spring-doc.cadn.net.cn

integrationgraphspring-doc.cadn.net.cn

显示 Spring 集成图。 需要依赖spring-integration-core.spring-doc.cadn.net.cn

loggersspring-doc.cadn.net.cn

显示和修改应用程序中记录器的配置。spring-doc.cadn.net.cn

liquibasespring-doc.cadn.net.cn

显示已应用的任何 Liquibase 数据库迁移。 需要一个或多个Liquibase豆。spring-doc.cadn.net.cn

metricsspring-doc.cadn.net.cn

显示当前应用程序的 “metrics” 信息。spring-doc.cadn.net.cn

mappingsspring-doc.cadn.net.cn

显示所有@RequestMapping路径。spring-doc.cadn.net.cn

quartzspring-doc.cadn.net.cn

显示有关 Quartz Scheduler 作业的信息。 需进行消毒spring-doc.cadn.net.cn

scheduledtasksspring-doc.cadn.net.cn

显示应用程序中的计划任务。spring-doc.cadn.net.cn

sessionsspring-doc.cadn.net.cn

允许从 Spring Session 支持的会话存储中检索和删除用户会话。 需要使用 Spring Session 的基于 servlet 的 Web 应用程序。spring-doc.cadn.net.cn

shutdownspring-doc.cadn.net.cn

允许正常关闭应用程序。 仅在使用 jar 打包时有效。 默认处于禁用状态。spring-doc.cadn.net.cn

startupspring-doc.cadn.net.cn

显示由ApplicationStartup. 需要SpringApplication配置一个BufferingApplicationStartup.spring-doc.cadn.net.cn

threaddumpspring-doc.cadn.net.cn

执行线程转储。spring-doc.cadn.net.cn

如果您的应用程序是 Web 应用程序(Spring MVC、Spring WebFlux 或 Jersey),则可以使用以下附加端点:spring-doc.cadn.net.cn

身份证 描述

heapdumpspring-doc.cadn.net.cn

返回堆转储文件。 在 HotSpot JVM 上,HPROF-format 文件。 在 OpenJ9 JVM 上,PHD-format 文件。spring-doc.cadn.net.cn

logfilespring-doc.cadn.net.cn

返回日志文件的内容(如果logging.file.namelogging.file.path属性)。 支持使用 HTTPRange标头检索日志文件的部分内容。spring-doc.cadn.net.cn

prometheusspring-doc.cadn.net.cn

以 Prometheus 服务器可抓取的格式公开指标。 需要依赖micrometer-registry-prometheus.spring-doc.cadn.net.cn

启用终端节点

默认情况下,除shutdown已启用。 要配置端点的启用,请使用其management.endpoint.<id>.enabled财产。 以下示例启用shutdown端点:spring-doc.cadn.net.cn

management.endpoint.shutdown.enabled=true
management:
  endpoint:
    shutdown:
      enabled: true

如果您希望终端节点启用为选择加入而不是选择退出,请将management.endpoints.enabled-by-defaultproperty 设置为false并使用单个端点enabled属性以选择重新加入。 以下示例启用infoendpoint 并禁用所有其他 endpoints:spring-doc.cadn.net.cn

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
management:
  endpoints:
    enabled-by-default: false
  endpoint:
    info:
      enabled: true
已禁用的端点将从应用程序上下文中完全删除。 如果您只想更改终端节点所公开的技术,请使用includeexclude性能相反。

公开端点

默认情况下,仅通过 HTTP 和 JMX 公开运行状况终端节点。 由于 Endpoints 可能包含敏感信息,因此您应该仔细考虑何时公开它们。spring-doc.cadn.net.cn

要更改公开的终端节点,请使用以下特定于includeexclude性能:spring-doc.cadn.net.cn

财产 违约

management.endpoints.jmx.exposure.excludespring-doc.cadn.net.cn

management.endpoints.jmx.exposure.includespring-doc.cadn.net.cn

healthspring-doc.cadn.net.cn

management.endpoints.web.exposure.excludespring-doc.cadn.net.cn

management.endpoints.web.exposure.includespring-doc.cadn.net.cn

healthspring-doc.cadn.net.cn

includeproperty 列出公开的端点的 ID。 这excludeproperty 列出不应公开的端点的 ID。 这exclude属性优先于include财产。 您可以配置includeexclude属性中,其中包含终端节点 ID 列表。spring-doc.cadn.net.cn

例如,要仅公开healthinfoendpoints 的 JMX 上,请使用以下属性:spring-doc.cadn.net.cn

management.endpoints.jmx.exposure.include=health,info
management:
  endpoints:
    jmx:
      exposure:
        include: "health,info"

*可用于选择所有端点。 例如,要通过 HTTP 公开除envbeansendpoints,请使用以下属性:spring-doc.cadn.net.cn

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: "env,beans"
*在 YAML 中具有特殊含义,因此如果要包含(或排除)所有终端节点,请务必添加引号。
如果您的应用程序公开,我们强烈建议您同时保护终端节点
如果要在端点公开时实施自己的策略,可以注册一个EndpointFilter豆。

安全

出于安全考虑,只有/healthendpoint 默认通过 HTTP 公开。 您可以使用management.endpoints.web.exposure.include属性来配置公开的端点。spring-doc.cadn.net.cn

在设置management.endpoints.web.exposure.include,确保公开的 actuator 不包含敏感信息,通过将它们放置在防火墙后面进行保护,或者由类似 Spring Security 的东西进行保护。

如果 Spring Security 在类路径上,并且没有其他SecurityFilterChainBean 存在,则除/health由 Spring Boot 自动配置保护。 如果您定义了自定义SecurityFilterChainbean,Spring Boot 自动配置会回退,并允许您完全控制 actuator 访问规则。spring-doc.cadn.net.cn

如果您希望为 HTTP 端点配置自定义安全性(例如,仅允许具有特定角色的用户访问它们),Spring Boot 提供了一些方便的RequestMatcher可以与 Spring Security 结合使用的对象。spring-doc.cadn.net.cn

典型的 Spring Security 配置可能类似于以下示例:spring-doc.cadn.net.cn

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http.securityMatcher(EndpointRequest.toAnyEndpoint());
		http.authorizeHttpRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
		http.httpBasic(withDefaults());
		return http.build();
	}

}
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.Customizer.withDefaults
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.web.SecurityFilterChain

@Configuration(proxyBeanMethods = false)
class MySecurityConfiguration {

	@Bean
	fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
		http.securityMatcher(EndpointRequest.toAnyEndpoint()).authorizeHttpRequests { requests ->
			requests.anyRequest().hasRole("ENDPOINT_ADMIN")
		}
		http.httpBasic(withDefaults())
		return http.build()
	}

}

前面的示例使用EndpointRequest.toAnyEndpoint()将请求匹配到任何终端节点,然后确保所有终端节点都具有ENDPOINT_ADMIN角色。 其他几种匹配器方法也可用于EndpointRequest. 有关详细信息,请参阅 API 文档spring-doc.cadn.net.cn

如果您在防火墙后面部署应用程序,您可能希望无需身份验证即可访问所有 actuator 终端节点。 您可以通过更改management.endpoints.web.exposure.include属性,如下所示:spring-doc.cadn.net.cn

management.endpoints.web.exposure.include=*
management:
  endpoints:
    web:
      exposure:
        include: "*"

此外,如果存在 Spring Security,则需要添加自定义安全配置,以允许对端点进行未经身份验证的访问,如下例所示:spring-doc.cadn.net.cn

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http.securityMatcher(EndpointRequest.toAnyEndpoint());
		http.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll());
		return http.build();
	}

}
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.web.SecurityFilterChain

@Configuration(proxyBeanMethods = false)
class MySecurityConfiguration {

	@Bean
	fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
		http.securityMatcher(EndpointRequest.toAnyEndpoint()).authorizeHttpRequests { requests ->
			requests.anyRequest().permitAll()
		}
		return http.build()
	}

}
在上述两个示例中,配置仅适用于 actuator 终端节点。 由于 Spring Boot 的安全配置在存在任何SecurityFilterChainbean,您需要配置一个额外的SecurityFilterChainbean 替换为适用于应用程序其余部分的规则。

跨站点请求伪造保护

由于 Spring Boot 依赖于 Spring Security 的默认值,因此默认情况下 CSRF 保护是打开的。 这意味着需要POST(shutdown 和 loggers 端点)、一个PUTDELETE使用默认安全配置时收到 403 (禁止) 错误。spring-doc.cadn.net.cn

我们建议仅在您创建的服务由非浏览器客户端使用时才完全禁用 CSRF 保护。

您可以在 Spring Security 参考指南中找到有关 CSRF 保护的其他信息。spring-doc.cadn.net.cn

配置端点

终端节点会自动缓存对不采用任何参数的读取作的响应。 要配置终端节点缓存响应的时间量,请使用其cache.time-to-live财产。 以下示例将beansendpoint 的缓存设置为 10 秒:spring-doc.cadn.net.cn

management.endpoint.beans.cache.time-to-live=10s
management:
  endpoint:
    beans:
      cache:
        time-to-live: "10s"
management.endpoint.<name>prefix 唯一标识正在配置的终端节点。

清理敏感值

返回的信息/env,/configprops/quartzendpoints 可以是敏感的,因此默认情况下,值始终是完全清理的(替换为 )。******spring-doc.cadn.net.cn

只有在以下情况下,才能以未经清理的形式查看值:spring-doc.cadn.net.cn

show-values属性配置为以下值之一:spring-doc.cadn.net.cn

对于 HTTP 端点,如果用户已经过身份验证并且角色由端点的 roles 属性配置,则认为该用户已获得授权。 默认情况下,任何经过身份验证的用户都已获得授权。spring-doc.cadn.net.cn

对于 JMX 终端节点,所有用户始终获得授权。spring-doc.cadn.net.cn

以下示例允许具有admin角色来查看/envendpoint 的原始形式。 未经授权的用户或没有adminrole 将只看到经过净化的值。spring-doc.cadn.net.cn

management.endpoint.env.show-values=when-authorized
management.endpoint.env.roles=admin
management:
  endpoint:
    env:
      show-values: when-authorized
      roles: "admin"
此示例假定没有SanitizingFunctionbean 已定义。

用于 Actuator Web 端点的超媒体

将添加一个“发现页面”,其中包含指向所有终端节点的链接。 “发现页面”位于/actuator默认情况下。spring-doc.cadn.net.cn

要禁用“发现页面”,请将以下属性添加到您的应用程序属性中:spring-doc.cadn.net.cn

management.endpoints.web.discovery.enabled=false
management:
  endpoints:
    web:
      discovery:
        enabled: false

配置自定义管理上下文路径后,“发现页面” 会自动从/actuator到 Management 上下文的根目录。 例如,如果管理上下文路径为/management中,“发现”页面可从/management. 当 Management context path 设置为 时,将禁用发现页面,以防止与其他映射发生冲突。/spring-doc.cadn.net.cn

CORS 支持

跨域资源共享 (CORS) 是一种 W3C 规范,可让您以灵活的方式指定授权的跨域请求类型。 如果您使用 Spring MVC 或 Spring WebFlux,则可以配置 Actuator 的 Web 端点以支持此类场景。spring-doc.cadn.net.cn

CORS 支持默认处于禁用状态,并且只有在您设置了management.endpoints.web.cors.allowed-origins财产。 以下配置允许GETPOST来自example.com域:spring-doc.cadn.net.cn

management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST
management:
  endpoints:
    web:
      cors:
        allowed-origins: "https://example.com"
        allowed-methods: "GET,POST"
CorsEndpointProperties以获取完整的选项列表。

实现自定义端点

如果您添加了@Bean注解@Endpoint中,任何带有@ReadOperation,@WriteOperation@DeleteOperation通过 JMX 自动公开,在 Web 应用程序中还通过 HTTP 公开。 可以使用 Jersey、Spring MVC 或 Spring WebFlux 通过 HTTP 公开端点。 如果 Jersey 和 Spring MVC 都可用,则使用 Spring MVC。spring-doc.cadn.net.cn

以下示例公开返回自定义对象的读取作:spring-doc.cadn.net.cn

	@ReadOperation
	public CustomData getData() {
		return new CustomData("test", 5);
	}
	@ReadOperation
	fun getData(): CustomData {
		return CustomData("test", 5)
	}

您还可以使用@JmxEndpoint@WebEndpoint. 这些终端节点仅限于各自的技术。 例如@WebEndpoint仅通过 HTTP 公开,而不通过 JMX 公开。spring-doc.cadn.net.cn

您可以使用@EndpointWebExtension@EndpointJmxExtension. 这些注释允许您提供特定于技术的作来增强现有终端节点。spring-doc.cadn.net.cn

最后,如果您需要访问特定于 Web 框架的功能,则可以实现 servlet 或 Spring@Controller@RestController端点,但代价是它们无法通过 JMX 使用或使用不同的 Web 框架。spring-doc.cadn.net.cn

接收输入

终端节点上的作通过其参数接收输入。 当通过 Web 公开时,这些参数的值取自 URL 的查询参数和 JSON 请求正文。 当通过 JMX 公开时,参数将映射到 MBean作的参数。 默认情况下,参数是必需的。 可以通过使用@javax.annotation.Nullable@Nullable.spring-doc.cadn.net.cn

您可以将 JSON 请求正文中的每个根属性映射到终端节点的参数。 请考虑以下 JSON 请求正文:spring-doc.cadn.net.cn

{
	"name": "test",
	"counter": 42
}

您可以使用它来调用一个写入作,该作采用String nameint counter参数,如下例所示:spring-doc.cadn.net.cn

	@WriteOperation
	public void updateData(String name, int counter) {
		// injects "test" and 42
	}
	@WriteOperation
	fun updateData(name: String?, counter: Int) {
		// injects "test" and 42
	}
由于端点与技术无关,因此只能在方法签名中指定简单类型。 具体而言,使用CustomData类型定义namecounterproperties 的 API 请求。
要让 input 映射到 operation 方法的参数,实现端点的 Java 代码应该使用-parameters和实现端点的 Kotlin 代码应使用-java-parameters. 如果您使用 Spring Boot 的 Gradle 插件,或者如果您使用 Maven 和spring-boot-starter-parent.

输入类型转换

如有必要,传递给终端节点作方法的参数会自动转换为所需的类型。 在调用作方法之前,通过 JMX 或 HTTP 接收的输入会使用ApplicationConversionService以及任何ConverterGenericConverter使用@EndpointConverter.spring-doc.cadn.net.cn

自定义 Web 终端节点

@Endpoint,@WebEndpoint@EndpointWebExtension使用 Jersey、Spring MVC 或 Spring WebFlux 通过 HTTP 自动公开。 如果 Jersey 和 Spring MVC 都可用,则使用 Spring MVC。spring-doc.cadn.net.cn

Web 终端节点请求谓词

将为 Web 公开的终端节点上的每个作自动生成一个请求谓词。spring-doc.cadn.net.cn

路径

谓词的路径由终端节点的 ID 和 Web 公开的终端节点的基本路径确定。 默认基本路径为/actuator. 例如,ID 为sessions使用/actuator/sessions作为其在谓词中的路径。spring-doc.cadn.net.cn

您可以通过使用@Selector. 此类参数将作为 path 变量添加到 path 谓词中。 调用 endpoint作时,变量的值将传递到 operation 方法中。 如果要捕获所有剩余的 path 元素,可以添加@Selector(Match=ALL_REMAINING)添加到最后一个参数中,并使其成为与String[].spring-doc.cadn.net.cn

消耗

对于@WriteOperation(HTTPPOST) 使用请求正文,consumes子句的application/vnd.spring-boot.actuator.v2+json, application/json. 对于所有其他作,consumes子句为空。spring-doc.cadn.net.cn

生产

produces子句的produces属性的@DeleteOperation,@ReadOperation@WriteOperation附注。 该属性是可选的。 如果未使用,则produces子句是自动确定的。spring-doc.cadn.net.cn

如果作方法返回voidVoidproduces子句为空。 如果作方法返回Resourceproduces子句是application/octet-stream. 对于所有其他作,produces子句是application/vnd.spring-boot.actuator.v2+json, application/json.spring-doc.cadn.net.cn

Web 终端节点响应状态

终端节点作的默认响应状态取决于作类型(读取、写入或删除)以及作返回的内容(如果有)。spring-doc.cadn.net.cn

如果@ReadOperation返回一个值,则响应状态将为 200 (OK)。 如果未返回值,则响应状态将为 404 (Not Found)。spring-doc.cadn.net.cn

如果@WriteOperation@DeleteOperation返回一个值,则响应状态将为 200 (OK)。 如果未返回值,则响应状态将为 204 (No Content)。spring-doc.cadn.net.cn

如果调用作时没有必需的参数,或者使用无法转换为必需类型的参数,则不会调用作方法,并且响应状态将为 400 (Bad Request)。spring-doc.cadn.net.cn

Web 终端节点范围请求

您可以使用 HTTP 范围请求来请求 HTTP 资源的一部分。 当使用 Spring MVC 或 Spring Web Flux 时,返回Resource自动支持范围请求。spring-doc.cadn.net.cn

使用 Jersey 时,不支持范围请求。

Web 端点安全

Web 终端节点或特定于 Web 的终端节点扩展上的作可以接收当前的PrincipalSecurityContext作为方法参数。 前者通常与@javax.annotation.Nullable@Nullable为经过身份验证和未经身份验证的用户提供不同的行为。 后者通常用于使用其isUserInRole(String)方法。spring-doc.cadn.net.cn

健康信息

您可以使用运行状况信息来检查正在运行的应用程序的状态。 监控软件经常使用它来在生产系统出现故障时提醒某人。 由health端点依赖于management.endpoint.health.show-detailsmanagement.endpoint.health.show-components属性,可以使用以下值之一进行配置:spring-doc.cadn.net.cn

名字 描述

neverspring-doc.cadn.net.cn

详细信息永远不会显示。spring-doc.cadn.net.cn

when-authorizedspring-doc.cadn.net.cn

详细信息仅向授权用户显示。 授权角色可使用management.endpoint.health.roles.spring-doc.cadn.net.cn

alwaysspring-doc.cadn.net.cn

详细信息将向所有用户显示。spring-doc.cadn.net.cn

默认值为never. 当用户处于终端节点的一个或多个角色中时,即被视为已获得授权。 如果终端节点没有配置角色(默认),则所有经过身份验证的用户都被视为已获得授权。 您可以使用management.endpoint.health.roles财产。spring-doc.cadn.net.cn

如果您已保护应用程序并希望使用always,则您的安全配置必须允许经过身份验证和未经身份验证的用户访问 Health 终端节点。

运行状况信息是从HealthContributorRegistry(默认情况下,所有HealthContributor实例ApplicationContext). Spring Boot 包括许多自动配置的HealthContributorbeans 的 bean 中,您也可以编写自己的 bean 来编写。spring-doc.cadn.net.cn

一个HealthContributor可以是HealthIndicatorCompositeHealthContributor. 一个HealthIndicator提供实际的运行状况信息,包括Status. 一个CompositeHealthContributor提供其他HealthContributor实例。 总而言之,贡献者形成一个树结构来表示整个系统的运行状况。spring-doc.cadn.net.cn

默认情况下,最终的系统运行状况由StatusAggregator,它将对每个HealthIndicator基于状态的有序列表。 排序列表中的第一个状态用作整体运行状况。 如果没有HealthIndicator返回StatusAggregatorUNKNOWNstatus 为 used。spring-doc.cadn.net.cn

您可以使用HealthContributorRegistry在运行时注册和注销运行状况指示器。

自动配置的 HealthIndicators

在适当的时候, Spring Boot 会自动配置HealthIndicatorBean 的 Bean 中。 您还可以通过配置management.health.key.enabled, 使用key下表列出了:spring-doc.cadn.net.cn

钥匙 名字 描述

cassandraspring-doc.cadn.net.cn

CassandraDriverHealthIndicatorspring-doc.cadn.net.cn

检查 Cassandra 数据库是否已启动。spring-doc.cadn.net.cn

couchbasespring-doc.cadn.net.cn

CouchbaseHealthIndicatorspring-doc.cadn.net.cn

检查 Couchbase 集群是否已启动。spring-doc.cadn.net.cn

dbspring-doc.cadn.net.cn

DataSourceHealthIndicatorspring-doc.cadn.net.cn

检查连接到DataSource可以获得。spring-doc.cadn.net.cn

diskspacespring-doc.cadn.net.cn

DiskSpaceHealthIndicatorspring-doc.cadn.net.cn

检查磁盘空间是否不足。spring-doc.cadn.net.cn

elasticsearchspring-doc.cadn.net.cn

ElasticsearchRestClientHealthIndicatorspring-doc.cadn.net.cn

检查 Elasticsearch 集群是否已启动。spring-doc.cadn.net.cn

hazelcastspring-doc.cadn.net.cn

HazelcastHealthIndicatorspring-doc.cadn.net.cn

检查 Hazelcast 服务器是否已启动。spring-doc.cadn.net.cn

influxdbspring-doc.cadn.net.cn

InfluxDbHealthIndicatorspring-doc.cadn.net.cn

检查 InfluxDB 服务器是否已启动。spring-doc.cadn.net.cn

jmsspring-doc.cadn.net.cn

JmsHealthIndicatorspring-doc.cadn.net.cn

检查 JMS 代理是否已启动。spring-doc.cadn.net.cn

ldapspring-doc.cadn.net.cn

LdapHealthIndicatorspring-doc.cadn.net.cn

检查 LDAP 服务器是否已启动。spring-doc.cadn.net.cn

mailspring-doc.cadn.net.cn

MailHealthIndicatorspring-doc.cadn.net.cn

检查邮件服务器是否已启动。spring-doc.cadn.net.cn

mongospring-doc.cadn.net.cn

MongoHealthIndicatorspring-doc.cadn.net.cn

检查 Mongo 数据库是否已启动。spring-doc.cadn.net.cn

neo4jspring-doc.cadn.net.cn

Neo4jHealthIndicatorspring-doc.cadn.net.cn

检查 Neo4j 数据库是否已启动。spring-doc.cadn.net.cn

pingspring-doc.cadn.net.cn

PingHealthIndicatorspring-doc.cadn.net.cn

始终以UP.spring-doc.cadn.net.cn

rabbitspring-doc.cadn.net.cn

RabbitHealthIndicatorspring-doc.cadn.net.cn

检查 Rabbit 服务器是否已启动。spring-doc.cadn.net.cn

redisspring-doc.cadn.net.cn

RedisHealthIndicatorspring-doc.cadn.net.cn

检查 Redis 服务器是否已启动。spring-doc.cadn.net.cn

您可以通过设置management.health.defaults.enabled财产。

附加HealthIndicatorbean 可用,但默认情况下不启用:spring-doc.cadn.net.cn

钥匙 名字 描述

livenessstatespring-doc.cadn.net.cn

LivenessStateHealthIndicatorspring-doc.cadn.net.cn

公开 “Liveness” 应用程序可用性状态。spring-doc.cadn.net.cn

readinessstatespring-doc.cadn.net.cn

ReadinessStateHealthIndicatorspring-doc.cadn.net.cn

公开 “Readiness” 应用程序可用性状态。spring-doc.cadn.net.cn

编写自定义 HealthIndicators

要提供自定义运行状况信息,您可以注册实现HealthIndicator接口。 您需要提供health()方法并返回一个Health响应。 这Health响应应包括 状态 ,并且可以选择包含要显示的其他详细信息。 下面的代码显示了一个示例HealthIndicator实现:spring-doc.cadn.net.cn

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

	@Override
	public Health health() {
		int errorCode = check();
		if (errorCode != 0) {
			return Health.down().withDetail("Error Code", errorCode).build();
		}
		return Health.up().build();
	}

	private int check() {
		// perform some specific health check
		return ...
	}

}
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.HealthIndicator
import org.springframework.stereotype.Component

@Component
class MyHealthIndicator : HealthIndicator {

	override fun health(): Health {
		val errorCode = check()
		if (errorCode != 0) {
			return Health.down().withDetail("Error Code", errorCode).build()
		}
		return Health.up().build()
	}

	private fun check(): Int {
		// perform some specific health check
		return  ...
	}

}
给定HealthIndicator是 bean 的名称,没有HealthIndicatorsuffix (如果存在)。 在前面的示例中,运行状况信息在my.
运行状况指示器通常通过 HTTP 调用,并且需要在任何连接超时之前做出响应。 Spring Boot 将记录一条警告消息,用于响应时间超过 10 秒的任何运行状况指示器。 如果要配置此阈值,可以使用management.endpoint.health.logging.slow-indicator-threshold财产。

除了 Spring Boot 的预定义Status类型Health可以返回自定义Status这表示新的系统状态。 在这种情况下,您还需要提供StatusAggregator接口,或者您必须使用management.endpoint.health.status.orderconfiguration 属性。spring-doc.cadn.net.cn

例如,假设一个新的Status代码为FATAL正在您的某个HealthIndicator实现。 要配置严重性顺序,请将以下属性添加到您的应用程序属性中:spring-doc.cadn.net.cn

management.endpoint.health.status.order=fatal,down,out-of-service,unknown,up
management:
  endpoint:
    health:
      status:
        order: "fatal,down,out-of-service,unknown,up"

响应中的 HTTP 状态代码反映了整体运行状况。 默认情况下,OUT_OF_SERVICEDOWN映射到 503。 任何未映射的运行状况,包括UP,映射到 200。 如果您通过 HTTP 访问运行状况终端节点,您可能还需要注册自定义状态映射。 配置自定义映射将禁用DOWNOUT_OF_SERVICE. 如果要保留默认映射,则必须显式配置它们以及任何自定义映射。 例如,以下属性映射FATAL设置为 503(服务不可用),并保留DOWNOUT_OF_SERVICE:spring-doc.cadn.net.cn

management.endpoint.health.status.http-mapping.down=503
management.endpoint.health.status.http-mapping.fatal=503
management.endpoint.health.status.http-mapping.out-of-service=503
management:
  endpoint:
    health:
      status:
        http-mapping:
          down: 503
          fatal: 503
          out-of-service: 503
如果您需要更多控制,您可以定义自己的HttpCodeStatusMapper豆。

下表显示了内置状态的默认状态映射:spring-doc.cadn.net.cn

地位 映射

DOWNspring-doc.cadn.net.cn

SERVICE_UNAVAILABLE (503)spring-doc.cadn.net.cn

OUT_OF_SERVICEspring-doc.cadn.net.cn

SERVICE_UNAVAILABLE (503)spring-doc.cadn.net.cn

UPspring-doc.cadn.net.cn

默认情况下没有映射,因此 HTTP 状态为200spring-doc.cadn.net.cn

UNKNOWNspring-doc.cadn.net.cn

默认情况下没有映射,因此 HTTP 状态为200spring-doc.cadn.net.cn

反应性健康指标

对于响应式应用程序,例如那些使用 Spring WebFlux 的应用程序,ReactiveHealthContributor提供用于获取应用程序运行状况的非阻塞协定。 与传统的HealthContributor,则运行状况信息是从ReactiveHealthContributorRegistry(默认情况下,所有HealthContributorReactiveHealthContributor实例ApplicationContext). 定期HealthContributor不检查反应式 API 的实例将在 Elastic Scheduler 上执行。spring-doc.cadn.net.cn

在响应式应用程序中,您应该使用ReactiveHealthContributorRegistry在运行时注册和注销运行状况指示器。 如果您需要注册常规HealthContributor,您应该用ReactiveHealthContributor#adapt.

要从反应式 API 提供自定义运行状况信息,您可以注册实现ReactiveHealthIndicator接口。 下面的代码显示了一个示例ReactiveHealthIndicator实现:spring-doc.cadn.net.cn

import reactor.core.publisher.Mono;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyReactiveHealthIndicator implements ReactiveHealthIndicator {

	@Override
	public Mono<Health> health() {
		return doHealthCheck().onErrorResume((exception) ->
			Mono.just(new Health.Builder().down(exception).build()));
	}

	private Mono<Health> doHealthCheck() {
		// perform some specific health check
		return ...
	}

}
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.ReactiveHealthIndicator
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono

@Component
class MyReactiveHealthIndicator : ReactiveHealthIndicator {

	override fun health(): Mono<Health> {
		return doHealthCheck()!!.onErrorResume { exception: Throwable? ->
			Mono.just(Health.Builder().down(exception).build())
		}
	}

	private fun doHealthCheck(): Mono<Health>? {
		// perform some specific health check
		return  ...
	}

}
要自动处理错误,请考虑从AbstractReactiveHealthIndicator.

自动配置的 ReactiveHealthIndicators

在适当的时候, Spring Boot 会自动配置以下内容ReactiveHealthIndicator豆:spring-doc.cadn.net.cn

钥匙 名字 描述

cassandraspring-doc.cadn.net.cn

CassandraDriverReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Cassandra 数据库是否已启动。spring-doc.cadn.net.cn

couchbasespring-doc.cadn.net.cn

CouchbaseReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Couchbase 集群是否已启动。spring-doc.cadn.net.cn

elasticsearchspring-doc.cadn.net.cn

ElasticsearchReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Elasticsearch 集群是否已启动。spring-doc.cadn.net.cn

mongospring-doc.cadn.net.cn

MongoReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Mongo 数据库是否已启动。spring-doc.cadn.net.cn

neo4jspring-doc.cadn.net.cn

Neo4jReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Neo4j 数据库是否已启动。spring-doc.cadn.net.cn

redisspring-doc.cadn.net.cn

RedisReactiveHealthIndicatorspring-doc.cadn.net.cn

检查 Redis 服务器是否已启动。spring-doc.cadn.net.cn

如有必要,反应式指标将替换常规指标。 此外,任何HealthIndicator未明确处理的 API 将自动包装。

健康组

有时,将运行状况指示器组织到可用于不同目的的组中非常有用。spring-doc.cadn.net.cn

要创建运行状况指示器组,您可以使用management.endpoint.health.group.<name>属性并指定运行状况指示器 ID 列表以includeexclude. 例如,要创建仅包含数据库指示器的组,您可以定义以下内容:spring-doc.cadn.net.cn

management.endpoint.health.group.custom.include=db
management:
  endpoint:
    health:
      group:
        custom:
          include: "db"

同样,要创建一个从组中排除数据库指标并包括所有其他指标的组,您可以定义以下内容:spring-doc.cadn.net.cn

management.endpoint.health.group.custom.exclude=db
management:
  endpoint:
    health:
      group:
        custom:
          exclude: "db"

默认情况下,如果运行状况组包含或排除不存在的运行状况指示器,则启动将失败。 要禁用此行为,请将management.endpoint.health.validate-group-membershipfalse.spring-doc.cadn.net.cn

默认情况下,组继承相同的StatusAggregatorHttpCodeStatusMapper设置作为系统运行状况。 但是,您也可以按组定义这些 ID。 您还可以覆盖show-detailsrolesproperties (如果需要):spring-doc.cadn.net.cn

management.endpoint.health.group.custom.show-details=when-authorized
management.endpoint.health.group.custom.roles=admin
management.endpoint.health.group.custom.status.order=fatal,up
management.endpoint.health.group.custom.status.http-mapping.fatal=500
management.endpoint.health.group.custom.status.http-mapping.out-of-service=500
management:
  endpoint:
    health:
      group:
        custom:
          show-details: "when-authorized"
          roles: "admin"
          status:
            order: "fatal,up"
            http-mapping:
              fatal: 500
              out-of-service: 500
您可以使用@Qualifier("groupname")如果您需要注册自定义StatusAggregatorHttpCodeStatusMapperbean 用于组。

运行状况组还可以包含/排除CompositeHealthContributor. 您还可以仅包含/排除CompositeHealthContributor. 这可以使用组件的完全限定名称来完成,如下所示:spring-doc.cadn.net.cn

management.endpoint.health.group.custom.include="test/primary"
management.endpoint.health.group.custom.exclude="test/primary/b"

在上面的示例中,customgroup 将包含HealthContributor替换为名称primary它是复合体的一个组成部分test. 这里primary本身是一个复合体,而HealthContributor替换为名称b将从custom群。spring-doc.cadn.net.cn

运行状况组可以在主端口或管理端口上的其他路径上可用。 这在 Kubernetes 等云环境中很有用,出于安全目的,为 actuator 端点使用单独的 management 端口是很常见的。 拥有单独的端口可能会导致运行状况检查不可靠,因为即使运行状况检查成功,主应用程序也可能无法正常工作。 运行状况组可以配置其他路径,如下所示:spring-doc.cadn.net.cn

management.endpoint.health.group.live.additional-path="server:/healthz"

这将使live运行状况组在主服务器端口上可用/healthz. 前缀是必需的,并且必须是server:(表示主服务器端口)或management:(表示管理端口,如果已配置。 路径必须是单个路径段。spring-doc.cadn.net.cn

DataSource 运行状况

DataSource运行状况指示器 显示标准数据源和路由数据源 Bean 的运行状况。 路由数据源的运行状况包括其每个目标数据源的运行状况。 在运行状况终端节点的响应中,路由数据源的每个目标都使用其路由键命名。 如果您不想在指示器的输出中包含路由数据源,请将management.health.db.ignore-routing-data-sourcestrue.spring-doc.cadn.net.cn

Kubernetes 探针

部署在 Kubernetes 上的应用程序可以通过 Container Probe 提供有关其内部状态的信息。 根据您的 Kubernetes 配置,kubelet 会调用这些探测并对结果做出反应。spring-doc.cadn.net.cn

默认情况下, Spring Boot 管理您的应用程序可用性状态。 如果部署在 Kubernetes 环境中,执行器会从ApplicationAvailability接口,并在专用的运行状况指示器中使用该信息:LivenessStateHealthIndicatorReadinessStateHealthIndicator. 这些指标显示在全局运行状况终端节点 ("/actuator/health"). 它们还通过使用运行状况组作为单独的 HTTP 探测公开:"/actuator/health/liveness""/actuator/health/readiness".spring-doc.cadn.net.cn

然后,您可以使用以下终端节点信息配置 Kubernetes 基础设施:spring-doc.cadn.net.cn

livenessProbe:
  httpGet:
    path: "/actuator/health/liveness"
    port: <actuator-port>
  failureThreshold: ...
  periodSeconds: ...

readinessProbe:
  httpGet:
    path: "/actuator/health/readiness"
    port: <actuator-port>
  failureThreshold: ...
  periodSeconds: ...
<actuator-port>应设置为 actuator endpoints 可用的端口。 它可以是主 Web 服务器端口,也可以是单独的管理端口(如果"management.server.port"属性。

仅当应用程序在 Kubernetes 环境中运行时,才会自动启用这些运行状况组。 您可以在任何环境中使用management.endpoint.health.probes.enabledconfiguration 属性。spring-doc.cadn.net.cn

如果应用程序的启动时间超过配置的活跃期,Kubernetes 会提到"startupProbe"作为可能的解决方案。 一般来说,"startupProbe"在这里不一定需要,因为"readinessProbe"失败,直到所有启动任务都完成。 这意味着您的应用程序在准备就绪之前不会接收流量。 但是,如果您的应用程序需要很长时间才能启动,请考虑使用"startupProbe"以确保 Kubernetes 不会在应用程序启动过程中终止它。 请参阅描述 Probe 在应用程序生命周期中的行为方式的部分。

如果您的 Actuator 端点部署在单独的 Management 上下文中,则端点不会使用与主应用程序相同的 Web 基础架构(端口、连接池、框架组件)。 在这种情况下,即使主应用程序无法正常工作(例如,它无法接受新连接),探测检查也可能成功。 因此,最好将livenessreadiness主服务器端口上可用的运行状况组。 这可以通过设置以下属性来完成:spring-doc.cadn.net.cn

management.endpoint.health.probes.add-additional-paths=true

这将使liveness组 available at/livezreadiness组 available at/readyz在主服务器端口上。 可以使用additional-path属性,有关详细信息,请参阅运行状况组spring-doc.cadn.net.cn

使用 Kubernetes 探针检查外部状态

Actuator 将 “liveness” 和 “readiness” 探针配置为 Health Group。 这意味着所有运行状况组功能都可供他们使用。 例如,您可以配置其他运行状况指示器:spring-doc.cadn.net.cn

management.endpoint.health.group.readiness.include=readinessState,customCheck
management:
  endpoint:
    health:
      group:
        readiness:
          include: "readinessState,customCheck"

默认情况下, Spring Boot 不会向这些组添加其他运行状况指示器。spring-doc.cadn.net.cn

“活动性” 探测不应依赖于外部系统的运行状况检查。 如果应用程序的活跃状态被破坏,Kubernetes 会尝试通过重启应用程序实例来解决这个问题。 这意味着,如果外部系统(例如数据库、Web API 或外部缓存)发生故障,Kubernetes 可能会重新启动所有应用程序实例并产生级联故障。spring-doc.cadn.net.cn

至于 “readiness” 探测,应用程序开发人员必须仔细选择检查外部系统。 因此, Spring Boot 在就绪情况探测中不包括任何其他运行状况检查。 如果应用程序实例的就绪状态为 unready,Kubernetes 不会将流量路由到该实例。 某些外部系统可能不由应用程序实例共享,在这种情况下,它们可能包含在就绪情况探测中。 其他外部系统对于应用程序可能不是必需的(应用程序可能具有断路器和回退),在这种情况下,它们绝对不应包含在内。 不幸的是,所有应用程序实例共享的外部系统是通用的,您必须做出判断:将其包含在就绪情况探测中,并期望当外部服务关闭时应用程序会停止服务,或者将其排除在外,并处理堆栈上层的故障,也许是在调用者中使用断路器。spring-doc.cadn.net.cn

如果应用程序的所有实例都未就绪,则具有type=ClusterIPNodePort不接受任何传入连接。 没有 HTTP 错误响应(503 等),因为没有连接。 具有type=LoadBalancer可能接受也可能不接受连接,具体取决于提供商。 具有显式 Ingress 的服务也以取决于实现的方式进行响应 — Ingress 服务本身必须决定如何处理来自下游的 “connection refused”。 HTTP 503 在负载均衡器和入口的情况下都很可能出现。

此外,如果应用程序使用 Kubernetes 自动扩展,则它可能会对从负载均衡器中取出的应用程序做出不同的反应,具体取决于其自动扩展器配置。spring-doc.cadn.net.cn

应用程序生命周期和探测状态

Kubernetes 探针支持的一个重要方面是它与应用程序生命周期的一致性。 这两者之间存在显著差异AvailabilityState(这是应用程序的内存中内部状态) 和实际的探针(公开该状态)。 根据应用程序生命周期的阶段,探测可能不可用。spring-doc.cadn.net.cn

Spring Boot 在启动和关闭期间发布应用程序事件, 探测器可以监听此类事件并公开AvailabilityState信息。spring-doc.cadn.net.cn

下表显示了AvailabilityState以及 HTTP 连接器在不同阶段的状态。spring-doc.cadn.net.cn

当 Spring Boot 应用程序启动时:spring-doc.cadn.net.cn

启动阶段 LivenessState 就绪状态 HTTP 服务器 笔记

开始spring-doc.cadn.net.cn

BROKENspring-doc.cadn.net.cn

REFUSING_TRAFFICspring-doc.cadn.net.cn

未启动spring-doc.cadn.net.cn

Kubernetes 会检查 “liveness” Probe,如果时间过长,则会重启应用程序。spring-doc.cadn.net.cn

开始spring-doc.cadn.net.cn

CORRECTspring-doc.cadn.net.cn

REFUSING_TRAFFICspring-doc.cadn.net.cn

拒绝请求spring-doc.cadn.net.cn

应用程序上下文将刷新。应用程序执行启动任务,但尚未接收流量。spring-doc.cadn.net.cn

准备spring-doc.cadn.net.cn

CORRECTspring-doc.cadn.net.cn

ACCEPTING_TRAFFICspring-doc.cadn.net.cn

接受请求spring-doc.cadn.net.cn

启动任务已完成。应用程序正在接收流量。spring-doc.cadn.net.cn

当 Spring Boot 应用程序关闭时:spring-doc.cadn.net.cn

关闭阶段 活动状态 就绪状态 HTTP 服务器 笔记

运行spring-doc.cadn.net.cn

CORRECTspring-doc.cadn.net.cn

ACCEPTING_TRAFFICspring-doc.cadn.net.cn

接受请求spring-doc.cadn.net.cn

已请求关闭。spring-doc.cadn.net.cn

正常关闭spring-doc.cadn.net.cn

CORRECTspring-doc.cadn.net.cn

REFUSING_TRAFFICspring-doc.cadn.net.cn

新请求被拒绝spring-doc.cadn.net.cn

如果启用,正常关闭将处理正在进行的请求spring-doc.cadn.net.cn

关机完成spring-doc.cadn.net.cn

不适用spring-doc.cadn.net.cn

不适用spring-doc.cadn.net.cn

服务器已关闭spring-doc.cadn.net.cn

应用程序上下文已关闭,应用程序已关闭。spring-doc.cadn.net.cn

有关 Kubernetes 部署的更多信息,请参阅 Kubernetes 容器生命周期

应用信息

应用程序信息公开了从所有InfoContributorbean 中定义的ApplicationContext. Spring Boot 包括许多自动配置的InfoContributorbeans 的 bean 中,你可以编写自己的 bean 来编写。spring-doc.cadn.net.cn

自动配置的 InfoContributors

在适当的时候, Spring 会自动配置以下内容InfoContributor豆:spring-doc.cadn.net.cn

身份证 名字 描述 先决条件

buildspring-doc.cadn.net.cn

BuildInfoContributorspring-doc.cadn.net.cn

公开生成信息。spring-doc.cadn.net.cn

一个META-INF/build-info.properties资源。spring-doc.cadn.net.cn

envspring-doc.cadn.net.cn

EnvironmentInfoContributorspring-doc.cadn.net.cn

公开Environment其名称以info..spring-doc.cadn.net.cn

没有。spring-doc.cadn.net.cn

gitspring-doc.cadn.net.cn

GitInfoContributorspring-doc.cadn.net.cn

公开 git 信息。spring-doc.cadn.net.cn

一个git.properties资源。spring-doc.cadn.net.cn

javaspring-doc.cadn.net.cn

JavaInfoContributorspring-doc.cadn.net.cn

公开 Java 运行时信息。spring-doc.cadn.net.cn

没有。spring-doc.cadn.net.cn

osspring-doc.cadn.net.cn

OsInfoContributorspring-doc.cadn.net.cn

公开 Operating System 信息。spring-doc.cadn.net.cn

没有。spring-doc.cadn.net.cn

processspring-doc.cadn.net.cn

ProcessInfoContributorspring-doc.cadn.net.cn

公开进程信息。spring-doc.cadn.net.cn

没有。spring-doc.cadn.net.cn

是否启用单个贡献者由其management.info.<id>.enabled财产。 不同的参与者对此属性具有不同的默认值,具体取决于其先决条件和他们公开的信息的性质。spring-doc.cadn.net.cn

由于没有先决条件来指示应启用它们,env,java,osprocess默认情况下,contributors 处于禁用状态。 每个都可以通过设置其management.info.<id>.enabledproperty 设置为true.spring-doc.cadn.net.cn

buildgit默认情况下,信息贡献者处于启用状态。 每个都可以通过设置其management.info.<id>.enabledproperty 设置为false. 或者,要禁用通常默认启用的每个贡献者,请将management.info.defaults.enabledproperty 设置为false.spring-doc.cadn.net.cn

自定义应用程序信息

envcontributor 时,您可以自定义info端点info.*Spring 属性。 都Environment属性infokey 会自动公开。 例如,您可以将以下设置添加到application.properties文件:spring-doc.cadn.net.cn

info.app.encoding=UTF-8
info.app.java.source=17
info.app.java.target=17
info:
  app:
    encoding: "UTF-8"
    java:
      source: "17"
      target: "17"

除了对这些值进行硬编码外,您还可以在构建时扩展 info 属性spring-doc.cadn.net.cn

假设您使用 Maven,则可以按如下方式重写前面的示例:spring-doc.cadn.net.cn

info:
  app:
    encoding: "@project.build.sourceEncoding@"
    java:
      source: "@java.version@"
      target: "@java.version@"

Git 提交信息

另一个有用的功能是infoendpoint 是它发布有关git源代码存储库。 如果GitPropertiesbean 可用,您可以使用infoendpoint 来公开这些属性。spring-doc.cadn.net.cn

一个GitProperties如果git.propertiesfile 位于 Classpath 的根目录中。 有关更多详细信息,请参阅生成 Git 信息

默认情况下,端点会公开git.branch,git.commit.idgit.commit.time属性(如果存在)。 如果您不希望在终端节点响应中使用这些属性中的任何一个,则需要将它们从git.properties文件。 如果要显示完整的 git 信息(即git.properties),请使用management.info.git.mode属性,如下所示:spring-doc.cadn.net.cn

management.info.git.mode=full
management:
  info:
    git:
      mode: "full"

要从info端点,将management.info.git.enabledproperty 设置为false如下:spring-doc.cadn.net.cn

management.info.git.enabled=false
management:
  info:
    git:
      enabled: false

构建信息

如果BuildPropertiesbean 可用,则infoendpoint 还可以发布有关您的构建的信息。 如果META-INF/build-info.propertiesfile 在 Classpath 中可用。spring-doc.cadn.net.cn

Maven 和 Gradle 插件都可以生成该文件。 有关更多详细信息,请参阅生成生成信息

Java 信息

infoendpoint 发布有关 Java 运行时环境的信息,请参阅JavaInfo了解更多详情。spring-doc.cadn.net.cn

OS 信息

infoendpoint 发布有关您的作系统的信息,请参阅OsInfo了解更多详情。spring-doc.cadn.net.cn

过程信息

infoendpoint 发布有关您的进程的信息,请参阅ProcessInfo了解更多详情。spring-doc.cadn.net.cn

编写自定义 InfoContributor

要提供自定义应用程序信息,您可以注册实现InfoContributor接口。spring-doc.cadn.net.cn

以下示例提供了一个exampleentry 中具有单个值:spring-doc.cadn.net.cn

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class MyInfoContributor implements InfoContributor {

	@Override
	public void contribute(Info.Builder builder) {
		builder.withDetail("example", Collections.singletonMap("key", "value"));
	}

}
import org.springframework.boot.actuate.info.Info
import org.springframework.boot.actuate.info.InfoContributor
import org.springframework.stereotype.Component
import java.util.Collections

@Component
class MyInfoContributor : InfoContributor {

	override fun contribute(builder: Info.Builder) {
		builder.withDetail("example", Collections.singletonMap("key", "value"))
	}

}

如果您到达infoendpoint 的 Endpoint,您应该会看到一个包含以下附加条目的响应:spring-doc.cadn.net.cn

{
	"example": {
		"key" : "value"
	}
}

软件物料清单 (SBOM)

sbomendpoint 公开软件物料清单。 CycloneDX SBOM 可以自动检测,但其他格式也可以手动配置。spring-doc.cadn.net.cn

sbom然后,actuator 端点将公开一个名为 “application” 的 SBOM,它描述了应用程序的内容。spring-doc.cadn.net.cn

要在项目构建时自动生成CycloneDX SBOM,请参见Generate a CycloneDX SBOM部分。

其他 SBOM 格式

如果要以其他格式发布 SBOM,可以使用一些配置属性。spring-doc.cadn.net.cn

配置属性management.endpoint.sbom.application.location设置应用程序 SBOM 的位置。 例如,将此设置为classpath:sbom.json将使用/sbom.json资源。spring-doc.cadn.net.cn

可自动检测 CycloneDX、SPDX 和 Syft 格式的 SBOM 的介质类型。 要覆盖自动检测到的媒体类型,请使用 configuration 属性management.endpoint.sbom.application.media-type.spring-doc.cadn.net.cn

其他 SBOM

执行器终端节点可以处理多个 SBOM。 要添加 SBOM,请使用 configuration 属性management.endpoint.sbom.additional,如以下示例所示:spring-doc.cadn.net.cn

management.endpoint.sbom.additional.system.location=optional:file:/system.spdx.json
management.endpoint.sbom.additional.system.media-type=application/spdx+json
management:
  endpoint:
    sbom:
      additional:
        system:
          location: "optional:file:/system.spdx.json"
          media-type: "application/spdx+json"

这将添加一个名为 “system” 的 SBOM,它存储在/system.spdx.json. 这optional:prefix 可用于防止在文件不存在时启动失败。spring-doc.cadn.net.cn