本文介紹SpringCloud使用Prometheus,基於Eureka服務發現。java
在以前寫過兩篇有關Prometheus使用的文章,以下:git
《SpringBoot使用SOFA-Lookout監控》spring
可是若是使用微服務的話,一個服務一個服務的配置彷佛太麻煩,Prometheus提供了不少服務發現的機制去統一配置服務,具體能夠查看官網介紹:prometheus.io/docs/promet…apache
包含以下這些配置:springboot
從圖中能夠看出,這裏提供了Consul的服務發現機制,沒有Eureka的服務發現機制。可是若是Eureka想要使用的話能夠經過配置一個適配器的方式,使用consul_sd_config配置的方式使用Prometheus服務發現。bash
建立一個Eureka Server,這裏使用的Eureka最新版本Greenwich.SR1,也就是如今Idea默認建立的,在配置中加入eureka-consul-adapter依賴,pom文件完整內容以下所示。app
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalaoyang</groupId>
<artifactId>springcloud_prometheus_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_prometheus_server</name>
<description>springcloud_prometheus_server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>at.twinformatics</groupId>
<artifactId>eureka-consul-adapter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
複製代碼
這裏須要注意一下SpringCloud版本與eureka-consul-adapter的對照,以下所示。maven
剩下的就是一些簡單地配置,如配置文件:spring-boot
server.port=8761
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
##禁止本身向本身註冊
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
複製代碼
啓動類加入註解啓動註冊中心,以下:
@SpringBootApplication
@EnableEurekaServer //啓動服務註冊中心
public class SpringcloudPrometheusServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudPrometheusServerApplication.class, args);
}
}
複製代碼
其實這個也沒什麼好說的,和普通使用Prometheus同樣,固然,也能夠使用SOFA-Lookout的模式,這裏根據狀況自行選擇便可,這裏以使用micrometer-registry-prometheus依賴爲例,完整pom以下所示。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalaoyang</groupId>
<artifactId>springcloud_prometheus_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_prometheus_client</name>
<description>springcloud_prometheus_client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
複製代碼
配置文件以下,端口號8888,以下:
##端口號
server.port=8888
spring.application.name=springboot_prometheus_client
eureka.client.service-url.defaultZone=http://server1:8761/eureka/
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
複製代碼
啓動類以下:
@SpringBootApplication
public class SpringcloudPrometheusClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudPrometheusClientApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
複製代碼
在Prometheus中配置eureka地址(server),以及須要收集的服務(services)。
- job_name: 'consul-prometheus'
scheme: http
metrics_path: '/actuator/prometheus'
consul_sd_configs:
#consul 地址
- server: '127.0.0.1:8761'
scheme: http
services: [SPRINGBOOT_PROMETHEUS_CLIENT]
複製代碼
分別啓動Eureka-Server和Eureka-Client,首先查看Eureka界面,以下:
接下來查看一下Prometheus,以下:
從上圖能夠看到,服務以及被收集了,接下來查看grafana控制檯,如圖:
源碼地址:
Eureka Server : gitee.com/dalaoyang/s…
Eureka Client : gitee.com/dalaoyang/s…