SpringBoot1.5.X接入prometheus監控基于Eureka服務自發現

2020年9月14日16:12:05 發表評論 6,080 ℃

Prometheus監控對微服務的整合還是比較友好的,尤其是在有大量微服務的時候,不可能每新增一個服務,就去手動修改Prometheus的配置,增加服務監控配置,這個時候就需要使用服務自發現。今天阿湯博客就介紹下SpringBoot1.5.X(Spring Cloud Edgware.SR6)(不同的版本主要區別于引入依賴的版本不同)接入prometheus對微服務JVM監控,如何實現prometheus對服務自發現。

1、微服務端引入pom依賴:

<!-- prometheus -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.0.11</version>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-spring-legacy</artifactId>
    <version>1.0.11</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

spring-boot-starter-security依賴是為了開啟對監控數據獲取接口進行用戶名密碼校驗的,如果沒有這個校驗我們的監控接口相當于在裸奔。任何人都可以通過這個接口獲取我們的監控數據。

2、添加一個啟動類,使prometheus獲取我們當前項目的名稱以及其他信息。

package com.hjkj.component;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.spring.autoconfigure.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MicrometerConfiguration {
    @Bean
    MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
        return meterRegistry1 -> {
            meterRegistry.config()
                    .commonTags("application", "micrometer-gateway");
        };
    }
}

3、由于Prometheus v2.21.0之前的版本不支持 Eureka注冊中心的服務發現,所以Eureka Server需要引入一個適配依賴(主要用于prometheus自動發現服務):

<dependency>
    <groupId>at.twinformatics</groupId>
    <artifactId>eureka-consul-adapter</artifactId>
    <version>0.0.1</version>
</dependency>

eureka-consul-adapter版本和SpringBoot的對應關系:

Java 1.8+

Versions 1.1.x and later 

Spring Boot 2.1.x

Spring Cloud Greenwich

Versions 1.0.x and later

Spring Boot 2.0.x

Spring Cloud Finchley

Versions 0.x

Spring Boot 1.5.x

Spring Cloud Edgware

4、配置application.yml

management:
    context-path: /actuator
    endpoints:
        jmx:
            exposure:
                include: '*'
        web:
            exposure:
                include: info,health,prometheus
    metrics:
        distribution:
            percentiles-histogram[http:server:requests]: true
    security:
        enabled: true
security:
    basic:
        enabled: true
        path: /actuator
    user:
      name: yoursUser
      password: yoursPassWord

5、配置prometheus.yml,增加job。

  - job_name: "m.maowutv.com-java"
    scheme: http
    metrics_path: '/actuator/prometheus'
    basic_auth:
      username: yoursUser
      password: yoursPassWord
    consul_sd_configs:
      - server: 'eureka-server-adress:8761'
        scheme: http
        services:
    relabel_configs:
      - source_labels: [__meta_consul_service_id]
        target_label: instance

按照以上配置以后,prometheus就會自己去eureka注冊中心拉取服務進行監控信息獲取,效果圖如下:

SpringBoot1.5.X接入prometheus監控基于Eureka服務自發現

相關閱讀:

SpringBoot1.5.x 使用prometheus監控Tomcat線程顯示異常解決辦法

Prometheus報錯Error refreshing service Unexpected response code: 503解決辦法

【騰訊云】云服務器、云數據庫、COS、CDN、短信等云產品特惠熱賣中

發表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: