Spring Cloud-Zipkin (brave-okhttp) for monitoring

This paper is intercepted from: http://blog.csdn.net/liaokailin/article/details/52077620

This blog is based on the blog of God. Refuse to reach out to the party.

What is zipkin?

        Zipkin It is an open source distributed real-time data tracking system based on Google Dapper The paper was designed and developed by Twitter. Its main function is to aggregate real-time monitoring data from various heterogeneous systems to track the system delay problem under the micro-service architecture.

1. Getting zipkin runtime interface package

If you don't use zipkin's jar package to run, you can also use spring boot to build zipkin's services.

1.1 Download service jar package on official website

Enter the official website http://zipkin.io/pages/quickstart.html, then use the command to start the service jar.

Officially, jar packages are available, as well as jar packages for zipkin on linux via weget.

Click on the connection indicated by the arrow to download the jar package. The following red box shows how to get the jar package under linux.

1.2 Run the official jar package and view the zipkin interface

Run the official downloaded jar package, as shown in the following figure

1.3 zipkin interface test

After running successfully, we can enter http://localhost:9411 in the browser and see the monitoring page of zipkin.

Note: The blogger's screenshot is that there are already services under surveillance. You don't have service surveillance, so they are empty.

 

2. java code implementation monitoring

This program is based on maven. Students who can't Maven go to see it first, but it's relatively simple to use.

2.1 pom.xml configuration

Full pom.xml configuration

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

    <groupId>com.jg.zipkin</groupId>
    <artifactId>zipkin-brave</artifactId>
    <packaging>jar</packaging>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
    </parent>



    <!-- https://mvnrepository.com/artifact/io.zipkin.brave/brave-core -->
    <dependencies>

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

        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-core</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-http</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spancollector-http</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-web-servlet-filter</artifactId>
            <version>3.9.0</version>
        </dependency>

        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
    </repositories>


    <!-- Package as an executable jar -->
    <build>
        <finalName>server1</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

2.2 Program Realization

2.2.1 New HomeController class

package com.jg.zipkin.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/")
public class HomeController {

    @Autowired
    private OkHttpClient client;

    private  Random random = new Random();

    @RequestMapping("start")
    public String start() throws InterruptedException, IOException {
        int sleep= random.nextInt(100);
        TimeUnit.MILLISECONDS.sleep(sleep);
        Request request = new Request.Builder().url("http://localhost:9090/foo").get().build();
        Response response = client.newCall(request).execute();
        return " [service1 sleep " + sleep+" ms]" + response.body().toString();
    }


    @RequestMapping("foo")
    public String foo() throws InterruptedException, IOException {
        int sleep= random.nextInt(100);
        TimeUnit.MILLISECONDS.sleep(sleep);
        Request request = new Request.Builder().url("http://localhost:9091/bar").get().build();  //service3
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        request = new Request.Builder().url("http://localhost:9092/tar").get().build();  //service4
        response = client.newCall(request).execute();
        result += response.body().string();
        return " [service2 sleep " + sleep+" ms]" + result;
    }


    @RequestMapping("bar")
    public String bar() throws InterruptedException, IOException {  //service3 method
        int sleep= random.nextInt(100);
        TimeUnit.MILLISECONDS.sleep(sleep);
        return " [service3 sleep " + sleep+" ms]";
    }

    @RequestMapping("tar")
    public String tar() throws InterruptedException, IOException { //service4 method
        int sleep= random.nextInt(100);
        TimeUnit.MILLISECONDS.sleep(sleep);
        return " [service4 sleep " + sleep+" ms]";
    }



}

2.2.2 New Application Class

package com.jg.zipkin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

   public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);

    }
}

 

2.2.3 New ZipkinConfig class

This class is the core class of zipkin

package com.jg.zipkin;

import okhttp3.OkHttpClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.Sampler;
import com.github.kristofa.brave.SpanCollector;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpSpanCollector;
import com.github.kristofa.brave.okhttp.BraveOkHttpRequestResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;


@Configuration
public class ZipkinConfig {

    @Autowired
    private ZipkinProperties properties;


    @Bean
    public SpanCollector spanCollector() {
        HttpSpanCollector.Config config = HttpSpanCollector.Config.builder()
                                                              .connectTimeout(5000)//5s, default 10s
                                                              .readTimeout(6000)//5s, default 60s
                                                              .compressionEnabled(false)//By default false, will span be gzipped before transport?
                                                              .flushInterval(1)//1s
                                                              .build();
        
        return HttpSpanCollector.create("http://10.114.72.122:1011", config, new EmptySpanCollectorMetricsHandler());
    }


    @Bean
    public Brave brave(SpanCollector spanCollector){
        Brave.Builder builder = new Brave.Builder("server1");  //Specify state
        builder.spanCollector(spanCollector);
        builder.traceSampler(Sampler.ALWAYS_SAMPLE);
        Brave brave = builder.build();
        return brave;
    }

    @Bean
    public BraveServletFilter braveServletFilter(Brave brave){
        BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),brave.serverResponseInterceptor(),new DefaultSpanNameProvider());
        return filter;
    }

    @Bean
    public OkHttpClient okHttpClient(Brave brave){
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new BraveOkHttpRequestResponseInterceptor(brave.clientRequestInterceptor(), brave.clientResponseInterceptor(), new DefaultSpanNameProvider()))
                .build();
        return client;
    }
}

2.2.4 Configuration Port Number

Configuration in application.properties
 

    server.port=8080

 

Configure the three services according to the above configuration. The basic configuration is exactly the same as the above configuration. Just modify the port number and service name.

The service described above is called server1, and the port number is 8080.

Next, configure the other three services:

server2:

In the ZipkinConfig class, modify the service name

Modify the port number as follows:

server.port=9090

server3:

In the ZipkinConfig class, modify the service name

Modify the port number as follows:

server.port=9091

server4:

In the ZipkinConfig class, modify the service name

Modify the port number as follows:

server.port=9092

2.3 Test

Start four services.

Access: http://localhost:8080/start

Get the following access return values (multiple visits):

We're going back to zipkin's ui:

You can see our four services.

Select the corresponding service on the UI, select the corresponding time, and click Find to get the response request information.

As shown in the figure below.

Specific display information:

In the ui interface of zipkin, click Dependencies in the header navigation bar, select the appropriate time, and click Analysis.

The relationship shown in the following figure can be obtained.

 

This is the end of zipkin+brave. This article is based on the article at the top. It's really the author who stands on the shoulders of giants and feels like sharing.

Keywords: github Maven Spring Java

Added by Poofman on Mon, 08 Jul 2019 03:45:47 +0300