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: "www.amd5.cn-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监控Tomcat线程显示异常解决办法
Prometheus报错Error refreshing service Unexpected response code: 503解决办法