Zipkin is an open source project of Twitter. It is a distributed tracking system dedicated to collecting monitoring data from all services. It provides two interfaces for collecting data and querying data. With Zipkin, we can see the call chain visually, and it’s probably easy to see how services are called and how long it takes to call them.
Create a Zipkin data collection service
First we need to create a Zipkin project that integrates Zipkin’s UI for data presentation and collection. The POM.xml configuration is as follows:
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autocongure-ui</artifactId>
</dependency>
Copy the code
Create a startup class:
@SpringBootApplication
@EnableZipkinServer
public class zipKinServerApplication {
public static void main(String[] args){ SpringApplication.run(ZipKinServerApplication.class, args); }}Copy the code
Adding configuration information:
spring.application.name = fangjia-zipkin
server.port=9411
Copy the code
At this point, the Zipkin service is created. Starts to http://localhost:9411/zipkin/ you can see the management page, as shown in the figure below.
Ii. Project integration Zipkin sends call chain data
In the previous article, we simply integrated Spring Cloud Sleuth and then output the trace information to the log. Now that the Zipkin service is created, the link-tracking information needs to be sent to the Zipkin collection service.
You need to add the following dependencies to your project:
<dependency>
<groupId>org.springframework.cloud</ groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
Copy the code
In the properties file, you can configure the Zipkin address, which defaults to http://127.0.1:9411, to send traced data to the collection service that executes.
Spring.zipkin.base -url = HTTP:/ / 127.0.0.1:9411
Copy the code
Then we start the previous service, access interface, and see that the data is already available in the Zipkin Web page, as shown in the figure below.
3. Sampling and collecting data
In real use, the interface might be called 10 times, but there is only one piece of data in Zipkin, because there is a percentage of information that is collected, and this is not a bug. The default ratio of the number of bytes in Zipkin to the number of times the interface is called is 0.1, which can be modified by configuration:
# zipkin sampling proportion spring. Sleuth. Sampler. The percentage =1
Copy the code
The reason for this configuration is that in high concurrency, if all data is collected, the amount of data is too large. Sampling can reduce part of the data, especially for Http data sending, which has a significant impact on performance.
Send call chain data using RabbitMQ instead of Http
Although there are sample-based collections, sending data over Http has a performance impact. If the Zipkin server restarts or dies, some of the collected data will be lost. To solve these problems, we will integrate Spring-cloud-sleuth-Zipkin-stream, use RabbitMQ to send collected data, and use message queues to improve transmission performance and ensure data loss.
First, revamp our Zipkin project to increase steam dependency:
<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>
Copy the code
Then add the @enableZipkInstreamServer annotation to the startup class, removing the @enableZipkinServer annotation. Add RabbitMQ connection configuration to properties file:
# the rabbitmq configuration spring. The rabbitmq. Addresses = closer:/ / 192.168.10.47:5672
spring.rabbitmq.username=yinjihuan
spring.rabbitmq.password=123456
Copy the code
The next step is to change the service to include RabbitMQ dependencies and replace Http with RabbitMQ:
<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>
Copy the code
Add RabbitMQ connection configuration to the properties file:
spring.rabbitmq.addresses=amqp:/ / 192.168.10.47:5672
spring.rabbitmq.username=yinj ihuan
spring.rabbitmq.password=123456
Copy the code
The rabbitmq configuration
At this point the integration is complete, remember to remove the spring.zipkin.base-URL configuration. Since we now use RabbitMQ to send data, this configuration is not required.
Use Elasticsearch to store link data
At present, all the data we collected are stored in the memory of Zipkin service. Once the service is restarted, these data will be lost. We need to persist these data. Elasticsearch can be used to store data in MySQL, so it is not a good choice to use Elasticsearch.
Update our Zipkin project to add Elasticsearch storage dependency:
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>
zipkin-autoconfigure-storage-elasticsearch-http
</artifactId>
<version>1.24. 0</version>
<optional>true</optional>
</dependency>
Copy the code
Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch
zipkin.storage.StorageComponent=elasticsearch
zipkin.storage.type=elasticsearch
zipkin.storage.elasticsearch.cluster=elasticsearch-zipkin-cluster
zipkin.storage.elasticsearch.hosts=127.0. 01.:9300
zipkin.storage.elasticsearch.max-requests=64
zipkin.storage.elasticsearch.index=zipkin
zipkin.storage.elasticsearch.index-shards=5
zipkin.storage.elasticsearch.index-replicas=1
Copy the code
Restart the service and collect some data. There are two ways to verify that the data is stored in Elasticsearch.
- You can restart the Zipkin service and see if the data still exists, and if it does, the data is persisted.
- You can do this by looking atElasticsearchTo confirm whether the data has been stored successfully, accessElasticsearchQuery all current index information:http://localhost:9200/_cat/indices. If the index starts with zipkin, it indicates that the index has been created. Then directly query whether there is data under the index to verify whether the storage is successful and accesshttp://localhost:9200/Index name /search will do.