Spring Boot Actuator 使用初学者指南
来源:dev.to
时间:2024-08-01 22:18:43 279浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Spring Boot Actuator 使用初学者指南》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
spring boot actuator 是 spring boot 的一个子项目,它提供生产就绪的功能来帮助您监控和管理应用程序。它提供了一组内置端点,使您可以深入了解应用程序的运行状况、指标和环境,并对其进行动态控制。
什么是 spring boot 执行器?
spring boot actuator 提供了几个开箱即用的端点,可用于监视应用程序并与应用程序交互。可以通过 http、jmx 或使用 spring boot admin 访问这些端点。
spring boot 执行器的主要特性
- 健康检查:监控应用程序及其依赖项的健康状况。
- metrics:收集各种指标,例如内存使用情况、垃圾回收、web 请求详细信息等
- 环境信息:访问应用程序的环境属性。
- 应用程序信息:检索有关应用程序构建的信息,例如版本和名称。
- 动态日志级别:无需重新启动应用程序即可更改日志级别。
- 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学习网公众号!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
460 收藏
-
272 收藏
-
230 收藏
-
266 收藏
-
487 收藏
-
289 收藏
-
115 收藏
-
440 收藏
-
231 收藏
-
213 收藏
-
348 收藏
-
381 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习