在Spring Boot 2中启用/actuator/prometheus

经纬

在Spring Boot 2中,通过增强度量功能来支持Prometheus,但按照官方参考文档的方法并不起作用,所以我记录了一些解决方法。

– Spring Boot 2.0.0 的测试环境

步骤

添加依赖关系

在POM中添加micrometer-registry-prometheus的依赖关系。官方参考文档中没有提到这一点。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- この依存関係を追加するとうまくいった -->
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

增加终端点

在application.properties中添加Prometheus的终端点。默认情况下,只公开info和health。

# infoとhealthはデフォルトでexposeされているのでprometheusを追加
management.endpoints.web.exposure.include=info,health,prometheus

启动时的日志检查

启动后将输出以下日志。如果输出/actuator/prometheus,则表示正常。如果没有在POM中添加micrometer-registry-prometheus,则只会输出info和health信息。

Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
Mapped "{[/actuator/prometheus],methods=[GET],produces=[text/plain;version=0.0.4;charset=utf-8]}"

就是这样了。

广告
将在 10 秒后关闭
bannerAds