前言

  • 常在河邊走那裡不失鞋,當一個後端工程師,常常會遇到上線Application server突然無法壞掉的問題,因此我們需要一個監控的Server來輔助我們上線的Application,本篇將使用Prometheus監控server當作範例來做為展示

步驟

Spring boot 安裝Prometheus Client

  • 在pom.xml上新增兩個dependency
    • spring-boot-starter-actuator: spring boot內建的監控library
    • micrometer-registry-prometheus: 在spring boot上提供prometheus metrics格式
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
  </dependency>

Spring boot 調整設定檔

  • 在applcation.yml 新增下列設定
management:
  endpoint:
    health:
      show-details: always
    prometheus:
      enabled: true
  endpoints:
    web:
      exposure:
        include: prometheus,health
  metrics:
    tags:
      application: ${spring.application.name}
  health:
    diskspace:
      enabled: true
    defaults:
      enabled: false
  • 設定好,啟動spring boot即可在http://:/actuator/prometheus看到metrics

參考連結