Spring Boot Actuator 是 Spring Boot 的一个子项目,它提供生产就绪的功能来帮助您监控和管理应用程序。它提供了一组内置端点,使您可以深入了解应用程序的运行状况、指标和环境,并动态控制它。
Spring Boot Actuator 提供了几个开箱即用的端点,可用于监视应用程序并与应用程序交互。可以通过 HTTP、JMX 或使用 Spring Boot Admin 访问这些端点。
要在 Spring Boot 应用程序中使用 Actuator,您需要将 Actuator 依赖项添加到 pom.xml 文件中:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
如果您使用 Gradle,请将以下内容添加到您的 build.gradle 文件中:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
默认情况下,仅启用少数端点。您可以在 application.yml 文件中启用其他端点:
management: endpoints: web: exposure: include: "*" # This exposes all available endpoints endpoint: health: show-details: always # Show detailed health information
Actuator 设置完成后,您就可以访问它提供的各种端点。以下是一些常用的端点:
/actuator/health 端点提供有关应用程序运行状况的信息:
GET http://localhost:8080/actuator/health
响应示例:
{ "status": "UP", "components": { "db": { "status": "UP", "details": { "database": "H2", "result": 1 } }, "diskSpace": { "status": "UP", "details": { "total": 499963174912, "free": 16989374464, "threshold": 10485760, "exists": true } } } }
/actuator/metrics 端点提供与您的应用程序相关的各种指标:
GET http://localhost:8080/actuator/metrics
响应示例:
{ "names": [ "jvm.memory.used", "jvm.gc.pause", "system.cpu.usage", "system.memory.usage", "http.server.requests" ] }
要获取特定指标的详细信息:
GET http://localhost:8080/actuator/metrics/jvm.memory.used
响应示例:
{ "name": "jvm.memory.used", "description": "The amount of used memory", "baseUnit": "bytes", "measurements": [ { "statistic": "VALUE", "value": 5.1234567E7 } ], "availableTags": [ { "tag": "area", "values": [ "heap", "nonheap" ] }, { "tag": "id", "values": [ "PS Eden Space", "PS Survivor Space", "PS Old Gen", "Metaspace", "Compressed Class Space" ] } ] }
/actuator/env 端点提供有关环境属性的信息:
GET http://localhost:8080/actuator/env
响应示例:
{ "activeProfiles": [], "propertySources": [ { "name": "systemProperties", "properties": { "java.runtime.name": { "value": "Java(TM) SE Runtime Environment" }, "java.vm.version": { "value": "25.181-b13" } } }, { "name": "systemEnvironment", "properties": { "PATH": { "value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" }, "HOME": { "value": "/root" } } } ] }
/actuator/info 端点提供有关应用程序的信息:
GET http://localhost:8080/actuator/info
要自定义信息,请在 application.yml 中添加属性:
info: app: name: My Spring Boot Application description: This is a sample Spring Boot application version: 1.0.0
默认情况下,所有 Actuator 端点无需身份验证即可访问。为了保护这些端点,您可以使用 Spring Security。将 Spring Security 依赖项添加到您的 pom.xml 中:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
更新您的 application.yml 以限制访问:
management: endpoints: web: exposure: include: "*" # Expose all endpoints endpoint: health: show-details: always # Show detailed health information spring: security: user: name: admin # Default username password: admin # Default password # Restrict actuator endpoints to authenticated users management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always security: enabled: true roles: ACTUATOR
创建安全配置类来配置HTTP安全:
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/actuator/**").hasRole("ACTUATOR") .anyRequest().authenticated() .and() .httpBasic(); } }
通过此配置,只有具有 ACTUATOR 角色的经过身份验证的用户才能访问 Actuator 端点。
您可以创建自定义执行器端点来公开特定于您的应用程序的附加信息或功能。以下是创建自定义端点的示例:
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; @Endpoint(id = "custom") @Component public class CustomEndpoint { @ReadOperation public String customEndpoint() { return "Custom Actuator Endpoint"; } }
访问您的自定义端点:
GET http://localhost:8080/actuator/custom
Spring Boot Actuator 提供了一组强大的工具来帮助您监控和管理应用程序。通过利用其内置端点和创建自定义端点的能力,您可以获得有关应用程序性能和运行状况的宝贵见解。使用 Spring Security 保护这些端点,以确保只有授权用户才能访问,并且您将拥有一个易于管理和监控的生产就绪应用程序。
Actuator 是任何 Spring Boot 应用程序的重要组成部分,使您能够掌握应用程序运行时环境的脉搏,并在出现问题时快速响应。立即开始使用 Spring Boot Actuator 来增强应用程序的可观察性和可操作性。
以上是Spring Boot Actuator 使用初学者指南的详细内容。更多信息请关注PHP中文网其他相关文章!