https://gitee.com/DanShenGuiZu/learnDemo/tree/master/actuator-learn/actuator01
用于查看当前应用的各类重要度量指标
http://127.0.0.1:8080/actuator/metrics
http://127.0.0.1:8080/actuator/metrics/{MetricName}
查看JVM最大内存的详细信息
http://127.0.0.1:8080/actuator/metrics/jvm.memory.max
用query param的方式查看 某个区域 的详细信息
# 查看tag=id的Metaspace区域 的详细信息
http://127.0.0.1:8080/actuator/metrics/jvm.memory.max?tag=id:Metaspace
我们在一个 Contoller 使用一个类型为 Gauge 的计数器来记录一个数值
@RestController
public class GaugeController {
@GetMapping("/gauge/hello")
public String hello() {
Metrics.gauge("user.test.gauge", 3);
return " gauge hello";
}
}
http://127.0.0.1:8080/gauge/hello
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.gauge
/**
* 通过http请求,触发这个服务
*/
@RestController
public class MyCounterController {
@Autowired
MyCounterService myCounterService;
@GetMapping("/counter/hello")
public String hello() {
// 触发这个服务
myCounterService.processCollectResult();
return " counter hello";
}
}
/**
* 自定义一个计数器服务:
*/
@Service
public class MyCounterService {
static final Counter userCounter = Metrics.counter("user.counter.total", "services", "demo");
// 自定义一个计数器服务
public void processCollectResult() {
userCounter.increment(1D);
}
}
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
# 执行这个请求7次
http://127.0.0.1:8080/counter/hello
# 查看自定义的指标明细,可以看到计数次数为7
http://127.0.0.1:8080/actuator/metrics/user.counter.total
@RestController
public class TimeController {
private Timer timer = Metrics.timer("user.test.timer", "timer", "timersample");
@GetMapping("/time/hello")
public String hello() {
// 执行createOrder方法并记录执行时间
timer.record(() -> createOrder());
return " time hello";
}
// 模拟方法耗时
private void createOrder() {
try {
// 耗时3秒
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}
}
}
# 请求3次
http://127.0.0.1:8080/time/hello
# 查看自定义指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.timer
@RestController
public class SummaryController {
private DistributionSummary summary = Metrics.summary("user.test.summary", "summary", "summarysample");
@GetMapping("/summary/hello")
public String hello() {
// 使用 Summary 来连续记录三次值
summary.record(2D);
summary.record(3D);
summary.record(4D);
return " summary hello";
}
}
# 执行请求,这里执行了16次
http://127.0.0.1:8080/summary/hello
# 查看自定义的指标
http://127.0.0.1:8080/actuator/metrics/
# 查看自定义指标的明细
http://127.0.0.1:8080/actuator/metrics/user.test.summary