天天看點

SpringCloud使用Sofa-lookout監控(基于Eureka)

本文介紹SpringCloud使用Sofa-lookout,基于Eureka服務發現。

1.前景

本文屬于是前幾篇文章的後續,其實一開始感覺這個沒有什麼必要寫的,但是最近一個朋友問我關于這個的問題,是以想着還是寫一篇基于Eureka監控使用Pormetheus監控Sofa-Lookout監控的文章。

前幾篇文章請看:

《SpringBoot使用prometheus監控》 《SpringBoot使用SOFA-Lookout監控》 《SpringCloud使用Prometheus監控(基于Eureka)》

至于使用consul服務發現監控這裡就不介紹了,網上的文章很多,千篇一律。這一篇與上面的這個文章還是有一些不同的,Sofa-Lookout是通過暴露一個端口來進行監控,比較特殊,是以這裡給大家借鑒一下。

2.Eureka Server

本文還是需要在Eureka上面加入eureka-consul-adapter依賴,eureka-consul-adapter項目github位址:

https://github.com/twinformatics/eureka-consul-adapter

這裡在介紹一下Eureka Server内容,就是在普通Eureka Server基礎上加入上述依賴,完整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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalaoyang</groupId>
    <artifactId>springcloud_sofalookout_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud_sofalookout_server</name>
    <description>springcloud_sofalookout_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>

</project>
           

配置檔案如下:

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           

啟動類如下:

package com.dalaoyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //啟動服務注冊中心
public class SpringcloudSofalookoutServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudSofalookoutServerApplication.class, args);
    }

}
           

3.Eureka Client

這裡有一些是與之前

文章不同的地方,首先在pom檔案中加入sofa依賴:

<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalaoyang</groupId>
    <artifactId>springcloud_sofalookout_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud_sofalookout_client</name>
    <description>springcloud_sofalookout_client</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-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alipay.sofa.lookout</groupId>
            <artifactId>lookout-sofa-boot-starter</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.alipay.sofa.lookout</groupId>
            <artifactId>lookout-reg-prometheus</artifactId>
            <version>1.5.2</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>

</project>
           

配置檔案配置如下:

##端口号
server.port=8888
##服務名稱
spring.application.name=springboot_sofalookout_client
##EurekaServer
eureka.client.service-url.defaultZone=http://server1:8761/eureka/

com.alipay.sofa.lookout.prometheus-exporter-server-port=8081
eureka.instance.metadata-map.address=${spring.cloud.client.ip-address}:${com.alipay.sofa.lookout.prometheus-exporter-server-port}



           

其中需要解釋如下兩個參數:

  • com.alipay.sofa.lookout.prometheus-exporter-server-port:sofa-lookout端口
  • eureka.instance.metadata-map.address:封裝的Eureka元資訊,内容是真正sofa-lookout資料的位址,友善稍後prometheus使用,這個屬性不是必須要叫這個名字!!!

4.Prometheus

prometheus配置如下:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['127.0.0.1:9090']

  - job_name: 'consul-prometheus'
    consul_sd_configs:
      - server: '172.16.103.33:8761'
        services: [SPRINGBOOT_SOFALOOKOUT_CLIENT]
    relabel_configs:
      - source_labels: ['__meta_consul_metadata_address']
        target_label:  __address__
           

這裡有幾點需要注意的:

  • 172.16.103.33是我的内網IP,這個不需太多關注。
  • relabel_configs标簽用于修改一些預設配置,比如本文使用__meta_consul_metadata_address屬性替代__address__屬性,這裡的__meta_consul_metadata_address屬性值就是我們在application.properties中的eureka.instance.metadata-map.address屬性值,這個是eureka-consul-adapter依賴為我們轉換的。
  • 上面兩個屬性怎麼擷取的? 接下來告訴你!

5.測試

分别啟動EurekaServer,EurekaClient,prometheus,首先檢視Eureka,如圖所示。

執行個體已經注冊了,接下來檢視prometheus/targets,如圖所示。

從這裡可以看到,prometheus已經可以成功收集了,接下來我們檢視一下都有哪些屬性,滑鼠放到對應的labels上,如圖所示。

浮框的内容就是對應執行個體的屬性值,可以對應使用。

接下來檢視對應Grafana,也可以檢視到對應資訊了,如圖

6.源碼

Eureka Server源碼位址:

https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_sofalookout_server

Eureka Client源碼位址:

https://gitee.com/dalaoyang/springcloud_learn/tree/master/springcloud_sofalookout_client