Spring 引导执行器 - 实施自定义指标

Spring boot actuator - Implement custom metrics

我想使用执行器为我的 spring boot rest web 服务实现自定义指标或统计数据,但我找不到简单的教程。 例如:

对于 AOP (面向方面​​的编程),这似乎是一个很好的方案,因为这将允许您将此统计逻辑与业务逻辑分开。

查看 Spring 文档以了解有关 AOP 的更多信息以及如何使用 Spring 实现它。

然后您可以在您的控制器上定义一个切入点,并提供一个服务来计算(然后可能存储)数据。

参考下面link AOP Example

对于第二点,解决方案是创建一个端点class(它可以是也可以不是休息控制器class)。例如:

@Component
@RestControllerEndpoint(id = "pfm-statistics")
public class StatisticsEndpoint {
  @GetMapping(value = "/", produces = "application/vnd.openxmlformats- 
     officedocument.spreadsheetml.sheet")
  @ResponseBody
  public byte[] generateStatisticsAsExcel() {
 ...

注意ID是从URL调用的路径。我们也可以创建一个简单的端点,如果需要,只需 return 一个字符串。在这种情况下,我们可以使用@Endpoint 代替@RestControllerEndpoint 注释,作为旁注,id 应始终包含破折号