Spring Cloud Sleuth

Source: Internet
Author: User
Spring Cloud Sleuth 1.spring-cloud-sleuth: One of the components of spring cloud that implements a distributed tracking solution for Springcloud applications that is compatible with Zipkin, Htrace and log-based Tracking 2.Span: basic unit of work, for example, sending an RPC in a new Span equivalent to sending a response request to rpc,span through a 64-bit ID Unique identifier, trace with another 64-bit ID, Span also has other data information, such as summaries, timestamp events, key value Comments (tags), span IDs, and progress IDs (usually IP addresses) that span keeps starting and stopping, while recording time information, and when you create a span, you must stop it at some point in the future. 3.Trace: A tree-like structure consisting of a series of spans, for example, if you are running a distributed Big Data project, you may need to create a Trace. 4.Annotation: Used to record the existence of an event in a timely manner, some core annotations used to define the start and end of a request 1) CS (client Sent): Clients initiate a request, this annotion describes the beginning of this span 2) SR ( Server Received): The service side gets the request and is ready to start processing it, if its SR minus CS Timestamp can get network delay 3) SS (server Sent): Note Indicates the completion of request processing (when the request returns to the client), If the SS minus the SR timestamp is available for processing request time required by the server 4) CR (client Received): Indicates the end of span, clients successfully received a reply from the server, if the CR minus CS timestamp can get the client to get reply from the server all the time required
Second, example: HTTP communication mode, MQ asynchronous Communication +mysql mode, MQ asynchronous communication + Elasticsearch mode three projects: Server-monitor monitoring (responsible for data collection and information display function), Consumer-beehive service Consumer, Provider-user service provider 1.http method: Save the data in memory, but the production environment still needs to persist data in 1) server-monitor:a.pom.xml dependency:
<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId> spring-boot-starter</artifactid>
	</dependency>
	<dependency>
    		<groupId> io.zipkin.java</groupid>
    		<artifactId>zipkin-server</artifactId>
	</dependency>
	<dependency>
    		<groupId>io.zipkin.java</groupId>
    		<artifactId> zipkin-autoconfigure-ui</artifactid>
   		<scope>runtime</scope>
	</dependency>
	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId> spring-boot-starter-test</artifactid>
    		<scope>test</scope>
	</dependency>

B.**application.java:
	Import org.springframework.boot.SpringApplication;
	Import org.springframework.boot.autoconfigure.SpringBootApplication;
	Import Zipkin.server.EnableZipkinServer;

	Replace the public  class with Enablezipkinstreamserver when @SpringBootApplication @EnableZipkinServer//change HTTP to MQ
	servermonitorapplication {public
    		static void Main (string[] args) {
        		Springapplication.run ( Servermonitorapplication.class,args);
    		}
	}
C.APPLICATION.YML:
	Server:
  	  8050
	Spring:
  	  application:
Server-monitor output: ANSI:    	    
  	  
 
       	      always
	encrypt:
false  	  
D.BOOTSTAP.YML:
    Spring:
  	  zipkin:
http://10.83.3.12:8050 Eureka:    	    

   	  client:
    	    serviceurl:
http://10.83.3.1:8761/eureka/      	       , http://10.83.3.2:8762/eureka/,http://10.83.3.3:8763/eureka/
  	  instance:
     	     True     #指定自身服务的显示ip形式
    	     ${spring.cloud.client.ipAddress}
    	    ${  Spring.cloud.client.ipAddress}:${server.port}

2) Provider-user and consumer-beehive configuration a.pom.xml:
	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId> spring-cloud-sleuth-zipkin</artifactid>
	</dependency>
	<dependency>
    		<groupid >org.springframework.cloud</groupId>
    		<artifactid>spring-cloud-starter-sleuth</artifactid >
	</dependency>
B.APPLICATION.YML:
	Server:
  	  8001
	Spring:
  	  application:
     	     provider-user #consumer-beehive project: Consumer-beehive
  	  Output:
     	    ANSI:
always      	      
C.BOOTSTRAP.YML:
    Spring:
    	  zipkin:
http://10.83.3.12:8050    	    
  Eureka:   Client:   serviceurl:  
  	  Defaultzone:  http://10.83.3.1:8761/eureka/,http://10.83.3.2:8762/eureka/,http://10.83.3.3:8763/eureka/  instance:   prefer-ip-address:  true #指定自身服务的显示ip形式  ip-address:  ${ Sprin G.cloud.client.ipaddress }  Instance-id:  ${ Spring.cloud.client.ipaddress}:${ Server.port } 
3) Measurement: A. Access: http://localhost:8050 just started service name may not be selectable, stating that there is no data currently, we can see the database without any data B. Access Consumer-beehive, call the Provider-user method: B1. HTTP://10.83.29.110:8020/CONSUMERBEEHIVE/1 B2. When you refresh the request in a, the service name selects either Consumer-beehive or Proivder-user B3. Point find Traces button, you can see the link of the call and the time-consuming situation b4. Point dependencies, see call Relationship 4) suboptimal: A.spring-cloud-sleuth collects the information to have a certain ratio, the default sample rate is 0.1, the way to configure this value to add the spring.sleuth.sampler.percentage parameter configuration in the configuration file (if you do not configure the default 0.1), if we increase this value to 1, you can To see the information collected is more timely. But for the same request two times time-consuming information is very different, spring-cloud-sleuth this tracing service call link will have a certain impact on our business program performance The B.zipkin client sends data to the Zipkin-server program using HTTP communication, which involves the connection and the sending process each time it is sent. C. When our Zipkin-server program is shut down or restarted, the way the client collects the information sent by HTTP will be lost. 5) The improved approach is a. Communication using sockets or other more efficient means of communication. B. Sending of client data minimizes the time consumed by the business thread, and sends the collected information in an asynchronous manner. C. The middleware for adding cache classes between client and Zipkin-server, such as Redis, MQ, etc., during the Zipkin-server program hangs or restarts, the client can still send the information it collects.
2.Mq Asynchronous Communication +mysql mode: 1) server-monitor:a.pom.xml A1. Remove Io.zipkin.java:zipkin-server A2. Adding Spring-cloud-sleuth-zipkin-stream and Spring-cloud-starter-stream-rabbit
	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId> spring-cloud-sleuth-zipkin-stream</artifactid>
	</dependency>
	<dependency>
    		< Groupid>org.springframework.cloud</groupid>
    		<artifactid>spring-cloud-starter-stream-rabbit </artifactId>
	</dependency>

B.**application.java B1. Remove the @EnableZipkinServer annotation b2. Add @enablezipkinstreamserver Annotations C.bootstrap.yml
Spring:Zipkin:Base-url:http://10.83.3.12:8050Sleuth:Enabled:False #表示当前程序不使用sleuthDataSource:#数据库脚本创建地址, when there are multiple elements that can be used [x] to represent the collection, the spring boot data source is configuredSchema:Classpath:/zipkin.sqlDriver-class-name:Com.mysql.jdbc.DriverURL:jdbc:mysql://10.83.3.16:3306/test?autoreconnect=true&useunicode=true&characterencoding=utf-8& Zerodatetimebehavior=converttonull&usessl=falseUsername:RootPassword:RootInitialize:TrueContinue-on-error:TrueRABBITMQ:#rabbitmq配置Host:10.83.3.0Port:5672Username:GuestPassword:GuestEureka:Client:serviceurl:Defaultzone:http://10.83.3.1:8761/eureka/,http://10.83.3.2:8762/eureka/,http://10.83.3.3:8763/eureka/instance:prefer-ip-address:True #指定自身服务的显示ip形式ip-address:${spring.cloud.client.ipAddress}Instance-id:${spring.cloud.client.ipAddress}:${Server.port} #zipkin数据保存到数据库中需要进行如下配置Zipkin:Storage:Type:MySQL #表示zipkin数据存储方式是mysql

D.sql file: Added under resource: Zipkin.sql file. Note: specific zipkin.sql can be searched for official.
2) Provider-user and Consumer-beehive configuration a.pom.xml A1. Remove Spring-cloud-starter-zipkin A2. Adding Spring-cloud-sleuth-zipkin-stream and Spring-cloud-starter-stream-rabbit
    <dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId> spring-cloud-sleuth-zipkin-stream</artifactid>
	</dependency>
	<dependency>
    		< Groupid>org.springframework.cloud</groupid>
    		<artifactid>spring-cloud-starter-stream-rabbit </artifactId>
	</dependency>

B.BOOTSTRAP.YML: Adding MQ Configuration
	#rabbitmq配置
  	  
  	  10.83.3.0
  	  5672
  	  guestGuest

3.Mq Asynchronous Communication + Elasticsearch mode: 1) server-monitor:a.pom.xml A1. Remove Mqsql Related configuration A2. Add Elasticsearch Configuration
	<dependency>
    		<groupId>io.zipkin.java</groupId>
    		<artifactid>zipkin</ artifactid>
    		<version>1.24.0</version>
	</dependency>

	<!--saving to a database requires the following dependencies--
	<!--Adding spring-data-elasticsearch dependencies--
	<dependency>
    		<groupid>io.zipkin.java</ Groupid>
    		<artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>

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.