本文介紹SpringCloud使用Prometheus,基于Eureka服務發現。
1.Prometheus介紹
在之前寫過兩篇有關Prometheus使用的文章,如下:
《SpringBoot使用prometheus監控》 《SpringBoot使用SOFA-Lookout監控》但是如果使用微服務的話,一個服務一個服務的配置似乎太麻煩,Prometheus提供了很多服務發現的機制去統一配置服務,具體可以檢視官網介紹:[
https://prometheus.io/docs/prometheus/latest/configuration/configuration/]()包含如下這些配置:
從圖中可以看出,這裡提供了Consul的服務發現機制,沒有Eureka的服務發現機制。但是如果Eureka想要使用的話可以通過配置一個擴充卡的方式,使用consul_sd_config配置的方式使用Prometheus服務發現。
2.Eureka Server
建立一個Eureka Server,這裡使用的Eureka最新版本Greenwich.SR1,也就是現在Idea預設建立的,在配置中加入eureka-consul-adapter依賴,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_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的對照,如下所示。
剩下的就是一些簡單地配置,如配置檔案:
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);
}
}
3.Eureka Client
其實這個也沒什麼好說的,和普通使用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);
}
}
4.Prometheus配置
在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]
5.測試
分别啟動Eureka-Server和Eureka-Client,首先檢視Eureka界面,如下:
接下來檢視一下Prometheus,如下:
從上圖可以看到,服務以及被收集了,接下來檢視grafana控制台,如圖:
6.源碼
源碼位址:
Eureka Server :
https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_prometheus_serverEureka Client :
https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_prometheus_client