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 use Zipkin in Dubbo to implement distributed tracking

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

Share

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

This article mainly introduces "how to use Zipkin in Dubbo to achieve distributed tracking". In daily operation, I believe many people have doubts about how to use Zipkin in Dubbo to achieve distributed tracking. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use Zipkin to achieve distributed tracking in Dubbo". Next, please follow the editor to study!

Introduction to Zipkin

Zipkin is a distributed tracking system based on Dapper paper and open source by Twitter. It collects the information of distributed service execution time to trace the service call link and analyze the service execution delay.

Zipkin architecture

Cdn.nlark.com/lark/0/2018/png/13615/1539327563392-b01ee384-c79f-48b9-9027-4bc10a4397f7.png ">

Collector collector, Storage storage, API, UI user interface and other parts constitute the Zipkin Server part, corresponding to the openzipkin/zipkin project on GitHub. The component that collects the time-consuming information called in the application and reports it is symbiotic with the application, and has the implementation version of each language, in which the implementation of Java is openzipkin/brave on GitHub. In addition to the Java client implementation, openzipkin provides implementations in many other languages, including go, php, JavaScript,. Net, ruby, and so on. For a specific list, see Zipkin's Exiting instrumentations.

The working process of Zipkin

When a user initiates a call, the client of the Zipkin generates a globally unique trace id for the entire call link at the entrance and a span id for each distributed call in the link. There can be a parent-child nesting relationship between span and span, which represents the upstream and downstream relationship in distributed calls. The relationship between span and span can be sibling, representing the two child calls under the current call. A trace consists of a set of span, which can be regarded as a tree with trace as the root node and span as several child nodes.

Span is separated by call boundaries, which in Zipkin are represented by the following four annotation:

The cs-Clent Sent client sent a request

The sr-Server Receive server receives the request

The ss-Server Send server finishes processing and sends a response to the client

The cr-Client Receive client receives the result

Obviously, from the timestamps on these four annotation, it is easy to know the time-consuming of a complete call at different stages, such as:

Sr-cs represents the time spent on the request on the network

Ss-sr represents the time it takes for the server to process the request

Cr-ss represents the time spent on the network to respond.

Cr-cs represents the overall time spent on a call

Zipkin passes trace-related information on the call link and asynchronously reports the time-consuming information of the current call to Zipkin Server at the end of each call boundary. After receiving trace information, Zipkin Server stores it. The storage types supported by Zipkin are inMemory, MySql, Cassandra, and ElasticsSearch. Then the Web UI of Zipkin will extract and display the trace information from the storage through API access, as shown in the following figure:

Use in Dubbo

Because Brave has actively supported Dubbo, it is very easy to integrate Zipkin-based link tracking into Dubbo. The following instructions follow the guidelines on Dubbo RPC support in Brave to explain how to use Zipkin in Dubbo.

Install Zipkin Server

Follow the quick start in the Zipkin official documentation to install Zipkin, as follows:

$curl-sSL https://zipkin.io/quickstart.sh | bash-s $java-jar zipkin.jar

The storage type used by Zipkin Server installed in this way is inMemory. When the server is down, all collected trace information will be lost and is not applicable to the production system. If you use it in a production system, you need to configure another storage type. Zipkin supports MySql, Cassandra, and ElasticSearch. Cassandra and ElasticSearch are recommended. For related configurations, please refer to the official documentation.

For demonstration purposes, the storage used in this article is of the inMemory type. After starting successfully, you can see the following prompt on the terminal:

$java-jar zipkin.jarPicked up JAVA_TOOL_OPTIONS:-Djava.awt.headless=true * * * * * * * * * * * *:: Powered by Spring Boot:: (v2.0.5.RELEASE)... o.s.b.w.e.u.UndertowServletWebServer: Undertow started on port (s) 9411 (http) with context path''2018-10-10 18 40 purse 31.605 INFO 21072-[main] z.s.ZipkinServer: Started ZipkinServer in 6.835 seconds (JVM running for 8.35)

Then access the http://localhost:9411 authentication WEB interface in the browser.

Configure Maven dependency to introduce Brave dependency

Create a new Java project and introduce Brave-related dependencies into pom.xml as follows:

5.4.2 2.7.9 io.zipkin.brave brave-bom ${brave.version} pom import io.zipkin. Reporter2 zipkin-reporter-bom ${zipkin-reporter.version} pom import io.zipkin.brave brave-instrumentation-dubbo-rpc io.zipkin.brave brave-spring- Beans io.zipkin.brave brave-context-slf4j io.zipkin.reporter2 zipkin-sender-okhttp3

Where:

Introduce brave-instrumentation-dubbo-rpc,brave support for dubbo: https://github.com/openzipkin/brave/blob/master/instrumentation/dubbo-rpc/README.md

Introduce brave-spring-beans,brave support for spring bean: https://github.com/openzipkin/brave/blob/master/spring-beans/README.md

By introducing brave-context-slf4j,brave 's support for SLF4J, you can use traceId and spanId: https://github.com/openzipkin/brave/blob/master/context/slf4j/README.md in MDC

Introduce zipkin-sender-okhttp3 and use okhttp3 to report data: https://github.com/openzipkin/zipkin-reporter-java

Introduce Dubbo related dependencies

The Dubbo-related dependencies are Dubbo itself and the Zookeeper client. In the following example, we will use a separate Zookeeper Server for service discovery.

Org.apache.curator curator-framework 2.12.0 io.netty netty com.alibaba dubbo 2.6.2

Where:

Dubbo relies on independent Zookeeper Server for service discovery, and the client used here is Curator

Introducing the dependency of the Dubbo framework, in principle, any version of 2.6 works, and version 2.6.2 is used here.

Realize

The scenario we construct here is a service dependency chain with two nodes, that is, when a Dubbo client invokes service A, service A will continue to invoke service B. In this example, service An is greeting service, and the downstream service service B it depends on is hello service.

Define service interface

To do this, you need to define two service interfaces, GreetingService and HelloService.

Com.alibaba.dubbo.samples.api.GreetingService

Package com.alibaba.dubbo.samples.api;public interface GreetingService {String greeting (String message);}

Com.alibaba.dubbo.samples.api.HelloService

Package com.alibaba.dubbo.samples.api;public interface HelloService {String hello (String message);} implements the service interface

In order to differentiate treatment, all HelloService-related implementation code is placed under the hello subpackage, while GreetingService-related implementation code is placed under the greeting subpackage.

Implement com.alibaba.dubbo.samples.api.HelloService

Package com.alibaba.dubbo.samples.service.hello;import com.alibaba.dubbo.samples.api.HelloService;import java.util.Random;public class HelloServiceImpl implements HelloService {@ Override public String hello (String message) {try {/ / simulate business logic processing time through sleep Thread.sleep (new Random (System.currentTimeMillis ()) .nextInt (1000)) } catch (InterruptedException e) {/ / no op} return "hello," + message;}}

Implement com.alibaba.dubbo.samples.api.GreetingService

Package com.alibaba.dubbo.samples.service.greeting;import com.alibaba.dubbo.samples.api.GreetingService;import com.alibaba.dubbo.samples.api.HelloService;import java.util.Random;public class GreetingServiceImpl implements GreetingService {/ / downstream depends on the service, and the service proxy private HelloService helloService; public void setHelloService (HelloService helloService) {this.helloService = helloService that is injected into HelloService by the spring container at run time } @ Override public String greeting (String message) {try {/ / simulate business logic processing time through sleep Thread.sleep (new Random (System.currentTimeMillis ()) .nextInt (1000));} catch (InterruptedException e) {/ / no op} return "greeting," + helloService.hello (message);}}

It is important to note that a member variable of type HelloService is declared in the implementation of GreetingServiceImpl, and in the greeting method, the hello method on HelloService is called after executing its own logic. The implementation of helloService here will be injected externally in the running state, not the implementation of HelloServiceImpl, but the remote calling agent of HelloService. In this way, the goal of continuing to invoke another remote Dubbo service in one Dubbo service is accomplished. From the point of view of link tracing, the client call GreetingService is a span,GreetingService call HelloService is another span, and both have a parent-child relationship, both belong to the same trace, that is, belong to the same calling link.

In addition, in the implementation of GreetingServiceImpl and HelloServiceImpl, Thread.sleep is used to simulate the time-consuming of processing business logic in order to better demonstrate on Zipkin UI.

Configuration

In order to focus on showing how to use Zipkin, this article does not use more advanced technologies in the configuration and programming model, but uses the most traditional Spring XML configuration to help readers understand. At a more advanced level, through annotation or even spring boot, readers can consult Dubbo and Zipkin-related documents on their own.

Expose HelloService services

Add the following configuration to resouces/spring/hello-service.xml to expose HelloServiceImpl as a Dubbo service:

The locally initiated Zookeeper Server is used as the registry with the default address of zookeeper://127.0.0.1:2181

Expose services on port 20880 with Dubbo native services

Register HelloServiceImpl as the Spring Bean that id is helloService, so that you can reference this implementation class later

By exposing HelloServiceImpl as a Dubbo service

Add Zipkin-related configuration

Add Zipkin-related configurations to resources/spring/hello-service.xml:

Modify the configuration of dubbo service exposure and add the tracing filter of Zipkin to the filter chain of Dubbo

Configure sender for Zipkin and spring bean for tracing according to https://github.com/openzipkin/brave/blob/master/spring-beans/README.md

Add startup class for HelloService

In com.alibaba.dubbo.samples.service.hello.Application, initialize a spring context and start it by reading the spring/hello-service.xml just configured by ClassPathXmlApplicationContext

Package com.alibaba.dubbo.samples.service.hello;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;public class Application {public static void main (String [] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("spring/hello-service.xml"); context.start (); System.out.println ("Hello service started"); / / press any key to exit System.in.read ();}}

Expose GreetingService services and use Zipkin

Configure GreetingService in resources/spring/greeting-service.xml. The relevant steps are similar to HelloService, without going into detail, and focus on how to configure the dependencies of downstream services in GreetingService. The complete XML configuration is as follows:

The configuration here is similar to the HelloService above, and there are two key points to focus on:

Add the startup class of GreeeingService, similar to HelloService, through the configuration of spring/greeting-service.xml to initialize a new spring context to complete.

In step 3, note that the service needs to be exposed on different ports, otherwise it will conflict with HelloService. In this example, port 20881 is selected.

Declare the remote agent for HelloService through step 4, and then assemble it to GreetingService in step 5 to complete the declaration of the upstream and downstream dependencies of the service

Package com.alibaba.dubbo.samples.service.greeting; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Application {public static void main (String [] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("spring/greeting-service.xml"); context.start (); System.out.println ("Greeting service started") / / press any key to exit System.in.read ();}}

Implement the client

Initialize a spring context through resources/spring/client.xml, get the remote agent of GreetingService from it, and initiate a remote call.

Package com.alibaba.dubbo.samples.client;import com.alibaba.dubbo.samples.api.GreetingService;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("spring/client.xml"); context.start (); / / get the remote agent and initiate a call to GreetingService greetingService = (GreetingService) context.getBean ("greetingService") System.out.println (greetingService.greeting ("world"));}

The configuration in resource/spring/client.xml is similar to that of the Dubbo service, mainly configuring remote agents and configuring Zipkin

The directory structure of the completed project is as follows:

Running

Now let's run the entire link and see how Zipkin link tracing works.

Start Zookeeper Server

Execute the following command to start a Zookeeper Server locally. If it is not installed, please download it from the ZooKeeper official website:

$zkServer start starts Zipkin Server

Execute the following command to start a Zipkin Server locally:

$curl-sSL https://zipkin.io/quickstart.sh | bash-s $java-jar zipkin.jar starts HelloService

Start HelloService using the following command, or you can start it directly in IDE:

$mvn exec:java-Dexec.mainClass=com.alibaba.dubbo.samples.service.hello.Application

After starting successfully, you should be able to see the word "Hello service started" on the terminal.

Start GreetingService

Start GreetingService using the following command, or you can start it directly in IDE:

$mvn exec:java-Dexec.mainClass=com.alibaba.dubbo.samples.service.greeting.Application

After starting successfully, you should be able to see the word "Greeting service started" on the terminal.

Run the Dubbo client

Use the following command to run the Dubbo client to make a remote call to GreetingService, or you can run it directly in IDE:

$mvn exec:java-Dexec.mainClass=com.alibaba.dubbo.samples.client.Application

After successful execution, the client will output "greeting, hello, world" on the terminal.

Link tracing

Open a browser to access "http://localhost:9411" and search through the "Find Traces" button to find the link trace you just called. The effect is as follows:

You can further select each span to view the details within the call boundary. For example, the details of the hello-service span are as follows:

At this point, the study on "how to use Zipkin to achieve distributed tracking in Dubbo" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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