Spring使用 Spring-cloud-aws 和 cloudwatch 指标启动

Springboot with Spring-cloud-aws and cloudwatch metrics

我想开始在我的 Spring 启动应用程序中使用指标,我还想将它们发布到我的亚马逊 cloudwatch

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

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

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

勾选this conversation:

@sachinlad Indeed the documentation is unfortunately missing, we will create a updated version within the next releases. Do enable the metic export to Cloud Formation, you will need to configure the namespace with the property cloud.aws.cloudwatch.namespace

Have a look at the integration test 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 that is an integration test and export the metrics to cloud formation.

Hope that helps, feel free to come back in case of any problems.

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

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

我在项目里设置好后写的

来自header:

"Article explains how to send Spring Boot and Netflix Servo metrics to AWS CloudWatch. Morover it describes mechanisms making it happen. It also mentions problems I run into trying to do the same with Spring Boot and Spectator."

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

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

这是 spring 引导 2 的设置。

使用 spring 启动 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

为此我查阅了多份文件。他们身上缺少了某些东西。因此,我正在编写从您的 spring 启动应用程序在 Cloudwatch 上设置自定义指标所需的一切。

设置这些属性:

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

小心提及命名空间。此名称将反映在您的 cloudwatch 指标中。 cloudwatch 注册表的默认 "step" 为 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 实例并简单地创建您想要的任何指标。

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

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

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

smsCounter.increment();