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); } }

  


其中:127.0.0.1:9411請使用給定的測試和正式服務器ip進行替換

”這里寫項目業務名稱“:請使用規范寫法

  想要以Java形式定制默認的配置,簡單的實現WebMvcConfigurer接口,或者繼承WebMvcConfigurerAdapter並重寫需要的方法

  在剛才的自定義配置類的時候,我們去繼承了WebMvcConfigurerAdapter,所以加入@EnableWebMvc注解指定我們的配置生效

  

五:啟動測試

請啟動自己的項目再隨意訪問自己的若干個接口

然后根據給定ip:9411進行查看是否監控上

注:數據量大的話,可能會出現延遲,最長不超過10分鍾。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM