手记

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

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

1.前景

本文属于是前几篇文章的后续,其实一开始感觉这个没有什么必要写的,但是最近一个朋友问我关于这个的问题,所以想着还是写一篇基于Eureka监控使用Pormetheus监控Sofa-Lookout监控的文章。

前几篇文章请看:

至于使用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.源码

0人推荐
随时随地看视频
慕课网APP