In a business complex microservices architecture, it is often difficult to comb the call relationships between services, one HTTP request may involve calls to multiple services (Eg:service A, service B, service C ...), if you want to analyze the call relationships between services, And the response time of each service, find the service that has the performance bottleneck, then Zipkin comes in handy, it is a tracing system of open source of Twitter company, the official website address is: http://zipkin.io/, Spring Cloud can integrate with it undoubtedly.
Steps to use:
One, the micro-service party
1.1 adding dependent jar packages
Compile ' Org.springframework.cloud:spring-cloud-starter-bus-kafka ' compile ' org.springframework.cloud: Spring-cloud-starter-sleuth ' compile ' org.springframework.cloud:spring-cloud-sleuth-stream '
Note: In order to realize the decoupling of tracing data buried point and acquisition, Spring Cloud introduces the concept of message bus, the micro-service does not need to care about the tracing system, what it looks like, just throw the message on the bus. So the introduction of Bus-kafka and Sleuth-stream.
1.2 APPLICATION.YML Configuration
Spring: ... Cloud: bus: enabled:true stream: default-binder:kafka Kafka: Binder: Brokers: 10.0.1.2,10.0.1.3,10.0.1.4//kafaka server cluster list zknodes:10.0.1.5,10.0.1.6,10.0.1.7//ZK server cluster list defaultzkport:2181//zk Port defaultbrokerport:9092//kafka Broker Port ... Sleuth: Sampler: percentage:0.2//Sample rate 0.2 is 20%
The above 2 configuration is OK, the code without any modification, the real code 0 intrusion
Second, Zipkin-server
Zipkin received data from the Kafka, there are 4 ways to Save: In-memory (save in memory), MySQL, Cassandra, Elasticsearch
Personal development debugging, recommend using In-memory mode, other environments do not use! (Note: Because as more data is collected, it can easily cause oom in memory)
2.1 MySQL Storage
2.1.1 Main jar Package dependencies
dependencies { ... The key is the following several compile ' org.springframework.cloud:spring-cloud-starter-sleuth ' compile ' Org.springframework.cloud:spring-cloud-sleuth-zipkin-stream ' compile ' org.springframework.cloud: Spring-cloud-starter-bus-kafka ' compile ' io.zipkin.java:zipkin-server ' compile ' Io.zipkin.java: Zipkin-autoconfigure-ui ' compile ' io.zipkin.java:zipkin-autoconfigure-storage-mysql ' #mysql的存储 ... The following are the general items of Spring-boot/cloud compile ' org.springframework.boot:spring-boot-starter-actuator ' compile ' Org.springframework.boot:spring-boot-starter-web ' compile ' org.springframework.boot: Spring-boot-starter-security ' compile ' log4j:log4j:1.2.17 '//zipkin storage jar package, dependent on the lower version of log4j compile ' org.apache.logging.log4j:log4j-slf4j-impl:2.8.2 ' compile ' mysql:mysql-connector-java:6.0.5 ' }
2.1.2 application.yml Configuration
Spring: application: name:zipkin-server DataSource://Specify MySQL data source schema:classpath:/mysql.sql url:jdbc:mysql://192.168.1.2:3306/zipkin?autoreconnect=true&usessl=false username:root Password: * * * driver-class-name:com.mysql.cj.jdbc.driver initialize:true continue-on-error:true Sleuth: enabled:false Cloud: bus: enabled:true ... Stream: default-binder:kafka Kafka: Binder: brokers: ${kafka.brokers} zknodes: ${ Kafka.zknodes} defaultzkport: ${kafka.zkport} defaultbrokerport: ${kafka.brokerport}zipkin: Storage: Type:mysql//configured as MySQL storage
2.1.3 Main entry code
@SpringBootApplication (exclude = { Mybatisautoconfiguration.class, Redisautoconfiguration.class, Redisrepositoriesautoconfiguration.class}) @EnableZipkinStreamServerpublic class Zipkinserver {public static void Main (string[] args) { springapplication.run (zipkinserver.class, args);} }
Note: If your project relies on other packages such as Redis,mybatis, you can refer to the above wording, excluding these automatic configuration, otherwise, do not add that a bunch of exclude.
2.2 Cassandra
2.2.1 dependent jar Package
Note: Under Cassandra and Elasticsearch, you may encounter the dependencies panel in Zipkin without data, see the discussion on GitHub for details: https://github.com/openzipkin/ Zipkin-dependencies/issues/22
Compile ' Org.springframework.boot:spring-boot-starter-data-cassandra ' compile (' Io.zipkin.java: zipkin-autoconfigure-storage-cassandra3:1.29.3 ') { exclude group: "Com.datastax.cassandra", module: " Cassandra-driver-core " } Compile ' com.datastax.cassandra:cassandra-driver-core:3.1.1 ' compile ' com.datastax.cassandra:cassandra-driver-mapping:3.1.1 '
2.2.2 Application.yml
Spring: data: Cassandra: contact-points:localhost port:9042 keyspace-name:zipkin3 ... Zipkin: storage: type:cassandra3
2.3 Elasticsearch
2.3.1 Dependent jar Package
Compile ' io.zipkin.dependencies:zipkin-dependencies-elasticsearch:1.7.2 ' compile ' Io.zipkin.java: zipkin-autoconfigure-storage-elasticsearch-http:1.29.2 '
2.3.2 application.yml
Zipkin: storage: type:elasticsearch elasticsearch: cluster:elasticsearch hosts:http:// localhost:9200 index:zipkin index-shards:5 index-replicas:1
Spring Cloud Learning (8)-Sleuth & Zipkin call chain tracking