代码之家  ›  专栏  ›  技术社区  ›  SoT

如何在Spring引导应用程序上执行分布式度量

  •  0
  • SoT  · 技术社区  · 3 年前

    今天,我收到了在我们的Spring引导应用程序上使用Prometheus、Grafana、Actuator应用指标的要求。

    以下是详细信息:

    • 计算HTTP API终结点的HTTP输入数
    • 计算HTTP API终结点的HTTP输出数
    • 获取HTTP API端点处理的延迟
    • 最后,获取端到端流的延迟(即当用户到达API端点时,它通过kafka调用另一个微服务,然后转到下一个微服务器blabla,延迟为所用的总时间)

    我认为前3个需求可以很容易地归档:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    

    然后:

    Metrics.counter(name, tags).increment();
    Metrics.timer(name, tags).record(duration.toMillis(), TimeUnit.MILLISECONDS);
    

    但最后一个要求对我来说太难了。我不知道如何在微服务中收集指标。

    请给我一些可能的和可以接受的解决方案。非常感谢。

    0 回复  |  直到 3 年前
        1
  •  0
  •   zohreh    3 年前

    要测量方法X中涉及多个服务的端到端流的延迟,可以使用 @定时 注释。以下是您答案的优化版本:

    @Timed(METRIC_NAME)
    public Response methodX() {
    // Service call 1
    // ...
    // Service call n
    // Perform the necessary operations
    
    return response;
    }
    

    通过注释 方法X() 具有 @定时 并提供自定义度量名称( METRIC_NAME ),可以捕获方法执行的持续时间。这允许您测量流的端到端延迟。

    确保您有必要的依赖项和配置来启用度量收集,例如使用Micrometer与Prometheus和Spring Boot Actuator。

    记住调整度量名称( METRIC_NAME )反映测量操作的目的和上下文的东西。

    通过这种优化,您可以轻松跟踪端到端流的性能,并深入了解方法中每个服务调用的延迟。