如何做好springboot監控集成

1. 引入prometheus 到 pom.xml

  
       <!-- 作爲web項目必須的依賴 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-actuator</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-actuator-autoconfigure</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-security</artifactId>
       </dependency>

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

2. 在aop切面中寫一個Counter

            Counter.builder("request.times." + action).description("kkkk").
                    tag(action, method)
                    .register(meterRegistry)
                    .increment();

3. .properties配置

.properties

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=
management.endpoint.enabled=true
management.endpoint.httptrace.enabled=true
management.endpoint.shutdown.enabled=true
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.endpoint.health.show-details=ALWAYS
management.enabled-by-default=true
management.metrics.export.appoptics.enabled: true
management.metrics.export.appoptics.step: 10s
management.metrics.export.appoptics.api-token: fake
# actuator的metrics接口是否需要安全保證
management.endpoints.metrics.sensitive = false
# actuator的metrics接口是否開啓
management.endpoints.metrics.enabled=true

*.yml

spring:
  ##去除有問題的指標,因爲指標名重複產生bug
  metrics:
    servo:
      enabled: false
management:
  security:
    enabled: false

4. 下載相關服務

prometheus-2.17.1.windows-amd64.tar.gz
grafana-6.7.2.windows-amd64.zip

5.開啓prometheus服務

5.1 根據情況補充 metrics_path 和 targets 到prometheus.yml配置中
5.2 執行 prometheus.exe

效果圖

6. 啓動 grafana 連接 prometheus服務

在這裏插入圖片描述

7.補充

7.1. 可以通過請求獲取可用的資源
curl http://localhost:9090/api/v1/label/__name__/values
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章