Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Zipkin with Sleuth in Spring Cloud

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how Sleuth integrates Zipkin in Spring Cloud. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Brief introduction of Sleuth Integration ZipkinZipkin

Zipkin is a distributed tracking system, which is mainly used to collect and manage the data generated by microservices. The design of Zipkin is based on Google Dapper. In practical application, we need to have each micro-service report process data to the Zipkin server. For Spring Cloud, several modules have been provided for data reporting, and we only need to add dependencies and do simple configuration to "write" data to Zipkin.

After obtaining these data, Zipkin provides the functions of data query and analysis. These graphical functions can make us clear at a glance about the micro-service call process, processing time, dependency and other data.

Build the Zipkin server project

To start the Zipkin server, you can choose to use the jar package, or you can embed the Zipkin server into the Maven project.

To start using the jar package, you need to download Zipkin's startup jar package first. Readers can download it from Zipkin's official website at http://zipkin.io/, or you can download zipkin-server-1.31.1-exec.jar directly from the soft directory of this book. After getting the jar package, use the "java-jar" command to start the Zipkin server. After successful startup, port 9411 is occupied by default.

We can also embed the Zipkin server in the Maven project and build the Zipkin server directly with Sping Boot for simplicity. Create a new zk-server project (code directory is codes\ 10\ zk-server). The dependencies used by the project are shown in listing 10-1.

Listing 10-1:codes\ 10\ zk-server\ pom.xml

Io.zipkin.java zipkin-server io.zipkin.java zipkin-autoconfigure-ui runtime

In application.xml, configure the startup port to 9411, and listing 10-2 is the startup class.

Listing 10-2:codes\ 10\ zk-server\ src\ main\ java\ org\ crazyit\ cloud\ ZkServerApp.java

@ SpringBootApplication@EnableZipkinServerpublic class ZkServerApp {public static void main (String [] args) {SpringApplication.run (ZkServerApp.class, args);}}

Add the @ EnableZipkinServer annotation to the startup class, run the startup class, visit: http://localhost:9411, and you can see the main interface of Zipkin, as shown in figure 10-3.

Figure 10-3 Zipkin main interface

Configure microservices

Next, you need to configure the various microservices to write data to the Zipkin server. The case of this chapter mainly has three micro-service modules: books, payment and sales. Several modules provide the following services:

Book module (test-book-service): provides a book query service at "/ book/ {bookId}" and returns a Book object.

Payment module (test-pay-service): provides payment service with the address "/ pay", does not return, and only makes a simple console output.

Sales module (test-sale-service): provides sales services at the address "/ sale/ {bookId}". The interface between the book module and the payment module is called. The implementation of the sales service is shown in listing 10-3.

Listing 10-3:

Codes\ 10\ test-sale-service\ src\ main\ java\ org\ crazyit\ cloud\ SaleApplication.java

@ RequestMapping (method = RequestMethod.GET, value = "/ sale/ {bookId}") public String sale (@ PathVariable ("bookId") Integer bookId) {System.out.println ("sales module handles sales"); / / find book Book book = bookService.getBook (bookId); / / pay payService.doPay (new BigDecimal (10)) Return "sold successfully, title:" + book.getName () + ", author:" + book.getAuthor ();}

The sales module calls the interface of the book and payment module, using the Feign framework. Readers can refer to the relevant chapters of this book for the use of the framework, which will not be repeated here.

After the micro service is implemented, add three modules to add the following dependencies:

Org.springframework.cloud spring-cloud-starter-zipkin org.springframework.cloud spring-cloud-sleuth-zipkin

Next, configure the Zipkin server for each module, and the configuration of application.yml is as follows:

Spring: zipkin: base-url: http://localhost:9411 sleuth: sampler: percentage: 1.0

In application.yml, the server that uses spirng.zipkin.base-url to configure Zipkin, and spring.zipkin.sleuth.sampler.percentage to configure the sampling percentage of span data, the default is 0.1, that is, about 10% of the span data is sent to Zipkin. In this example, in order to see the effect, directly configure to 1, that is, all the span data will be sent to Zipkin. In a production environment, it is recommended to sample according to specific requirements so as not to increase the load on the server.

In order to see the output of Sleuth in the console of each microservice, you also need to configure the log level for three microservices and add the following configuration to the application.yml:

Logging: level: root: INFO org.springframework.cloud.sleuth: DEBUG this article on "how to integrate Sleuth in Spring Cloud" ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report