当前位置: 首页 > news >正文

java:spring actuator添加自定义endpoint

# 项目代码资源:

可能还在审核中,请等待。。。
https://download.csdn.net/download/chenhz2284/89437274

# 项目代码

【pom.xml】

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.12.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.3.12.RELEASE</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency>
</dependencies>
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version><configuration><compilerArgs><!--添加编译参数【-parameters】很重要,没有这个参数的话【/actuator/chzEndpoint/p1】这个地址无法访问--><arg>-parameters</arg></compilerArgs></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.12.RELEASE</version><executions><execution><phase>package</phase><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>

【application.properties】

server.port=8080
spring.application.name=myActuatormanagement.server.port=8080
management.endpoints.web.exposure.include=*

【ChzEndpoint.java】

package com.chz.myActuator.actuator;@Slf4j
@Component
@Endpoint(id = "chzEndpoint")
public class ChzEndpoint {private Map<String, Feature> features = new ConcurrentHashMap<>();@PostConstructpublic void init(){log.info("chz >> ChzEndpoint.<init>()");Feature p1Feature = new Feature();p1Feature.setEnabled(true);p1Feature.setName("p1");p1Feature.setValue(1D);features.put("p1", p1Feature);Feature p2Feature = new Feature();p2Feature.setEnabled(true);p2Feature.setName("p2");p2Feature.setValue(2D);features.put("p2", p2Feature);}// 请求地址为【GET /actuator/chzEndpoint】@ReadOperationpublic Map<String, Feature> features() {log.info("chz >> ChzEndpoint.features()");return features;}// 请求地址为【GET /actuator/chzEndpoint/p1】@ReadOperationpublic Feature feature(@Selector String name) {log.info("chz >> ChzEndpoint.feature(@Selector String name)");return features.get(name);}// 请求地址为【POST /actuator/chzEndpoint/p1】@WriteOperationpublic void putFeature(@Selector String name, Double value) {log.info("chz >> ChzEndpoint.putFeature(@Selector String name, Feature feature)");Feature feature = features.get(name);if( feature!=null ){feature.setValue(value);} else {feature = new Feature();feature.setEnabled(true);feature.setName(name);feature.setValue(value);feature.setTime(LocalDateTime.now());features.put(name, feature);}}// 请求地址为【DELETE /actuator/chzEndpoint/p1】@DeleteOperationpublic void deleteFeature(@Selector String name) {log.info("chz >> ChzEndpoint.deleteFeature(@Selector String name)");features.remove(name);}@Getter@Setterpublic class Feature{private Boolean enabled;private String name;private Double value;private LocalDateTime time = LocalDateTime.now();}
}

【TestController.java】

package com.chz.myActuator.controller;@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {@Autowiredprivate ConfigurableApplicationContext context;@GetMapping("/shutdown")public String shutdown() {context.close();return "shutdown";}
}

【MyActuatorTest.java】

package com.chz.myActuator;@SpringBootApplication
public class MyActuatorTest {public static void main(String[] args) {SpringApplication.run(MyActuatorTest.class, args);}
}

【test.http】

###GET http://localhost:8080/actuator/chzEndpoint###GET http://localhost:8080/actuator/chzEndpoint/p1###POST http://localhost:8080/actuator/chzEndpoint/p1
Content-Type: application/json{"value": 111.0
}###DELETE http://localhost:8080/actuator/chzEndpoint/p1
Content-Type: application/json

# 运行与测试

启动【MyActuatorTest】

访问【http://localhost:8080/actuator/chzEndpoint】
在这里插入图片描述
访问【http://localhost:8080/actuator/chzEndpoint/p1】
在这里插入图片描述
访问【POST http://localhost:8080/actuator/chzEndpoint/p1】
在这里插入图片描述
在这里插入图片描述
访问【DELETE http://localhost:8080/actuator/chzEndpoint/p1】
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/376674.html

相关文章:

  • LeetCode88-删除有序数组中的重复项
  • SpringBoot Starter 通用接口加密组件(防篡改)+ RequestBodyAdvice和ResponseBodyAdvice原理
  • delphi 如何使用TEdgeBrowser组件以及打包环境在其他主机上运行
  • Sui的Fastcrypto加密库刷新速度记录
  • Malformed \uxxxx encoding或Maven server structure problem问题解决
  • Sui主网升级至V1.27.2版本
  • Cheat Engine 学习
  • 【千帆AppBuilder】你有一封邮件待查收|未来的我,你好吗?欢迎体验AI应用《未来信使》
  • 【案例分析】一文讲清楚SaaS产品运营的六大杠杆是什么?具体怎么运用?
  • 系统架构——Spring Framework
  • Zig标准库:最全数据结构深度解析(1)
  • 什么是 Linux From Scratch (LFS)?
  • 常见的宽基指数基金
  • Python学习笔记6:pychram相关知识及安装教程,后续需要学习的入门知识
  • dockerfile文件的中的命令
  • 【紫光同创盘古PGX-Nano教程】——(盘古PGX-Nano开发板/PG2L50H_MBG324第十一章)模拟波形实验例程说明
  • LUA移植到STM32F4,移植REPL,通过RTT Viewer交互
  • 【GD32F303红枫派使用手册】第十九节
  • 【C语言】扫雷游戏
  • 逻辑蕴含、函数依赖集的闭包、Armstrong公理、属性集闭包
  • macOS聚集搜索功能开启与关闭
  • 大模型“诸神之战”,落地才是赛点
  • 接口重放攻击
  • MySQL学习笔记-进阶篇-SQL优化
  • 【机器学习】第2章 线性回归及最大熵模型
  • 科技创新对农业发展的影响
  • Linux 常用命令 - rm 【删除文件或目录】
  • 一血c++
  • 无问芯穹Qllm-Eval:制作多模型、多参数、多维度的量化方案
  • 2024-05-31T08:36:09.000+00:00 转换 YYYY-MM-DD HH-MM-SS