0

0

Spring Boot Actuator 使用初学者指南

WBOY

WBOY

发布时间:2024-08-06 17:50:03

|

828人浏览过

|

来源于dev.to

转载

spring boot actuator 使用初学者指南

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

什么是 spring boot 执行器?

spring boot actuator 提供了几个开箱即用的端点,可用于监视应用程序并与应用程序交互。可以通过 http、jmx 或使用 spring boot admin 访问这些端点。

spring boot 执行器的主要特性

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

设置 spring boot 执行器

1. 添加执行器依赖

要在 spring boot 应用程序中使用 actuator,您需要将 actuator 依赖项添加到 pom.xml 文件中:


    org.springframework.boot
    spring-boot-starter-actuator

如果您使用 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 端点提供有关环境属性的信息:

燕雀Logo
燕雀Logo

为用户提供LOGO免费设计在线生成服务

下载
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 中:


    org.springframework.boot
    spring-boot-starter-security

更新您的 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框架介绍
spring框架介绍

本专题整合了spring框架相关内容,想了解更多详细内容,请阅读专题下面的文章。

106

2025.08.06

spring boot框架优点
spring boot框架优点

spring boot框架的优点有简化配置、快速开发、内嵌服务器、微服务支持、自动化测试和生态系统支持。本专题为大家提供spring boot相关的文章、下载、课程内容,供大家免费下载体验。

135

2023.09.05

spring框架有哪些
spring框架有哪些

spring框架有Spring Core、Spring MVC、Spring Data、Spring Security、Spring AOP和Spring Boot。详细介绍:1、Spring Core,通过将对象的创建和依赖关系的管理交给容器来实现,从而降低了组件之间的耦合度;2、Spring MVC,提供基于模型-视图-控制器的架构,用于开发灵活和可扩展的Web应用程序等。

389

2023.10.12

Java Spring Boot开发
Java Spring Boot开发

本专题围绕 Java 主流开发框架 Spring Boot 展开,系统讲解依赖注入、配置管理、数据访问、RESTful API、微服务架构与安全认证等核心知识,并通过电商平台、博客系统与企业管理系统等项目实战,帮助学员掌握使用 Spring Boot 快速开发高效、稳定的企业级应用。

68

2025.08.19

Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性
Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性

Spring Boot 是一个基于 Spring 框架的 Java 开发框架,它通过 约定优于配置的原则,大幅简化了 Spring 应用的初始搭建、配置和开发过程,让开发者可以快速构建独立的、生产级别的 Spring 应用,无需繁琐的样板配置,通常集成嵌入式服务器(如 Tomcat),提供“开箱即用”的体验,是构建微服务和 Web 应用的流行工具。

34

2025.12.22

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

114

2025.12.24

pdf怎么转换成xml格式
pdf怎么转换成xml格式

将 pdf 转换为 xml 的方法:1. 使用在线转换器;2. 使用桌面软件(如 adobe acrobat、itext);3. 使用命令行工具(如 pdftoxml)。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1893

2024.04.01

xml怎么变成word
xml怎么变成word

步骤:1. 导入 xml 文件;2. 选择 xml 结构;3. 映射 xml 元素到 word 元素;4. 生成 word 文档。提示:确保 xml 文件结构良好,并预览 word 文档以验证转换是否成功。想了解更多xml的相关内容,可以阅读本专题下面的文章。

2087

2024.08.01

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

38

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Spring中文手册
Spring中文手册

共0课时 | 0人学习

马士兵spring视频教程
马士兵spring视频教程

共25课时 | 9.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号