SpringBoot1.5.X接入prometheus監(jiān)控基于Eureka服務(wù)自發(fā)現(xiàn)

2020年9月14日16:12:05 發(fā)表評(píng)論 5,942 ℃

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

1、微服務(wù)端引入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依賴是為了開(kāi)啟對(duì)監(jiān)控?cái)?shù)據(jù)獲取接口進(jìn)行用戶名密碼校驗(yàn)的,如果沒(méi)有這個(gè)校驗(yàn)我們的監(jiān)控接口相當(dāng)于在裸奔。任何人都可以通過(guò)這個(gè)接口獲取我們的監(jiān)控?cái)?shù)據(jù)。

2、添加一個(gè)啟動(dòng)類,使prometheus獲取我們當(dāng)前項(xiàng)目的名稱以及其他信息。

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注冊(cè)中心的服務(wù)發(fā)現(xiàn),所以Eureka Server需要引入一個(gè)適配依賴(主要用于prometheus自動(dòng)發(fā)現(xiàn)服務(wù)):

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

eureka-consul-adapter版本和SpringBoot的對(duì)應(yīng)關(guān)系:

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: "www.zhongjima.net-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就會(huì)自己去eureka注冊(cè)中心拉取服務(wù)進(jìn)行監(jiān)控信息獲取,效果圖如下:

SpringBoot1.5.X接入prometheus監(jiān)控基于Eureka服務(wù)自發(fā)現(xiàn)

相關(guān)閱讀:

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

Prometheus報(bào)錯(cuò)Error refreshing service Unexpected response code: 503解決辦法

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫(kù)、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評(píng)論

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