首页 > Java > java教程 > 正文

Spring Boot Actuator 使用初学者指南

WBOY
发布: 2024-08-02 10:00:24
原创
642 人浏览过

A Beginners Guide to Using Spring Boot Actuator

Spring Boot Actuator 是 Spring Boot 的一个子项目,它提供生产就绪的功能来帮助您监控和管理应用程序。它提供了一组内置端点,使您可以深入了解应用程序的运行状况、指标和环境,并动态控制它。

什么是 Spring Boot 执行器?

Spring Boot Actuator 提供了几个开箱即用的端点,可用于监视应用程序并与应用程序交互。可以通过 HTTP、JMX 或使用 Spring Boot Admin 访问这些端点。

Spring Boot 执行器的主要特性

  1. 健康检查:监控应用程序及其依赖项的健康状况。
  2. 指标:收集各种指标,例如内存使用情况、垃圾回收、Web 请求详细信息等
  3. 环境信息:访问应用程序的环境属性。
  4. 应用程序信息:检索有关应用程序构建的信息,例如版本和名称。
  5. 动态日志级别:无需重新启动应用程序即可更改日志级别。
  6. HTTP 跟踪:跟踪 HTTP 请求。

设置 Spring Boot 执行器

1. 添加执行器依赖

要在 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'
登录后复制

2. 启用执行器端点

默认情况下,仅启用少数端点。您可以在 application.yml 文件中启用其他端点:

management:
  endpoints:
    web:
      exposure:
        include: "*"  # This exposes all available endpoints
  endpoint:
    health:
      show-details: always  # Show detailed health information
登录后复制

使用执行器端点

Actuator 设置完成后,您就可以访问它提供的各种端点。以下是一些常用的端点:

1. 健康端点

/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
      }
    }
  }
}
登录后复制

2. 指标端点

/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"
      ]
    }
  ]
}
登录后复制

3. 环境端点

/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"
        }
      }
    }
  ]
}
登录后复制

4. 信息端点

/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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!