SpringBoot actuator(自定义端点类)
SpringBoot actuator(自定义端点类)
1、编写自动端点类很简单,只需要在类前面使用@Endpoint注解
EndPoint中id 来表示url路径
2、使用@ReadOperation //显示监控指标
3、使用@WriteOperation //动态修改指标,以post方式修改
示例如下:
package com.caicai.springboot.study.EndPoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.logging.SimpleFormatter; /* * 自定义端点类 * @Endpoint //表示这是一个自定义事件端点类 * Endpoint 中有一个id //它是设置端点的URL路径 * */ @Endpoint(id="datetime") //端点路径不要与系统自带的重合 public class DateTimeEndpoint { //一般端点都是对象,或者一个json返回的格式,所以通常我们会将端点定义一个MAP的返回形式 // 通过ReadOperation //访问地址是根据前缀+ endpoint 的ID // /Springboot-caicai/actuator/datetime private String format = "yyyy-MM-dd HH:mm:ss"; @ReadOperation //显示监控指标 public Map<String,Object> info(){ Map<String,Object> info = new HashMap<>(); info.put("name","caicia"); info.put("address","幸福路1号"); info.put("datetime",new SimpleDateFormat(format).format(new Date())); return info; } //动态修改指标 @WriteOperation //动态修改指标,是以post方式修改 public void setFormat(String format){ this.format = format; } }
4、动态修改参数,需要配置自定义配置类
使用四个注解
@Configuration //表示该类是配置类
@Bean//表示配置类为Spring boot 一个应用bean
@ConditionalOnMissingBean //条件注解1:表示当前bean缺少的时候才会注入bean
@ConditionalOnEnabledEndpoint //条件注解2:表示当监控端点被开启的时候,才会将自定义的类注入到程序应用中
package com.caicai.springboot.study.EndPoint; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /* * 自定义端点配置类 * * */ @Configuration //表示该类是配置类 public class EndPointConfig { @Bean//表示配置类为Spring boot 一个应用bean @ConditionalOnMissingBean //条件注解1:表示当前bean缺少的时候才会注入bean @ConditionalOnEnabledEndpoint //条件注解2:表示当监控端点被开启的时候,才会将自定义的类注入到程序应用中 public DateTimeEndpoint dateTimeEndpoint(){ return new DateTimeEndpoint(); } }
测试结果:
访问地址:
http://127.0.0.1:8080/Springboot-caicai/actuator/datetime
没修该之前时间的值:
使用POST方式进行修改
返回是正常的
最后我们GET一下实际的结果值
开开心心,上班!
快快乐乐,游玩!
及时行乐!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律