SpringMVC集成zipkin性能监控

一:导入jar包

<brave.version>3.16.0</brave.version>
<zipkin-reporter.version>0.6.9</zipkin-reporter.version>
​
​
<!--zipkin开始-->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-core-spring</artifactId>
            <version>${brave.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter</groupId>
            <artifactId>zipkin-sender-okhttp3</artifactId>
            <version>${zipkin-reporter.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter</groupId>
            <artifactId>zipkin-sender-libthrift</artifactId>
            <version>${zipkin-reporter.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter</groupId>
            <artifactId>zipkin-sender-kafka08</artifactId>
            <version>${zipkin-reporter.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spring-web-servlet-interceptor</artifactId>
            <version>${brave.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spring-resttemplate-interceptors</artifactId>
            <version>${brave.version}</version>
        </dependency>

  

二:配置前端过滤器(如已配置请忽略)

三:写自定义配置类

package com.mzx.controller;
    /**
     * @Param:
     * @return:
     * @Author: Mr.Meng
     * @Date: 2018/10/24
     */
import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.SpanNameProvider;
import com.github.kristofa.brave.spring.BraveClientHttpRequestInterceptor;
import com.github.kristofa.brave.spring.ServletHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import zipkin.Span;
import zipkin.reporter.AsyncReporter;
import zipkin.reporter.Reporter;
import zipkin.reporter.Sender;
import zipkin.reporter.okhttp3.OkHttpSender;
​
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
​
/**
 * This adds tracing configuration to any web mvc controllers or rest template clients. This should
 * be configured last.
 */
@Configuration
@EnableWebMvc // import as the interceptors are annotation with javax.inject and not automatically wired @Import({BraveClientHttpRequestInterceptor.class, ServletHandlerInterceptor.class}) public class WebTracingConfiguration extends WebMvcConfigurerAdapter { /** Configuration for how to send spans to Zipkin */ @Bean Sender sender() { return OkHttpSender.create("http://127.0.0.1:9411/api/v1/spans"); //return LibthriftSender.create("127.0.0.1"); // return KafkaSender.create("127.0.0.1:9092"); } ​ /** Configuration for how to buffer spans into messages for Zipkin */ @Bean Reporter<Span> reporter() { // return new LoggingReporter(); // Just log json info!!! // uncomment to actually send to zipkin! return AsyncReporter.builder(sender()).build(); } ​ @Bean Brave brave() { return new Brave.Builder("这里写项目业务名称").reporter(reporter()).build(); } ​ // decide how to name spans. By default they are named the same as the http method. @Bean SpanNameProvider spanNameProvider() { return new DefaultSpanNameProvider(); } @Bean RestTemplate template() { return new RestTemplate(); } ​ @Autowired private ServletHandlerInterceptor serverInterceptor; ​ @Autowired private BraveClientHttpRequestInterceptor clientInterceptor; ​ @Autowired private RestTemplate restTemplate; ​ // adds tracing to the application-defined rest template @PostConstruct public void init() { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(restTemplate.getInterceptors()); interceptors.add(clientInterceptor); restTemplate.setInterceptors(interceptors); } ​ // adds tracing to the application-defined web controllers @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(serverInterceptor); } }

  

转载于:https://www.cnblogs.com/mengyixin/p/9850765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值