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

相关文章

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp