【问题标题】:Springboot with Spring-cloud-aws and cloudwatch metrics带有 Spring-cloud-aws 和 cloudwatch 指标的 Spring Boot
【发布时间】:2018-01-11 14:21:33
【问题描述】:

我想开始在我的 Springboot 应用程序中使用指标,并且我还想将它们发布到我的 amazon cloudwatch

我知道使用 Springboot,我们可以激活提供内存指标并将其发布到 /metrics 端点的 spring-actuator。

我偶然发现 Spring-cloud 似乎有一些库可以定期将这些指标发布到 Cloudwatch,但是我不知道如何设置它们?使用方法绝对有0个例子。

谁能解释将指标发送到 cloudwatch 的步骤是什么?

【问题讨论】:

  • 值得注意的是,不使用 Spring Cloud 也可以做到这一点——如果你像我一样发现配置和寻找更轻量级的解决方案太痛苦了。见:stackoverflow.com/questions/56897336

标签: spring-boot metrics amazon-cloudwatch spring-cloud-aws


【解决方案1】:

查看this conversation:

@sachinlad 不幸的是,文档确实丢失了,我们将 在下一个版本中创建更新版本。启用metic 导出到 Cloud Formation,您需要配置命名空间 使用属性 cloud.aws.cloudwatch.namespace

看看集成测试 https://github.com/spring-cloud/spring-cloud-aws/blob/master/spring-cloud-aws-integration-test/src/test/java/org/springframework/cloud/aws/metric/MetricExporterTest.java 这是一个集成测试并将指标导出到云形成。

希望对您有所帮助,如有任何问题,请随时回来。

【讨论】:

    【解决方案2】:

    你可以在这里查看我的文章:

    https://dkublik.github.io/2017/10/28/springboot-metrics-with-servo-and-aws-cloudwatch.html

    我是在我的项目中设置后写的。

    来自标题:

    “文章解释了如何将 Spring Boot 和 Netflix Servo 指标发送到 AWS CloudWatch。更重要的是它描述了实现它的机制。它还提到了我在尝试对 Spring Boot 和 Spectator 执行相同操作时遇到的问题。”

    编辑: 新版本: https://dkublik.github.io/2018/08/26/springboot-metrics-with-micrometer-and-aws-cloudwatch.html

    【讨论】:

    • 从那时起我就放弃了,即使阅读了@Hendy 的答案。你的文章“似乎”有一个完整的例子。我没有测试过
    • 看起来这篇文章的更新版本适用于更新的库版本:dkublik.github.io/… 原始引用的文章使用不同的属性名称进行配置,这不适用于库版本。使用更新后的博文至少应该可以让您开始在 AWS 中查看指标。
    • 这正是我们不希望在 SO 上仅提供链接答案的原因,链接已失效:-(
    【解决方案3】:

    如果您想要一个不涉及使用整个 Spring Cloud 库的现成解决方案,您可以使用:https://github.com/dipayan90/spring-actuator-cloudwatch

    【讨论】:

      【解决方案4】:

      这是 Spring Boot 2 的设置。

      使用 spring boot 2.0.3。

      添加这些依赖项:

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-aws-actuator</artifactId>
          <version>2.0.0.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-aws</artifactId>
          <version>2.0.0.RELEASE</version>
      </dependency>
      

      application.yml:

      # you might want to set this to true depending on your setup
      cloud.aws.stack.auto: false
      # set static region to avoid s3 error - adjust region accordingly
      cloud.aws.region.static: eu-west-1
      
      management:
        metrics.export.cloudwatch.namespace: my-app
        metrics.export.cloudwatch.batch-size: 20
      

      【讨论】:

        【解决方案5】:

        为此,我浏览了多个文档。他们中缺少某些东西。因此,我正在编写从您的 Spring Boot 应用程序在 Cloudwatch 上设置自定义指标所需的所有内容。

        设置这些属性:

        management.metrics.export.cloudwatch.namespace=my-application
        management.metrics.export.cloudwatch.batchSize=20
        management.metrics.export.cloudwatch.step=5s
        

        小心提及命名空间。此名称将反映在您的 cloudwatch 指标中。 cloudwatch 注册表的默认“步长”为 1 分钟。所以你可以在这里改变它。

        在你的 pom 中添加这些依赖项(如果还没有的话):

        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-actuator</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws-messaging</artifactId>
        </dependency>
        

        完成了。现在您将能够在 cloudwatch 上查看指标。 现在,如果您想将自定义指标推送到某处,则使用 MetricsRegistry 的 Autowire 实例并简单地创建您想要的任何指标。

        让我们创建一个发送短信的计数器,例如:

        Counter smsCounter = Counter.builder(COUNT_METRICS)
                    .tag("type", "sms")
                    .description("The number of sms sent")
                    .register(meterRegistry);
        

        现在更新执行操作的计数器,如下所示:

        smsCounter.increment();
        

        【讨论】:

          猜你喜欢
          • 2016-07-24
          • 2021-04-02
          • 1970-01-01
          • 2021-04-10
          • 2020-10-26
          • 2017-11-13
          • 2015-09-03
          • 2016-09-14
          • 2018-05-11
          相关资源
          最近更新 更多