本文介绍SpringCloud使用Prometheus,基于Eureka服务发现。

1.Prometheus介绍

在之前写过两篇有关Prometheus使用的文章,如下:

《SpringBoot使用prometheus监控》

《SpringBoot使用SOFA-Lookout监控》

但是如果使用微服务的话,一个服务一个服务的配置似乎太麻烦,Prometheus提供了很多服务发现的机制去统一配置服务,具体可以查看官网介绍:https://prometheus.io/docs/prometheus/latest/configuration/configuration/

包含如下这些配置:

image

从图中可以看出,这里提供了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"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.dalaoyang springcloud_prometheus_server 0.0.1-SNAPSHOT springcloud_prometheus_server springcloud_prometheus_server

<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>

```

这里需要注意一下SpringCloud版本与eureka-consul-adapter的对照,如下所示。

image

剩下的就是一些简单地配置,如配置文件:

``` 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"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.dalaoyang springcloud_prometheus_client 0.0.1-SNAPSHOT springcloud_prometheus_client springcloud_prometheus_client

<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>

```

配置文件如下,端口号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界面,如下:

image

接下来查看一下Prometheus,如下:

image

从上图可以看到,服务以及被收集了,接下来查看grafana控制台,如图:

image

6.源码

源码地址:

Eureka Server : https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_prometheus_server

Eureka Client : https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_prometheus_client