zipkin: create your own link tracking system (a)

In the micro-services in its heyday, the number of applications a company of hundreds of hundreds. Complex dependencies between applications, locate the problem, troubleshoot problems is a daunting thing. To solve this problem, Google's Dapper paper came into being. Twitter based on the paper to create his own link tracking system (ie, the protagonist of this article): zipkin and open source

Brief introduction

Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data. Zipkin is a distributed tracking system. It helps to collect the data needed to solve the timing delays service architecture. Features include the collection and find this data.

A brief look at zipkin, detailed description of the venue: zipkin official website

Architecture

zipkin architecture

  • reporter: data link reporting module, disposed in the particular application
  • transport: the data transmission link module, generally http, Kafka
  • collector: collect & module consumption data link, by default http collect, can be configured to Kafka consumption
  • storage: a module storing link data, specific examples may ES, Cassandra or mysql

Link data model

[
    {
        "traceId":"5982fe77008310cc80f1da5e10147517",
        "name":"get",
        "id":"bd7a977555f6b982", "timestamp":1458702548467000, "duration":386000, "localEndpoint":{ "serviceName":"zipkin-query", "ipv4":"192.168.1.2", "port":9411 }, "annotations":[ { "timestamp":1458702548467000, "value":"sr" }, { "timestamp":1458702548853000, "value":"ss" } ] } ] 

For more information about other zipkin of the venue:

  • zipkin official website
  • zipkin github

Why choose zipkin

Industry as well as other open source link tracking system, why should we choose zipkin?

First of all listed their core demands:

  1. Performance little influence: the ability to tolerate a slight performance loss
  2. Multi-language support: Java, Node, Go, etc.
  3. Plug Scalable: You can develop custom link tracking plug-ins
  4. Community support large: you do not need too much to develop plug-ins link
  5. Small access costs

The industry mainstream open source link tracking system:

  1. skywalking
  2. pinpoint
  3. zipkin
  4. jaeger

The main contrast skywalking and zipkin

  skywalking zipkin
Internal implementation javaagent, bytecode enhancement aop plug
language support multi-language multi-language
performance it is good it is good
Plug-in extension difficult easily
Access costs Low, no development of perception Low, you need to configure Development
Community Support it is good it is good

can be seen

  • skywalking compared to zipkin has the advantage of low cost access, slightly better performance
  • zipkin compared to skywalking has the advantage of easy plug-in extensions

We finally chose zipkin

zipkin和brave

First, explain the relationship zipkin and brave:

  1. As can be seen in the beginning of the Chart: zipkin is the server for query and analysis, data collection and persistent links
  2. brave is the official link data acquisition plug-zipkin produced the Java language. Similarly there js, go version of the plug-in collection

Server set up zipkin

Provided in the official demo in the docker mirrored boot bag and jar start, but if you do, then develop a personalized introduction zipkin server it must then be started by self-dependent project. The first two ways to start the official website has a detailed tutorial, not presented here. The following describes about the self-built project to introduce zipkin server-dependent way to start.

Creating SpringBoot project

After creating SpringBoot projects related to the introduction of zipkin server jar package:

		<!-- zipkin 核心依赖 -->
        <dependency>
            <groupid>io.zipkin.java</groupid> <artifactid>zipkin-server</artifactid> <version>${zipkin-server.version}</version> </dependency> <!-- ui界面 可选 --> <dependency> <groupid>io.zipkin.java</groupid> <artifactid>zipkin-autoconfigure-ui</artifactid> <version>${zipkin-server.version}</version> </dependency> <!-- es存储 可选 --> <dependency> <groupid>io.zipkin.java</groupid> <artifactid>zipkin-autoconfigure-storage-elasticsearch</artifactid> <version>${zipkin-server.version}</version> </dependency> <!-- kafka collector 可选 --> <dependency> <groupid>io.zipkin.java</groupid> <artifactid>zipkin-autoconfigure-collector-kafka</artifactid> <version>${zipkin-server.version}</version> </dependency> 

Configuring kafka and address es:

zipkin.collector.kafka.bootstrap-servers=
zipkin.collector.kafka.topic=
zipkin.collector.kafka.groupId=

zipkin.storage.type=elasticsearch
zipkin.storage.elasticsearch.hosts= 

Add @EnableZipkinServercomment

Finally configured on SpringBoot startup class @EnableZipkinServernotes

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

So far, zipkin server can be run on a build completed; customized development: alarm, performance analysis reintroduced next blog. Here are some brave of use;

brave use

Use brave require maven rely attached to the end of the article

brave official list of plug-ins is very much, basically covering the daily use of the link: http, rpc, db and so on. This is the official support of middleware plug:Support plug-ins

Http API requesting access to Linktrace

Http API access to the following link tracking as an example, we need to configure places: official demo

/**
 * This adds tracing configuration to any web mvc controllers or rest template clients.
 */
@Configuration
// Importing a class is effectively the same as declaring bean methods
@Import(SpanCustomizingAsyncHandlerInterceptor.class) public class TracingConfiguration extends WebMvcConfigurerAdapter { /** * Configuration for how to send spans to Zipkin * 配置如何发送到zipkin服务器,这里使用http的方式发送 */ @Bean Sender sender() { return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); } /** * Configuration for how to buffer spans into messages for Zipkin * 配置reporter,何时发送到zipkin。触发方式:定时、size大小等 * */ @Bean AsyncReporter<span> spanReporter() { return AsyncReporter.create(sender()); } /** * Controls aspects of tracing such as the service name that shows up in the UI * 发送到zipkin的服务名,同一个应用的多个实例服务名应该相同 */ @Bean Tracing tracing(@Value("${spring.application.name}") String serviceName) { return Tracing.newBuilder() .localServiceName(serviceName) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder() // puts trace IDs into logs // 可以通过MDC.get("traceId")的方式拿到链路ID .addScopeDecorator(MDCScopeDecorator.create()) .build() ) .spanReporter(spanReporter()).build(); } /** * Allows someone to add tags to a span if a trace is in progress * 允许添加自定义tag到链路中 * */ @Bean SpanCustomizer spanCustomizer(Tracing tracing) { return CurrentSpanCustomizer.create(tracing); } /** Decides how to name and tag spans. By default they are named the same as the http method */ @Bean HttpTracing httpTracing(Tracing tracing) www.motianydl.cn { return HttpTracing.create(tracing); } /** * Creates server spans for http requests * 为http请求自动创建链路或者span * */ @Bean Filter tracingFilter(HttpTracing httpTracing) { return TracingFilter.create(httpTracing); } /** * 为RestTemplate发起的请求自动创建zipkin的链路信息 * */ @Bean RestTemplateCustomizer useTracedHttpClient(HttpTracing httpTracing) { final CloseableHttpClient httpClient = TracingHttpClientBuilder.create(httpTracing).build(); return new RestTemplateCustomizer(www.chuancenpt.com) { @Override public void customize(RestTemplate restTemplate) { restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); } }; } @Autowired SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer; /** Decorates server spans with application-defined web tags */ @Override public void www.51kunlunyule.com addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(webMvcTracingCustomizer); } } 

Core configuration above code is:

  1. sender: sent to the server zipkin way: http or kafka
  2. reporter: reporter decide when to send data to the link-local zipkin server
  3. tracing: very important, all of the link trace components are dependent on it. Including the development of its own link tracking component
  4. httpTracing, filter: http add a link to information requests

The dubbo rpc request access Linktrace

Because the last step has to brave the core configuration have to accompany Okay, so the dubbo link added to the link tracking is very simple, two-step process:

First introduced brave dubbo its dependencies:

        <!--  dubbo插件      -->
        <dependency>
            <groupid>io.zipkin.brave</groupid> <artifactid>brave-instrumentation-dubbo-rpc</artifactid> </dependency> 

Brave dubbo filter and then added to the filter chain Dubbo:

  • Method 1: Add two lines in application.properties configuration file:
dubbo.consumer.filter=tracing
dubbo.provider.filter=tracing
  • Method 2: Add disposed dubbo xml configuration file
<dubbo:consumer filter="tracing" www.luqintang.com />
<dubbo:provider filter="tracing"shentuylzc.cn /> 

Yes, it's that simple!

The operation to add mysql Linktrace

Database operations in the ring the entire request link is very important, because a lot of problems caused by database sql execution timeout. It is necessary to monitor the operation of the database, to be specific configuration refer to the official line:

/**
 * A MySQL exception interceptor that will annotate spans with SQL error codes.
 *
 * <p>To use it, both TracingQueryInterceptor and TracingExceptionInterceptor must be added by
 * appending <code>?queryInterceptors=brave.mysql8.TracingQueryInterceptor&amp;exceptionInterceptors=brave.mysql8.TracingExceptionInterceptor</code>. */ 

Other links

Like the other methods of access link, you can refer to the official documentation, do not start here presented.

other

zipkin series of articles

  1. Add link tracking to Ali cloud ons / RocketMQ
  2. zipkin server development of personalized: statistical reporting capabilities, alarm service

zipkin related websites

  • zipkin official website
  • zipkin github
  • brave github

Used to brave dependent

		<!--  核心依赖 -->
        <dependency>
            <groupid>io.zipkin.brave</groupid> <artifactid>brave</artifactid> </dependency> <!-- reporter www.shentuylgw.cn--> <dependency> <groupid>io.zipkin.reporter2</groupid> <artifactid>zipkin-sender-okhttp3</artifactid> </dependency> <dependency> <groupid>io.zipkin.reporter2</groupid> <artifactid>zipkin-sender-kafka</artifactid> </dependency> <!-- 日志依赖 --> <!-- Integrates so you can use log patterns like %X{traceId}/%X{spanId} --> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-context-slf4j</artifactid> </dependency> <!-- spring mvc项目支持 -www.lecaixuanzc.cn-> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-spring-beans</artifactid> </dependency> <!-- mvc插件 --> <!-- Adds the MVC class and method names www.baihuayl7.cn  server spans --> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-instrumentation-spring-webmvc</artifactid> </dependency> <!-- httpclient插件 www.chenghylpt.com  --> <!-- Instruments the underlying HttpClient requests that call the backend --> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-instrumentation-httpclient</artifactid> </dependency> <!-- dubbo插件 www.maidongylgw.cn--> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-instrumentation-dubbo-rpc</artifactid> </dependency> <!-- mysql插件 www.javachenglei.com --> <dependency> <groupid>io.zipkin.brave</groupid> <artifactid>brave-instrumentation-mysql8<www.yongshiyule178.com /artifactid> </dependency>

Guess you like

Origin www.cnblogs.com/laobeipai/p/12589323.html