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】
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A圳技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值