当前位置:首页 > 文章列表 > 文章 > 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 文件中:


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

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 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推荐
  • 毕业宝AIGC检测:AI生成内容检测工具,助力学术诚信
    毕业宝AIGC检测
    毕业宝AIGC检测是“毕业宝”平台的AI生成内容检测工具,专为学术场景设计,帮助用户初步判断文本的原创性和AI参与度。通过与知网、维普数据库联动,提供全面检测结果,适用于学生、研究者、教育工作者及内容创作者。
    12次使用
  • AI Make Song:零门槛AI音乐创作平台,助你轻松制作个性化音乐
    AI Make Song
    AI Make Song是一款革命性的AI音乐生成平台,提供文本和歌词转音乐的双模式输入,支持多语言及商业友好版权体系。无论你是音乐爱好者、内容创作者还是广告从业者,都能在这里实现“用文字创造音乐”的梦想。平台已生成超百万首原创音乐,覆盖全球20个国家,用户满意度高达95%。
    26次使用
  • SongGenerator.io:零门槛AI音乐生成器,快速创作高质量音乐
    SongGenerator
    探索SongGenerator.io,零门槛、全免费的AI音乐生成器。无需注册,通过简单文本输入即可生成多风格音乐,适用于内容创作者、音乐爱好者和教育工作者。日均生成量超10万次,全球50国家用户信赖。
    23次使用
  •  BeArt AI换脸:免费在线工具,轻松实现照片、视频、GIF换脸
    BeArt AI换脸
    探索BeArt AI换脸工具,免费在线使用,无需下载软件,即可对照片、视频和GIF进行高质量换脸。体验快速、流畅、无水印的换脸效果,适用于娱乐创作、影视制作、广告营销等多种场景。
    26次使用
  • SEO标题协启动:AI驱动的智能对话与内容生成平台 - 提升创作效率
    协启动
    SEO摘要协启动(XieQiDong Chatbot)是由深圳协启动传媒有限公司运营的AI智能服务平台,提供多模型支持的对话服务、文档处理和图像生成工具,旨在提升用户内容创作与信息处理效率。平台支持订阅制付费,适合个人及企业用户,满足日常聊天、文案生成、学习辅助等需求。
    27次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码