提交spring-boot-actuator用例
This commit is contained in:
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.actuator;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootActuatorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootActuatorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.actuator.aggregator;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthAggregator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 对所有的自定义健康指标进行聚合,按照自定义规则返回总和健康状态
|
||||
*/
|
||||
@Component
|
||||
public class CustomHealthAggregator implements HealthAggregator {
|
||||
|
||||
@Override
|
||||
public Health aggregate(Map<String, Health> healths) {
|
||||
for (Health health : healths.values()) {
|
||||
// 聚合规则可以自定义,这里假设我们自定义的监控状态中有一项FATAL,就认为整个服务都是不可用的,否则认为整个服务是可用的
|
||||
if (health.getStatus().getCode().equals("FATAL")) {
|
||||
return Health.status("FATAL").withDetail("error code", "综合判断后服务宕机").build();
|
||||
}
|
||||
}
|
||||
return Health.up().build();
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.heibaiying.actuator.endpoint;
|
||||
|
||||
import org.hyperic.sigar.CpuInfo;
|
||||
import org.hyperic.sigar.CpuPerc;
|
||||
import org.hyperic.sigar.Sigar;
|
||||
import org.hyperic.sigar.SigarException;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 自定义端点
|
||||
*/
|
||||
@Endpoint(id = "customEndPoint")
|
||||
@Component
|
||||
public class CustomEndPoint {
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> getCupInfo() throws SigarException {
|
||||
|
||||
Map<String, Object> cupInfoMap = new LinkedHashMap<>();
|
||||
|
||||
Sigar sigar = new Sigar();
|
||||
|
||||
CpuInfo infoList[] = sigar.getCpuInfoList();
|
||||
CpuPerc[] cpuList = sigar.getCpuPercList();
|
||||
|
||||
for (int i = 0; i < infoList.length; i++) {
|
||||
CpuInfo info = infoList[i];
|
||||
cupInfoMap.put("CPU " + i + " 的总量MHz", info.getMhz()); // CPU的总量MHz
|
||||
cupInfoMap.put("CPU " + i + " 生产商", info.getVendor()); // 获得CPU的生产商,如:Intel
|
||||
cupInfoMap.put("CPU " + i + " 类别", info.getModel()); // 获得CPU的类别,如:Core
|
||||
cupInfoMap.put("CPU " + i + " 缓存数量", info.getCacheSize()); // 缓冲存储器数量
|
||||
cupInfoMap.put("CPU " + i + " 用户使用率", CpuPerc.format(cpuList[i].getUser())); // 用户使用率
|
||||
cupInfoMap.put("CPU " + i + " 系统使用率", CpuPerc.format(cpuList[i].getSys())); // 系统使用率
|
||||
cupInfoMap.put("CPU " + i + " 当前等待率", CpuPerc.format(cpuList[i].getWait())); // 当前等待率
|
||||
cupInfoMap.put("CPU " + i + " 当前错误率", CpuPerc.format(cpuList[i].getNice())); // 当前错误率
|
||||
cupInfoMap.put("CPU " + i + " 当前空闲率", CpuPerc.format(cpuList[i].getIdle())); // 当前空闲率
|
||||
cupInfoMap.put("CPU " + i + " 总的使用率", CpuPerc.format(cpuList[i].getCombined()));// 总的使用率
|
||||
}
|
||||
return cupInfoMap;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.heibaiying.actuator.indicator;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthAggregator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 自定义健康检查指标
|
||||
*/
|
||||
@Component
|
||||
public class CustomHealthIndicator implements HealthIndicator {
|
||||
|
||||
@Override
|
||||
public Health health() {
|
||||
double random = Math.random();
|
||||
// 这里用随机数模拟健康检查的结果
|
||||
if (random > 0.5) {
|
||||
return Health.status("FATAL").withDetail("error code", "某健康专项检查失败").build();
|
||||
} else {
|
||||
return Health.up().withDetail("success code", "自定义检查一切正常").build();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# 这里用* 代表暴露所有端点只是为了观察效果,实际中按照需进行端点暴露
|
||||
include: "*"
|
||||
endpoint:
|
||||
health:
|
||||
# 详细信息显示给所有用户。
|
||||
show-details: always
|
||||
health:
|
||||
status:
|
||||
http-mapping:
|
||||
# 自定义健康检查返回状态码对应的http状态码
|
||||
FATAL: 503
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.actuator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootActuatorApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user