spring boot actuator 是 spring boot 的一个子项目,它提供生产就绪的功能来帮助您监控和管理应用程序。它提供了一组内置端点,使您可以深入了解应用程序的运行状况、指标和环境,并对其进行动态控制。
什么是 spring boot 执行器?
spring boot actuator 提供了几个开箱即用的端点,可用于监视应用程序并与应用程序交互。可以通过 http、jmx 或使用 spring boot admin 访问这些端点。
spring boot 执行器的主要特性
健康检查:监控应用程序及其依赖项的健康状况。
metrics:收集各种指标,例如内存使用情况、垃圾回收、web 请求详细信息等
环境信息:访问应用程序的环境属性。
应用程序信息:检索有关应用程序构建的信息,例如版本和名称。
动态日志级别:无需重新启动应用程序即可更改日志级别。
http tracing:跟踪 http 请求。
设置 spring boot 执行器
- 添加执行器依赖
要在 spring boot 应用程序中使用 actuator,您需要将 actuator 依赖项添加到 pom.xml 文件中:
org.springframework.bootspring-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.bootspring-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 使用初学者指南的详细内容,更多请关注php中文网其它相关文章!
Tommypoike1 个月前
发表在:关于我们hi
AmandaIncaboraa3 个月前
发表在:关于我们"我很想找出激励你的东西。 和我聊天 h...
AmandaIncabora23 个月前
发表在:关于我们我在等你的留言! 过来打个招呼! ...
AmandaIncaborac3 个月前
发表在:关于我们让我们今晚难忘...你的地方还是我的? ...
BryanDen4 个月前
发表在:关于我们Самый быстрый и безо...
91资源网站长-冰晨9 个月前
发表在:【账号直充】爱奇艺黄金VIP会员『1个月』官方直充丨立即到账丨24小时全天秒单!不错不错,价格比官方便宜
91资源网站长-冰晨9 个月前
发表在:2022零基础Java入门视频课程不错,学习一下