当前位置:首页 > 文章列表 > 文章 > java教程 > Spring Boot Actuator 使用初学者指南

Spring Boot Actuator 使用初学者指南

来源:dev.to 2024-08-01 22:18:43 0浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Spring Boot Actuator 使用初学者指南》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

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 文件中:

<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 使用初学者指南》的详细内容,更多关于的资料请关注golang学习网公众号!

版本声明
本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
Samsign菜单显示软件Samsign菜单显示软件
上一篇
Samsign菜单显示软件
PHP框架与移动开发:构建跨平台应用程序
下一篇
PHP框架与移动开发:构建跨平台应用程序
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 讯飞AI大学堂免费AI认证证书:大模型工程师认证,提升您的职场竞争力
    免费AI认证证书
    科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
    25次使用
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    160次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    199次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    178次使用
  • 稿定PPT:在线AI演示设计,高效PPT制作工具
    稿定PPT
    告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
    167次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码