为适用于AWS Cloudwatch的Micrometer配置SpringBoot 2应用程序

Man*_*oid 6 spring-boot micrometer

我有一个springboot 2应用程序,我想在AWS Cloudwatch中显示指标。

我已经将pom中的micrometer cloudwatch依赖性包括在内。

此处记录的是各种指标系统的设置,但没有cloudwatch的说明。

我需要为cloudwatch做哪些其他配置?

Fen*_*cer 2

首先,您可能需要添加一些额外的依赖项。我需要以下内容:

  • org.springframework.boot - spring-boot-starter-actuator
  • org.springframework.cloud - spring-cloud-starter-aws
  • io.micrometer - 微米核心
  • io.micrometer - micrometer-registry-cloudwatch

Boot 无法管理这些依赖项的版本(在我的例子中除了执行器之外),因此您可能必须找出适合您的版本。

此外,还必须设置一些应用程序属性:

# disable unwanted features to prevent autoconfigure which will produce errors and abort of application startup eventually
# alternatively you can try to configure those features correctly if you intend to use them
cloud.aws.stack.auto=false
# enable micrometer for cloudwatch (only where there is actually access to it)
management.metrics.export.cloudwatch.enabled=true
# set the namespace that will contain the metrics for this application
management.metrics.export.cloudwatch.namespace=test
# set max batch size to the actual maximum (if not a bug in certain versions of micrometer for cloudwatch will send
# batches that are too big) 
management.metrics.export.cloudwatch.batchSize=20
Run Code Online (Sandbox Code Playgroud)

下一步将在 AWS 中进行。与您的 EC2 实例(或您正在使用的任何实例)关联的角色需要拥有 权限CloudWatch:PutMetricData

使用此配置应该为您的 Spring-Boot-Application 启用 CloudWatch-Monitoring。

我遇到的消息来源之一指出您应该使用:

cloud.aws.credentials.instanceProfile=false
Run Code Online (Sandbox Code Playgroud)

这将阻止 Spring Boot 自动获取将指标推送到 CloudWatch 所需的凭据。您还可以通过另一种方式提供自己的凭据,但我没有尝试这样做。

  • 我尝试了不同的解决方案并最终为我工作。现在,aws cloudwatch 中仅报告我的执行器指标,而不是我使用 Micrometer 创建的自定义指标。(计数器、仪表)。你对此有什么想法吗?谢谢。 (2认同)