使用zipkin配置spring boot的链路器(httpclient、restTemplate)

一、首先导入zipkin需要的依赖

        <!--zipkin-brave start-->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-core</artifactId>
            <version>3.9.0</version>
        </dependency>
        <!--http方式收集-->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-spancollector-http</artifactId>
            <version>3.9.0</version>
        </dependency>
        <!--servlet装配-->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-web-servlet-filter</artifactId>
            <version>3.9.0</version>
        </dependency>

        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-apache-http-interceptors</artifactId>
            <version>3.9.0</version>
        </dependency>
        <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <!--zipkin-brave end-->

二、配置类中添加相应的配置

配置拦截器、收集器以及Brave

@Bean
    public SpanCollector spanCollector() {
        HttpSpanCollector.Config config = HttpSpanCollector.Config.builder().compressionEnabled(false).connectTimeout(5000)
                .flushInterval(1).readTimeout(6000).build();
        return HttpSpanCollector.create("你的zipkin地址及端口", config, new EmptySpanCollectorMetricsHandler());
    }

    /**
     * Brave各工具类的封装
     *
     * @param spanCollector
     * @return
     */
    @Bean
    public Brave brave(SpanCollector spanCollector) {
        Brave.Builder builder = new Brave.Builder("你定义的服务名称");// 指定serviceName
        builder.spanCollector(spanCollector);
        builder.traceSampler(Sampler.create(1));// 采集率
        return builder.build();
    }

    /**
     * 拦截器,需要serverRequestInterceptor,serverResponseInterceptor 分别完成sr和ss操作
     *
     * @param brave
     * @return
     */
    @Bean
    public BraveServletFilter braveServletFilter(Brave brave) {
        return new BraveServletFilter(brave.serverRequestInterceptor(), brave.serverResponseInterceptor(),
                new DefaultSpanNameProvider());
    }

配置httpclient

    @Bean
    public CloseableHttpClient httpClient(Brave brave) {
        CloseableHttpClient httpclient = HttpClients.custom()
                .addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(),
                        new DefaultSpanNameProvider()))
                .addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build();
        return httpclient;
    }

配置restTemplate

    @Bean
    public RestTemplateCustomizer restTemplateCustomizer(CloseableHttpClient closeableHttpClient) {

        return new RestTemplateCustomizer() {
            @Override
            public void customize(RestTemplate restTemplate) {
                restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(closeableHttpClient));
            }
        };
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        RestTemplate build = restTemplateBuilder.build();
        return build;

    }

三、项目中的使用

http方式

    @Autowired
    private CloseableHttpClient closeableHttpClient;

    @RequestMapping("zkHttp")
    public String testZipKin() throws IOException {
        HttpGet get = new HttpGet("http://127.0.0.1:9193/demo2/test");
        CloseableHttpResponse response = closeableHttpClient.execute(get);
        return EntityUtils.toString(response.getEntity(), "utf-8");
    }

restTemplate方式

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("testRestTemplate")
    public String testRestTemplate(){
        String forObject = restTemplate.getForObject("http://127.0.0.1:9193/demo2/test", String.class);
        return forObject;
    }

zipkin的结果如下

 

 我的调用方式是:demo1调用demo2,demo2又调用了demo1

 

posted @ 2019-08-30 16:12  夜洛IT后端技术分享  阅读(1049)  评论(0编辑  收藏  举报