java:spring actuator添加自定义endpoint

2024-06-15 20:04

本文主要是介绍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】
在这里插入图片描述
在这里插入图片描述

这篇关于java:spring actuator添加自定义endpoint的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064440

相关文章

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变