Spring Cloud Sleuth is a Log collection toolkit that encapsulates dapper and log-based tracing as well as Zipkin and htrace operations, enabling a distributed tracking solution for Springcloud applications.
First, let's get a few ideas straight.
Zipkin: An open source project on Twitter that allows developers to collect monitoring data on Twitter's services and provide a query interface
The system allows developers to easily collect and analyze data through a Web front-end, such as the processing time of each request service, to easily monitor bottlenecks in the system.
Spring cloud Sleuth with Zipkin, Sleuth integrates Zipkin.
Zipkin is stored in a variety of ways, by default it is stored in memory, but it can not persist, the container restarts and other conditions may result in data loss, in this project, the use of the saved to the MySQL database.
There are two ways to store data in Zipkin and sleuth integration. One is through HTTP, one is through the binding stream, this item is RABBITMQ by binding the message flow.
1, the establishment of Sleuth-server
1. Pom Introduction
<dependency> <groupId>org.springframework.cloud</groupId> <ARTIFACTID&G t;spring-cloud-sleuth-zipkin-stream</artifactid> </dependency> <dependency> < ;groupid>org.springframework.cloud</groupid> <ARTIFACTID>SPRING-CLOUD-STARTER-SLEUTH</ARTIFAC tid> </dependency> <dependency> <groupid>org.springframework.cloud</group Id> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactid>zipkin-autoconfi gure-ui</artifactid> </dependency> <dependency> <GROUPID>IO.ZIPKIN.JAVA&L t;/groupid> <artifactId>zipkin-server</artifactId> </dependency> <depende Ncy> <gRoupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> ; </dependency> <dependency> <groupId>mysql</groupId> <artifactid> ;mysql-connector-java</artifactid> <optional>true</optional> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq</artifactId> <version>3.8.2</version> </dependency>
The first 3 reasons for introduction can be consulted: Https://github.com/spring-cloud/spring-cloud-sleuth
Jooq the introduction of the specified version number, a can ' t load appears in the process. The error. The answers given after the query.
2. Configuration files
Server:port:9411Spring:application:name:zipkin-Server Datasource:schema:classpath:/mysql.sql Url:jdbc:mysql://172.18.218.252:23306/zipkin?autoreconnect=true&usessl=falsedriver-class-name:com.mysql.jdbc.Driver Username: * * * Password: * * * * Initialize:true Continue-on-error:truesleuth:enabled:falseRabbitmq:port:5672HOST:RABBITMQ Username: * * * * Password: ****zipkin:storage:type:mysql
3. class files
@SpringBootApplication @enablezipkinserver Public class sleuthmqsqlapplication { publicstaticvoid main (string[] args) { Springapplication.run (sleuthmqsqlapplication. class , args);} }
Attached Mysql.sql
Create mysql.sql files in the resource directory, insert the contents
CREATE TABLE IF not EXISTS zipkin_spans (' Trace_id_high ' BIGINT not NULL DEFAULT0COMMENT'If Non Zero, this means the trace uses-bit traceids instead of', ' trace_id ' BIGINT not NULL, ' ID ' BIGINT not NULL, ' name ' VARCHAR (255) not NULL, ' parent_id ' BIGINT, ' Debug ' BIT (1), ' Start_ts ' BIGINT COMMENT'Span.timestamp (): Epoch Micros used for Endts query and to implement TTL', ' duration ' BIGINT COMMENT'span.duration (): Micros used for minduration and maxduration query') ENGINE=innodb row_format=compressed CHARACTER set=UTF8 COLLATE utf8_general_ci; ALTER TABLE Zipkin_spans ADD UNIQUE KEY (' Trace_id_high ', ' trace_id ', ' id ') COMMENT'Ignore insert on duplicate'; ALTER TABLE Zipkin_spans ADD INDEX (' Trace_id_high ', ' trace_id ', ' id ') COMMENT'For joining with Zipkin_annotations'; ALTER TABLE Zipkin_spans ADD INDEX (' Trace_id_high ', ' trace_id ') COMMENT'For Gettracesbyids'; ALTER TABLE Zipkin_spans ADD INDEX (' name ') COMMENT'For gettraces and Getspannames'; ALTER TABLE Zipkin_spans ADD INDEX (' start_ts ') COMMENT'For gettraces ordering and range'; CREATE TABLE IF not EXISTS zipkin_annotations (' Trace_id_high ' BIGINT not NULL DEFAULT0COMMENT'If Non Zero, this means the trace uses-bit traceids instead of', ' trace_id ' BIGINT not NULL COMMENT'coincides with zipkin_spans.trace_id', ' span_id ' BIGINT not NULL COMMENT'coincides with Zipkin_spans.id', ' A_key ' VARCHAR (255) Not NULL COMMENT'Binaryannotation.key or annotation.value if type = =-1', ' A_value ' BLOB COMMENT'Binaryannotation.value (), which must be smaller than 64KB', ' A_type ' INT not NULL COMMENT'Binaryannotation.type () or-1 if Annotation', ' A_timestamp ' BIGINT COMMENT'used to implement TTL; Annotation.timestamp or Zipkin_spans.timestamp', ' Endpoint_ipv4 ' INT COMMENT'NULL when binary/annotation.endpoint is null', ' Endpoint_ipv6 ' BINARY ( -) COMMENT'NULL when Binary/annotation.endpoint is Null, or no IPv6 address', ' Endpoint_port ' SMALLINT COMMENT'NULL when binary/annotation.endpoint is null', ' Endpoint_service_name ' VARCHAR (255) COMMENT'NULL when binary/annotation.endpoint is null') ENGINE=innodb row_format=compressed CHARACTER set=UTF8 COLLATE utf8_general_ci; ALTER TABLE zipkin_annotations ADD UNIQUE KEY (' Trace_id_high ', ' trace_id ', ' span_id ', ' a_key ', ' A_timestamp ') COMMENT 'Ignore Insert on duplicate'; ALTER TABLE zipkin_annotations ADD INDEX (' Trace_id_high ', ' trace_id ', ' span_id ') COMMENT'For joining with Zipkin_spans'; ALTER TABLE zipkin_annotations ADD INDEX (' Trace_id_high ', ' trace_id ') COMMENT'For Gettraces/byids'; ALTER TABLE zipkin_annotations ADD INDEX (' endpoint_service_name ') COMMENT'For gettraces and Getservicenames'; ALTER TABLE zipkin_annotations ADD INDEX (' A_type ') COMMENT'For gettraces'; ALTER TABLE zipkin_annotations ADD INDEX (' A_key ') COMMENT'For gettraces'; ALTER TABLE zipkin_annotations ADD INDEX (' trace_id ', ' span_id ', ' A_key ') COMMENT'For dependencies Job'; CREATE TABLE IF not EXISTS zipkin_dependencies ("Day" DATE not NULL, ' parent ' VARCHAR (255) not NULL, ' child ' VARCHAR (255) not NULL, ' Call_count ' BIGINT) ENGINE=innodb row_format=compressed CHARACTER set=UTF8 COLLATE utf8_general_ci; ALTER TABLE zipkin_dependencies ADD UNIQUE KEY (' Day ', ' parent ', ' child ');
2, the Client
1, Pom file introduction
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-sleuth-stream</artifactid> </dependency> <dependency> <groupid >org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-sleuth</artifactid > </dependency> <dependency> <groupid>org.springframework.cloud</groupid > <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
Binding Rabbit Message Flow
2. Configuration files
Server: 9988 Spring: application: name:clientdemo rabbitmq: host:rabbitmq 5672 **** ****
Main Configuration Rabbit Connection information
3. class files
Public classclientapplication { private static Logger Logger = Loggerfactory.getlogger (clientapplication. Class ); @Autowired Servicedemoimpl Service; @RequestMapping ("/service") PublicString GetService () { logger.info ( "I am a client message,i ' m calling service"); returnService.getservice (); } @RequestMapping ("/") PublicString getgreed () {return "Client Success"; } Public Static voidMain (string[] args) {Springapplication.run (clientapplication.class, args); } @RequestMapping ("/ IP") PublicString GetIP () {returnService.getip (); }}
Spring Cloud Sleuth