This article introduces SpringCloud using Sofa-lookout, based on Eureka service discovery.

Prospect of 1.

This article is the follow-up of the previous articles. In fact, it was unnecessary to write at first, but recently a friend of mine asked me a question about this, so I decided to write an article based on Eureka monitoring using Pormetheus monitoring Sofa-Lookout monitoring.

Take a look at the previous posts:

SpringBoot Monitoring with Prometheus

SpringBoot monitoring with SOFA-Lookout

SpringCloud Monitoring with Prometheus (Based on Eureka)

As for using Consul for discovery monitoring, I won’t cover it here, but there are many similar articles online. There are some differences between this article and the above one. Sofa-Lookout monitors by exposing a port, which is quite special, so here is a reference for you.

2.Eureka Server

The github address of the Eureka-Consul-Adapter project is:

Github.com/twinformati…

Here we introduce the content of Eureka Server, which is to add the above dependencies on the basis of ordinary Eureka Server. The complete POM is as follows:

<? 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the 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 - the 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> <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>

Copy the code

The configuration file is as follows:

server.port=8761

eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

## Prohibit yourself from registering with yourself
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Copy the code

The startup classes are as follows:

package com.dalaoyang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @ SpringBootApplication @ EnableEurekaServer / / start the service registry public class SpringcloudSofalookoutServerApplication {public static void main(String[] args) { SpringApplication.run(SpringcloudSofalookoutServerApplication.class, args); }}Copy the code

3.Eureka Client

Here are a few differences from the previous article “SpringBoot uses SOFA-Lookout monitoring”. First add SOFA dependency to POM file:

<? 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the 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 - the 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 - the boot - starter < / artifactId > < version > 1.5.2 < / version > < / dependency > < the dependency > < the 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>

Copy the code

The configuration file is as follows:

# # the port number
server.port=8888
# service name
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}




Copy the code

The following two parameters need to be explained:

  • Com. Alipay. Sofa. Lookout. Prometheus – exporter – server – port: sofa – lookout port
  • Eureka. Instance. metadata-map.address: encapsulated Eureka meta-information containing the address of the real SOFA -lookout data for later use by Prometheus.

4.Prometheus

Prometheus is configured as follows:

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

Copy the code

Here are a few things to note:

  • 172.16.103.33 is my Intranet IP, which doesn’t require much attention.
  • The relabel_configs tag is used to modify some default configurations, such as this article’s use of the __meta_consul_metadata_address attribute instead of the __address__ attribute, Eureka.instance.metadata-map. address = eureka.instance.metadata-map.address; This is what the Eureka-Consul-Adapter relies on to convert for us.
  • How are the above two attributes obtained? Tell you next!

5. Test

Start EurekaServer, EurekaClient, and Prometheus respectively, and first look at Eureka, as shown in the figure.

Now that the instance is registered, look at Prometheus/Targets, as shown.

From this we can see that Prometheus has collected all the labels successfully. Next we will check the labels with the mouse pointer, as shown in the figure.

The content of the float box is the property value of the corresponding instance, which can be used accordingly.

Then look at the corresponding Grafana, and you can see the corresponding information, as shown in the figure

6. The source code

Eureka Server: gitee.com/dalaoyang/s…

Eureka Client: gitee.com/dalaoyang/s…