SpringBoot e-Commerce project mall (20K + STAR) address: github.com/macrozheng/…

Abstract

Spring Cloud Sleuth is a tool for tracing invocations between services in distributed systems. It can visually demonstrate the invocation process of a request, and its usage is described in detail in this article.

Spring Cloud Sleuth introduction

As our system grows larger, the invocation relationships between services become more and more complex. When a client makes a request that passes through multiple services and finally returns a result, each of those services may experience delays or errors that cause the request to fail. At this time, we need the request link tracking tool to help us clarify the service link called by the request and solve the problem.

Add request link tracing to the service

We will demonstrate this functionality through a service call between user-Service and ribbon service. Here, when we call the interface of the ribbon Service, The ribbon Service invokes the interface provided by the User-Service through the RestTemplate.

  • First, add support for the request link tracing function for user-Service and ribbon service.

  • Add dependencies to user-service and ribbon-service:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
Copy the code
  • Modify the application.yml file and configure the zipkin-server access address for collecting logs:
spring:
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      probability: 0.1 # Set the sampling collection probability of Sleuth
Copy the code

Integrate Zipkin to get and analyze logs

Zipkin is an open source project for Twitter to capture and analyze request link tracking logs generated in Spring Cloud Sleuth, which provides a Web interface to visually view request link tracking information.

  • SpringBoot above 2.0 version has don’t need to build its own zipkin – server, we can download it from the address zipkin-server:repo1.maven.org/maven2/io/z…

  • Once the download is complete, run zipkin-server with the following command:

Java jar zipkin - server - 2.12.9 - exec. JarCopy the code
  • Visit the Zipkin page at http://localhost:9411

  • Start eureka-sever, ribbon-service, user-service:

  • Multiple calls (Sleuth for sample collection) ribbon – service interface http://localhost:8301/user/1, have been found after the call to check Zipkin front page request link tracking information;

  • Click to view details to visually see the request invocation link and the elapsed time through each service:

Use Elasticsearch to store trace information

If we restart the Zipkin-server, we will find that all the trace information stored in memory is lost. Sometimes we need to store all the trace information in memory.

Install the Elasticsearch

  • Download Elasticsearch6.2.2 zip and unzip to the specified directory, download address: www.elastic.co/cn/download…

  • Run elasticSearch. bat in the bin directory to start ElasticSearch

Modify startup parameters to store information to Elasticsearch

  • To save the trace to Elasticsearch, run the following command:
# STORAGE_TYPE: indicates the storage type. ES_HOSTS: indicates the access address of ESJava-jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=elasticsearch --ES_HOSTS=localhost:9200Copy the code
  • After the need to restart the user – service and ribbon – service to take effect, restart after multiple calls http://localhost:8301/user/1; ribbon – service interfaces

  • If Kibana is installed for Elasticsearch, you can see that the trace information is already stored in it:

For more startup parameters

Github.com/openzipkin/…

The module used

Springcloud - learning ├ ─ ─ eureka - server-- Eureka Registry├ ─ ─ the user - service-- a service that provides the User object CRUD interface└ ─ ─ ribbon - serviceThe Ribbon service invokes the test service
Copy the code

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.