Spring Sleuth and Zipkin tracking microservices

Source: Internet
Author: User
Tags log log message queue rabbitmq

Original address: http://www.cnblogs.com/skyblog/p/6213683.html

As the number of microservices continues to grow, it is necessary to track the propagation of a request from one microservices to the next, and Spring Cloud sleuth solves this problem by introducing a unique ID in the log to ensure consistency between microservices invocations, This way you can track how a request is passed from one microservices to the next.

If you have experience using an AOP interception servlet, it's easy to do an AOP-based simple service statistic and tracking. But it's more difficult to follow the call chain like Zipkin. Called the call chain, is a service call B service, B service and call the C, D services. Such a chain to statistical tracking, to write a lot of code. Spring Cloud Sleuth allows you to do this without writing a single line of code.

This article covers 5 Spring boot projects:

 

The first three authentication basic sleuth used, the latter two demos if using message middleware as the source of the Zipkin, and if the Zipkin monitoring data is configured to the MySQL database. Because the former and the latter differ greatly in configuration, it is necessary to show them separately. A simple sleuth application, in this case, refers to the sleuth HTTP application, which is sleuth to push the monitoring log to the Zipkin server via an HTTP request. , Cloud-sleuth-server is actually Zipkin server, which receives monitoring log pushes from microservices Cloud-sleuth-service1 and Cloud-sleuth-service2, Cloud-sleuth-service1 called Cloud-sleuth-service2, forming a simple call chain. Where Cloud-sleuth-service1 POM configuration:

  

<Dependencies>    <Dependency>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-web</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-sleuth</Artifactid>    </Dependency>    <Dependency>       <groupId>Org.springframework.cloud</groupId>       <Artifactid>Spring-cloud-sleuth-zipkin</Artifactid>     </Dependency>                </Dependencies>

Sleuthclientapplication Applications:

@SpringBootApplication Public classsleuthclientapplication {@Bean Publicresttemplate resttemplate () {return Newresttemplate (); }      Public Static voidMain (string[] args) {Springapplication.run (sleuthclientapplication.class, args); }} @RestControllerclassHomeController {Private Static FinalLog log = Logfactory.getlog (HomeController.class); @AutowiredPrivateresttemplate resttemplate; PrivateString url= "http://localhost:9986"; @RequestMapping ("/service1")     PublicString Service1 ()throwsException {log.info ("Service1"); Thread.Sleep (200L); String s= This. Resttemplate.getforobject (url + "/service2", String.class); returns; }  }

  

application.properties:spring.application.name=sleuthservice1server.port= 9987 spring.zipkin.baseurl= http://localhost:9966spring.zipkin.enabled=true

Only need to configure the spring-cloud-sleuth- in the pom file Zipkin and configured in the configuration file Spring.zipkin.baseUrl, then Service1 can send the monitoring log to the target is BaseUrl point to the Zipkin server, is automatically called Zipkin of a rest interface to pass the monitoring log to it.

Zipkin Server's POM configuration:

   <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter</Artifactid>        </Dependency>        <Dependency>            <groupId>Io.zipkin</groupId>            <Artifactid>Zipkin-ui</Artifactid>            <Scope>Runtime</Scope>        </Dependency>        <Dependency>            <groupId>Io.zipkin.java</groupId>            <Artifactid>Zipkin-server</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-sleuth</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-sleuth-zipkin</Artifactid>    </Dependency>

Sleuthserverapplication:

@SpringBootApplication @enablezipkinserver  Public class sleuthserverapplication {          publicstaticvoid  main (string[] args) {        Springapplication.run (sleuthserverapplication. class , args);}    }

Application.properties configuration:

spring.application.name=sleuthserverserver.port= 9966

As for Service2, the configuration and Service1, just like the application, add the Servcie2 rest interface:

@SpringBootApplication Public classsleuthclientapplication { Public Static voidMain (string[] args) {Springapplication.run (sleuthclientapplication.class, args); }} @RestControllerclassHicontroller {Private Static FinalLog log = Logfactory.getlog (Hicontroller.class); @RequestMapping ("/service2")     PublicString Hi ()throwsException {log.info ("Service2"); Thread.Sleep (100L); return"Service2"; }}  

After running, Access Service1, and then follow the ports configured by Zipkin server to open the Monitoring page for Zipkin server and run the following results:

After you have opened a request, you will see a detailed call chain request information:

Click on a request in the call chain to see more detailed request information:

Shown, in turn:

Sleuthservcie1 Client Send:service1 The timestamp of the initiating request Service2.

Sleuthservcie2 Server Receive:service2 received the requested timestamp.

Sleuthservcie2 Server send:service2 finishes processing the send result timestamp.

Sleuthservcie1 Client Receive:service1 The timestamp of the results received.

At the top of the graph is the total time for this request, and the processing request time at a glance at each session.

In the case of a production environment, a second set of scenarios is generally required. is to send the logs to the message queue before being accepted by Zipkin, and Zipkin to save the log to the database after receiving it. The specific configuration is also very simple, it is important to note that the spring official example can not find the specific configuration of the message middleware, may be somewhere there is a default, I also configured a bit:

spring.sleuth.sampler.percentage=1. 0 spring.rabbitmq.host= 127. 0. 0. 1 spring.rabbitmq.port= 5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtualhost=/

Message Queuing only needs to be configured to automatically create the queue. The same is true of database configuration, which is also available in the official example, and is automatically built on configuration.

Spring.datasource.schema=classpath:/mysql.sql spring.datasource.url=jdbc:mysql://${  mysql_host:localhost}:3306/test  spring.datasource.username=root  spring.datasource.password=123  spring.datasource.initialize=true Spring.datasource.continueonerror=true Spring.sleuth.enabled=false Zipkin.storage.type=mysql

Specific reference source code, the source in the old place: Https://git.oschina.net/zhou666/spring-cloud-7simple

Spring Sleuth and Zipkin tracking microservices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.