本文介紹SpringCloud使用Sofa-lookout,基於Eureka服務發現。
1.前景
本文屬於是前幾篇文章的後續,其實一開始感覺這個沒有什麼必要寫的,但是最近一個朋友問我關於這個的問題,所以想著還是寫一篇基於Eureka監控使用Pormetheus監控Sofa-Lookout監控的文章。
前幾篇文章請看:
《SpringCloud使用Prometheus監控(基於Eureka)》
至於使用consul服務發現監控這裡就不介紹了,網上的文章很多,千篇一律。這一篇與上面的這個文章還是有一些不同的,Sofa-Lookout是通過暴露一個埠來進行監控,比較特殊,所以這裡給大家借鑑一下。
2.Eureka Server
本文還是需要在Eureka上面加入eureka-consul-adapter依賴,eureka-consul-adapter專案github地址:
這裡在介紹一下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
這裡有一些是與之前《SpringBoot使用SOFA-Lookout監控》文章不同的地方,首先在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原始碼地址:gitee.com/dalaoyang/s…
Eureka Client原始碼地址:gitee.com/dalaoyang/s…