Spring Cloud Distributed Link Tracking Sleuth + Zipkin + Elastic Search [Finchley Edition]

Keywords: Java Spring RabbitMQ github ElasticSearch

As the business becomes more and more complex, the system will also be split. Especially with the rise of micro-service architecture, it seems that a simple application, many services in the background may be supporting; a request may require multiple services invocation; when the request is slow or unavailable, it is impossible to know which micro-service is causing, then it is necessary to solve how to locate the service failure quickly. Point, Zipkin distributed tracking system can solve this problem very well.

So how to use it? Next, we complete a specific example to experience a micro-service link tracking:

The Spring Cloud Finchley version used in this article will be different from other versions.

We use user-service,order-service as two micro-services and zuul-gateway as service gateway.

Zuul-gateway-> order-service-> user-service forms a service call link to complete a request.

Note: Zipkin no longer recommends that we come from the custom Server. Zipkin-server can't be found in the latest version of Spring Cloud dependency management. There's no need to build a new Zipkin-server service. All kinds of online tutorials are copied from each other. Please ignore it.

1. Environmental Installation

  1. I use centos 7, java-10
  2. Installing Zipkin: Aggregate Call Delay Data between Business Systems
  3. Installing RabbitMQ: System Call Data Transfer
  4. Install Elastic search: System call data persistence
  5. Install Elastic search-head: Elastic search visualization

Second, create micro services

  1. user-service
  2. Here are the pom dependency files
    <dependency>    
            <groupId>org.springframework.boot</groupId>   
            <artifactId>spring-boot-starter-web</artifactId>   
        </dependency>   
        <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-actuator</artifactId>   
        </dependency>   
        <dependency>   
            <groupId>org.springframework.cloud</groupId>   
            <artifactId>spring-cloud-starter-openfeign</artifactId>   
        </dependency>   
      <!--Service Link Tracking-->  
        <dependency>   
            <groupId>org.springframework.cloud</groupId>   
            <artifactId>spring-cloud-starter-sleuth</artifactId>   
        </dependency>  
        <dependency>   
            <groupId>org.springframework.cloud</groupId>   
            <artifactId>spring-cloud-starter-zipkin</artifactId>   
        </dependency>   
        <!--data transmission-->   
        <dependency>   
            <groupId>org.springframework.cloud</groupId>   
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>   
        </dependency>   
  1. New @RestController interface UserOrderController, code as follows:
          @RestController
          public class UserOrderController {
          
              @Autowired
              private UserOrderService orderService;
          
              @RequestMapping(value = "/getUserOrder", method = RequestMethod.GET)
              public String getUserOrder() {
                  return orderService.getOrder();
              }
          }
  Description: Use FeignClient to invoke order-service getOrder service in user-service
  1. The application.yml configuration file is as follows:
          spring:
            application:
              name: user-service
            sleuth:
              web:
                client:
                  enabled: true
              sampler:
                probability: 1.0
            zipkin:
              base-url: http://192.168.10.100:9411/
              enabled: true
              sender:
                type: RABBIT
            rabbitmq:
              addresses: 192.168.10.100
              port: 15672
              username: admin
              password: 12345
              virtual-host: sleuth
          
          server:
            port: 9100
  zipkin Description of parameters:
    probability: 1.0        #Set the sampling ratio to 1.0, that is, all of them are required. The default is 0.1
    base-url:  http://192.168.10.100:9411/address of #Zipkin server
  
  1. order-service
  2. The pom dependency file is the same as the user-service
  3. New @RestController interface OrderController, code as follows:
          @RestController
          public class OrderController {
          
              @Value("${server.port}")
              private String port;
          
              @RequestMapping(value = "/getOrder", method = RequestMethod.GET)
              public String getOrder() {
                  return "Success, Order-Service, Port :" + port;
              }
          }
  Note: The getOrder interface is called for user-service
  1. application.yml configuration file is the same as user-service

  2. zuul-gateway gateway
    1. Here are the pom dependency files
           <dependency>
                       <groupId>org.springframework.cloud</groupId>
                       <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
                   </dependency>
                   <dependency>
                       <groupId>org.springframework.cloud</groupId>
                       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                   </dependency>
                   <!--Service Link Tracking-->
                   <dependency>
                       <groupId>org.springframework.cloud</groupId>
                       <artifactId>spring-cloud-starter-sleuth</artifactId>
                   </dependency>
                   <dependency>
                       <groupId>org.springframework.cloud</groupId>
                       <artifactId>spring-cloud-starter-zipkin</artifactId>
                   </dependency>
                   <!--data transmission-->
                   <dependency>
                       <groupId>org.springframework.cloud</groupId>
                       <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
                   </dependency>
    1. The application.yml configuration file is as follows:
           spring:
             application:
               name: zuul-gateway
             sleuth:
               web:
                 client:
                   enabled: true
               sampler:
                 probability: 1.0
             zipkin:
               base-url: http://192.168.10.100:9411/
               enabled: true
               sender:
                 type: RABBIT
             rabbitmq:
               addresses: 192.168.10.100
               port: 15672
               username: admin
               password: 12345
               virtual-host: sleuth
    
           server:
             port: 9310
    
           eureka:
             client:
               service-url:
                 defaultZone: http://localhost:8080/eureka/
    
           zuul:
             prefix: /v1   
             routes:
               # http://localhost:9310/v1/user/
               # user Api
               user-api:
                 path: /user/**
                 serviceId: user-service
               # order Api
               order-api:
                 path: /order/**
                 serviceId: order-service

    zipkin configuration is the same as user-service
    zuul Routing Configuration Find its own reference, ah, here is no explanation.

    Above all, our micro services are completed, and then all start up.

    3. Start up systems and components

    The user is not recommended to create Zipkin service, so how to transfer data to Zipkin server? Zipkin can read information from RabbitMQ by using Zipkin's environment variable.
    1. Start Zipkin service and specify RabbitMQ for data transmission. Elastic search persists data. The startup command is as follows:

        java -jar zipkin.jar --RABBIT_URI=amqp://admin:12345@localhost:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=http/localhost:9200 --ES_HTTP_LOGGING=BASIC

    Explain:

RABBIT_URI=amqp://admin:12345@localhost:5672/sleuth specifies RabbitMQ for data transmission
STORAGE_TYPE=elastic search--ES_HOSTS=http/localhost:9200--ES_HTTP_LOGGING=BASIC specifies that Eelastic search is used for data transmission.
For configurable environment variables, see https://www.rabbitmq.com/uri-spec.html

Of course, you think it's too difficult to build Elastic search. You can also recommend using Elastic search in MYSQL generation environment, or if you just want to try it out for yourself, you can save the data in memory instead of storing it.
2. Start the RabbitMQ service http://192.168.10.100:15672/View the start-up, recommend yourself to create a new user, and then log in to view.
3. Start the Elastic search service, http://192.168.10.100:9200/ View ES start-up, note that Elastic search can not be started by root users, specific how to operate please Baidu tutorial.
4. Start Elastic search-head, http://192.168.10.100:9100/ You can see the interface, pay attention to the cluster health value, if not connected is a problem, solve it by yourself.
5. Start the user-service, order-service,zuul-gateway gateway and request your own defined interface. This problem can be solved by yourself.

Looking at the RabbitMQ visual interface, you can see the data transmission information. The following picture:

Looking at the Zipkin visual interface, you can see the link information for service invocation. The following picture:

Looking at the Elastic search-head visualization interface, you can see the data information stored in Elastic search. The following picture:


The last completed distributed service link tracking system is completed.

Detailed code: https://github.com/jarvisqi/spring-cloud-microservice



Reference resources:

  1. https://github.com/openzipkin/zipkin
  2. http://www.rabbitmq.com/documentation.html
  3. https://www.elastic.co/products/elasticsearch
  4. https://windmt.com/2018/04/24/spring-cloud-12-sleuth-zipkin/

Posted by tekky on Thu, 27 Dec 2018 15:57:06 -0800