java:spring actuator添加自定义endpoint

作者 : admin 本文共3720个字,预计阅读时间需要10分钟 发布时间: 2024-06-15 共1人阅读

# 项目代码资源:

可能还在审核中,请等待。。。
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>

<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.()");
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】
java:spring actuator添加自定义endpoint插图
访问【http://localhost:8080/actuator/chzEndpoint/p1】
java:spring actuator添加自定义endpoint插图(1)
访问【POST http://localhost:8080/actuator/chzEndpoint/p1】
java:spring actuator添加自定义endpoint插图(2)
java:spring actuator添加自定义endpoint插图(3)
访问【DELETE http://localhost:8080/actuator/chzEndpoint/p1】
java:spring actuator添加自定义endpoint插图(4)
java:spring actuator添加自定义endpoint插图(5)

本站无任何商业行为
个人在线分享 » java:spring actuator添加自定义endpoint
E-->