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=myActuator

management.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<>();

    @PostConstruct
    public 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】
    @ReadOperation
    public Map<String, Feature> features() {
        log.info("chz >> ChzEndpoint.features()");
        return features;
    }

    // 请求地址为【GET /actuator/chzEndpoint/p1】
    @ReadOperation
    public Feature feature(@Selector String name) {
        log.info("chz >> ChzEndpoint.feature(@Selector String name)");
        return features.get(name);
    }

    // 请求地址为【POST /actuator/chzEndpoint/p1】
    @WriteOperation
    public 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】
    @DeleteOperation
    public void deleteFeature(@Selector String name) {
        log.info("chz >> ChzEndpoint.deleteFeature(@Selector String name)");
        features.remove(name);
    }

    @Getter
    @Setter
    public 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 {

    @Autowired
    private 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.mfbz.cn/a/712733.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Cocos2d-x 4.0 工程首次建立与编译(Mac m1)

Mac m1芯片下将cocos2d-x升级至4.0版本后&#xff0c;官方剔除了不同平台的工程以及变更了编译方式&#xff0c;直接使用cmake构建&#xff0c;需要做一些前置的准备工作。 环境准备&#xff1a; 项 版本 备注 MacOS10.3 or laterpython2.7.16(建议>2.7.10)cmake3.29.3Do…

Android 工程副总裁卸任

Android 工程副总裁卸任 Android工程副总裁Dave Burke宣布&#xff0c;他将辞去领导Android工程的职位&#xff0c;将重心转向“AI/生物”项目。不过&#xff0c;他并没有离开Alphabet&#xff0c;目前仍将担任Android系统开发顾问的角色。 Burke参与了Android系统的多个关键…

TCP三次握手的过程

一、什么是TCP TCP是面向连接的、可靠的、基于字节流的传输层通信协议。 二、TCP的头部格式 序列号:在建立连接时由计算机生成的随机数作为其初始值&#xff0c;通过SYN包传给接收端主机&#xff0c;每发送一次数据&#xff0c;就「累加」一次该「数据字节数」的大小。用来解…

155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题

设计一个支持 push &#xff0c;pop &#xff0c;top 操作&#xff0c;并能在常数时间内检索到最小元素的栈。 实现 MinStack 类: MinStack() 初始化堆栈对象。void push(int val) 将元素val推入堆栈。void pop() 删除堆栈顶部的元素。int top() 获取堆栈顶部的元素。int get…

PCB设计简介

PCB电路板各层的含义 A. Signal And Plane Layers(S) 1. Signal Layers(信号层): 信号层主要用于布置电路板上的导线。Altium Designer提供了32个信号层&#xff0c;包括Top layer(顶层)&#xff0c;Bottom layer(底层)和32个内电层。 包括&#xff1a;Top layer(顶层),Bott…

CleanMyMac占用内存大吗 CleanMyMac如何释放内存空间

Mac OS上内存可谓是“寸土寸金”&#xff0c;每一M的内存都是真金白银换来的。为了有更充足的系统空间&#xff0c;有用户会使用系统清理和优化工具CleanMyMac&#xff0c;那么下面我们来看看CleanMyMac占用内存大吗&#xff0c;CleanMyMac如何释放内存空间的相关内容吧。 一、…

spring boot配置ssl证书,支持https访问

1. 阿里云官网下载证书,云控制台搜索ssl&#xff0c;点击进入。 2.点击免费证书&#xff0c;立即购买。 3. 点击创建证书&#xff0c;填写完证书申请后&#xff0c;等待证书签发。 4. 证书签发以后&#xff0c;点击下载证书&#xff0c;spring boot选tomcat服务器类型的。 5. …

Linux应用编程 - i2c-dev操作I2C

嵌入式Linux操作I2C设备&#xff0c;我们一般会在内核态编写I2C驱动程序。另外还能在用户空间编写I2C程序&#xff0c;下面介绍相关代码的实现。 i2c-dev框架在内核中封装了I2C通信所需要的所有通信细节&#xff0c;I2C适配器会在/dev目录下创建字符设备&#xff0c;例如&#…

如何避免销售飞单私单!教你如何巧妙避开陷阱,业绩飙升!

明明投入了大量的时间和精力&#xff0c;客户却悄无声息地消失了&#xff1f;或是突然有一天&#xff0c;你发现原本属于你的订单被同事悄悄抢走&#xff1f;这背后&#xff0c;很可能隐藏着销售飞单私单的陷阱。今天&#xff0c;就让我们一起探讨如何巧妙避开这些陷阱&#xf…

TCP与UDP案例

udp不会做拆分整合什么的 多大就是多大

MyBatis使用Demo

文章目录 01、Mybatis 意义02、Mybatis 快速入门04、Mapper 代理开发05、Mybatis 配置文件07、查询所有&结果映射08、查询-查看详情09、查询-条件查询10、查询-动态条件查询多条件动态查询单条件动态查询 11、添加&修改功能添加功能修改功能 12、删除功能删除一个批量删…

【html】学会这一套布局,让你的网页更加

很多小伙伴们在刚刚开始学习网页设计的时候不知道怎么布局今天给大家介绍一种非常实用且更加专业的一种布局。 灵感来源&#xff1a; 小米官网 布局图; 实例效果图&#xff1a; 这是一个简单的HTML模板&#xff0c;包括头部、内容区域和底部。 头部部分包括一个分为左右两部分…

【记录】ChatGLM3-6B大模型部署、微调(二):微调

前言 上文记录了ChatGLM3-6B大模型本地化部署过程&#xff0c;本次对模型进行微调&#xff0c;目的是修改模型自我认知。采用官方推荐微调框架&#xff1a;LLaMA-Factory 安装LLaMA-Factory # 克隆项目 git clone https://github.com/hiyouga/LLaMA-Factory.git 安装依赖 # 安装…

Linux---系统的初步学习【项目一:Linux操作系统的安装与配置】

项目一 Linux操作系统的安装与配置 1.1 项目知识准备 1.1.1 操作系统是什么&#xff1f; ​ 操作系统&#xff08;Operating System&#xff0c;OS&#xff09;是管理计算机硬件与软件资源的计算机程序。操作系统需要处理如管理硬件、决定程序运行的优先次序、管理文件系统等…

逻辑斯蒂回归与最大熵

知识树 感知机的缺陷 修补感知机缺陷-逻辑斯蒂回归 下面这两个值是强制给的,不是推导的 最大熵 最大熵的一个小故事 最大熵模型 我们最终目标是要求P(Y|X) 书上写的是H,但是2我们认为H(Y|X)更合适 咱们最终的目的是要用拉格朗日乘数法,所以需要约束 总结 感觉深度之眼比较模…

汽车级TPSI2140QDWQRQ1隔离式固态继电器,TMUX6136PWR、TMUX1109PWR、TMUX1133PWR模拟开关与多路复用器(参数)

1、TPSI2140-Q1 是一款隔离式固态继电器&#xff0c;专为高电压汽车和工业应用而设计。 TPSI2140-Q1 与 TI 具有高可靠性的电容隔离技术和内部背对背 MOSFET 整合在一起&#xff0c;形成了一款完全集成式解决方案&#xff0c;无需次级侧电源。 该器件的初级侧仅由 9mA 的输入电…

Studio One 6.6.2 for Mac怎么激活,有Studio One 6激活码吗?

如果您是一名音乐制作人&#xff0c;您是否曾经为了寻找一个合适的音频工作站而苦恼过&#xff1f;Studio One 6 for Mac是一款非常适合您的MacBook的音频工作站。它可以帮助您轻松地录制、编辑、混音和发布您的音乐作品。 Studio One 6.6.2 for Mac具有直观的界面和强大的功能…

Zookeeper: 配置参数解读

Zookeeper中的配置文件zoo.cfg中参数含义解读如下&#xff1a; tickTime&#xff1a;通信心跳时间&#xff0c;Zookeeper服务器与客户端心跳时间&#xff0c;单位毫秒。 initLimit: LF初始通信时限 Leader和Follower初始连接时能容忍的最多心跳数。 syncLimit: LF同步通信时…

算法01 递推算法及相关问题详解【C++实现】

目录 递推的概念 训练&#xff1a;斐波那契数列 解析 参考代码 训练&#xff1a;上台阶 参考代码 训练&#xff1a;信封 解析 参考代码 递推的概念 递推是一种处理问题的重要方法。 递推通过对问题的分析&#xff0c;找到问题相邻项之间的关系&#xff08;递推式&a…

mongodb 集群安装

1. 配置域名 Server1&#xff1a; OS version: CentOS Linux release 8.5.2111 hostnamectl --static set-hostname mongo01 vi /etc/sysconfig/network # Created by anaconda hostnamemong01 echo "192.168.88.20 mong1 mongo01.com mongo02.com" >> /…