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 implement Link tracing Sleuth by Spring Cloud

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

Share

Shulou(Shulou.com)06/01 Report--

This article is about how Spring Cloud implements link-tracking Sleuth. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

I. brief introduction

Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.

-- extracted from the official website

The main function of Spring Cloud Sleuth is to provide tracking solutions in distributed systems, and compatible support for zipkin, you only need to introduce the corresponding dependencies in the pom file.

Second, service tracking analysis

In micro-service architecture, if services are divided into services, an interface exposed through REST calls may require the cooperation of many services to complete this interface function. If there is a problem with any service on the link or the network times out, it will cause the interface call to fail. With the continuous expansion of the business, the mutual invocation between services will become more and more complex.

With more and more services, the analysis of the call chain will become more and more complex. The invocation relationship between them may be as follows:

III. Terminology

Span: basic unit of work, for example, sending a RPC in a newly created span is equivalent to sending a response request to RPC,span with a 64-bit ID unique identity, trace is represented by another 64-bit ID, span also has other data information, such as summary, timestamp events, critical value comments (tags), span's ID, and progress ID (usually IP address) span is constantly starting and stopping, while recording time information When you create a span, you have to stop it at some point in the future.

Trace: a tree structure made up of a series of spans. For example, if you are running a distributed big data project, you may need to create a trace.

Annotation: used to record the existence of an event in a timely manner, and some core annotations to define the beginning and end of a request

Cs-Client Sent-the client initiates a request, and this annotion describes the beginning of the span

Sr-Server Received-the server gets the request and is ready to start processing it. If you subtract its sr from the cs timestamp, you can get the network delay.

The ss-Server Sent-comment indicates the completion of the request processing (when the request is returned to the client). If the ss minus the sr timestamp, the request processing time required by the server can be obtained.

Cr-Client Received-indicates the end of span, and the client has successfully received a reply from the server. If cr minus the cs timestamp, you can get all the time it takes for the client to get a reply from the server. Graphically use Zipkin annotations for Span and Trace in one system:

Graphically use Zipkin annotations for Span and Trace in one system:

IV. Construction project

After explaining the basic knowledge, let's practice. The case of this paper mainly consists of three projects: a server-zipkin, which mainly uses the functions of ZipkinServer to collect and display data; a service-hi, which exposes hi interface; and a service-miya, which exposes miya interface. The two service can be called each other. And only when called, server-zipkin collects data, which is why it is called service tracking.

4.1Building server-zipkin

Build a spring-boot project named server-zipkin and introduce dependencies into its pom:

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

Add the annotation @ EnableZipkinServer to its program entry class to enable the function of ZipkinServer:

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

Specify the service port in the configuration file application.yml as:

Server.port=94114.2 creates service-hi

The introduction of spring-cloud-starter-zipkin in its pom starts with the following code:

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

The address of the zipkin server is specified in its configuration file application.yml, and the header is specified by configuring "spring.zipkin.base-url":

Server.port=8988spring.zipkin.base-url= http://localhost:9411spring.application.name=service-hi

This is done by introducing spring-cloud-starter-zipkin dependencies and setting up spring.zipkin.base-url.

External exposure interface:

@ RequestMapping ("/ hi") public String callHome () {LOG.log (Level.INFO, "calling trace service-hi"); return restTemplate.getForObject ("http://localhost:8989/miya", String.class);} @ RequestMapping (" / info ") public String info () {LOG.log (Level.INFO," calling trace service-hi ") Return "Icomm service-hi";} 4.3 create service-miya

Create process pain service-hi, introduce the same dependency, and configure spring.zipkin.base-url.

External exposure interface:

@ RequestMapping ("/ hi") public String home () {LOG.log (Level.INFO, "hi is being called"); return "hi i'm miya!";} @ RequestMapping ("/ miya") public String info () {LOG.log (Level.INFO, "info is being called") Return restTemplate.getForObject ("http://localhost:8988/info",String.class);} 4.4) starts the project to demonstrate tracking

Start the above three projects in turn, open the browser to visit: http://localhost:9411/, and the following interface will appear:

Visit: http://localhost:8989/miya, the browser appears:

Isimm service-hi

Open the http://localhost:9411/ interface and click Dependencies to discover the dependencies of the service:

Click find traces to see the data of specific services calling each other:

Thank you for reading! This is the end of the article on "how to achieve Link tracking Sleuth in Spring Cloud". 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, you can 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

Internet Technology

Wechat

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

12
Report