Zipkin 持久化存入Elasticsearch

一.Zipkin服务端

1.1 依赖

 
        1.8
        2.9.4
        2.8.4
    

    
        
            io.zipkin.java
            zipkin-server
            ${zipkin.version}
            
                
                    org.springframework.boot
                    spring-boot-starter-log4j2
                
            
        
        
            io.zipkin.java
            zipkin-autoconfigure-ui
            runtime
            ${zipkin.version}
        
        
            io.zipkin.java
            zipkin-autoconfigure-storage-elasticsearch-http
            ${zipkin.es.version}
            true
        
    

1.2. 配置文件

zipkin.storage.StorageComponent=elasticsearch
zipkin.storage.type=elasticsearch
#可以做集群,我用的本地测试没有部署elastic集群
zipkin.storage.elasticsearch.hosts=192.168.42.40:9200
zipkin.storage.elasticsearch.cluster=elasticsearch
zipkin.storage.elasticsearch.index=zipkin
zipkin.storage.elasticsearch.index-shards=5
zipkin.storage.elasticsearch.index-replicas=1
maxHttpHeaderSize=8192
action.auto_create_index=true

1.3. 启动类

@EnableZipkinServer//开启ZipkinServe
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ZipkinApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZipkinApplication.class, args);
    }
}

二、客户端配置

2.1 SpringBoot 版本

2.1.1 依赖


		
			io.zipkin.brave
			brave-core
			3.10.0
		
		
			io.zipkin.brave
			brave-spancollector-http
			3.10.0
		
		
			io.zipkin.brave
			brave-web-servlet-filter
			3.10.0
		
		
			io.zipkin.brave
			brave-okhttp
			3.10.0
		
		

2.1.2 全局配置

@Data
@Configuration
@ConfigurationProperties(prefix = ZipkinConfig.ZIPKIN_PREFIX)
public class ZipkinConfig {

    public static final String ZIPKIN_PREFIX = "zipkin";

    //服务名称
    private String serviceName;

    //地址
    private String url;

    //连接时间
    private int connectTimeout;

    //读取时间
    private int readTimeout;

    //每间隔多少秒执行一次Span信息上传
    private int flushInterval;
    
    //是否启动压缩
    private boolean compressionEnabled;

    /**
     * @Description span(一次请求信息或者一次链路调用)信息收集器
     * @UserModule: exam-paper
     * @author Dylan
     * @date 2020/1/3
     * @param
     * @return com.github.kristofa.brave.SpanCollector
     */
    @Bean
    public SpanCollector spanCollector() {
        Config config = Config.builder()
                // 默认false,span在transport之前是否会被gzipped
                .compressionEnabled(compressionEnabled)
                .connectTimeout(connectTimeout)
                .flushInterval(flushInterval)
                .readTimeout(readTimeout)
                .build();
        return create(url, config, new EmptySpanCollectorMetricsHandler());
    }

    /**
     * @Description 作为各调用链路,只需要负责将指定格式的数据发送给zipkin
     * @UserModule: exam-paper
     * @author Dylan
     * @date 2020/1/3
     * @param spanCollector
     * @return com.github.kristofa.brave.Brave
     */
    @Bean
    public Brave brave(SpanCollector spanCollector) {
        //调用服务的名称
        Builder builder = new Builder(serviceName);
        builder.spanCollector(spanCollector);
        //采集率
        builder.traceSampler(Sampler.ALWAYS_SAMPLE);
        return builder.build();
    }

    /**
     * @Description 设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器
     * @UserModule: exam-paper
     * @author Dylan
     * @date 2020/1/3
     * @param brave
     * @return com.github.kristofa.brave.servlet.BraveServletFilter
     */
    @Bean
    public BraveServletFilter braveServletFilter(Brave brave) {
        BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
                brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
        return filter;
    }

    /**
     * @Description 设置client的(发起请求和获取到服务端返回信息)拦截器
     * @UserModule: exam-paper
     * @author Dylan
     * @date 2020/1/3
     * @param brave
     * @return okhttp3.OkHttpClient
     */
    @Bean
    public OkHttpClient okHttpClient(Brave brave) {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new BraveOkHttpRequestResponseInterceptor(
                        brave.clientRequestInterceptor(),
                        brave.clientResponseInterceptor(),
                        new DefaultSpanNameProvider())).build();
        return httpClient;
    }
}

2.1.3 配置文件

zipkin.serviceName=exam-app
zipkin.url=http://localhost:8096
zipkin.connectTimeout=6000
zipkin.readTimeout=6000
zipkin.flushInterval=1
zipkin.compressionEnabled=true
zipkin.storage.StorageComponent=elasticsearch
zipkin.storage.type=elasticsearch
zipkin.storage.elasticsearch.hosts=localhost:9200
zipkin.storage.elasticsearch.cluster=elasticsearch
zipkin.storage.elasticsearch.index=zipkin
zipkin.storage.elasticsearch.index-shards=5
zipkin.storage.elasticsearch.index-replicas=1
maxHttpHeaderSize=8192
action.auto_create_index=true
management.metrics.web.server.auto-time-requests=false
management.endpoints.web.exposure.include="*"

2.2 SpringCloud 版本

2.2.1 依赖

 <!--引入的zipkin依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

2.2.2 配置文件

  zipkin:
    base-url: http://localhost:8096
    # 关闭服务发现,否则Spring Cloud会把zipkin的url当做服务名称
    discoveryClientEnabled: false
    sender:
      type: web
  sleuth:
    sampler:
      probability: 1  # 设置抽样采集率为100%,默认为0.1,即10%

三. 验证

3.1 zipkin UI验证

访问消费者之后,在zipkin里会显示
Zipkin 持久化存入Elasticsearch_第1张图片
这时我们关掉服务,再重启,数据还在,说明持久化成功!

3.2 kibana验证

创建索引
Zipkin 持久化存入Elasticsearch_第2张图片
查看
Zipkin 持久化存入Elasticsearch_第3张图片

3.3 ES-Head验证

Zipkin 持久化存入Elasticsearch_第4张图片

工具安装教程

1. Elasticsearch6.x 安装以及一些坑 https://blog.csdn.net/weixin_38937840/article/details/103897483

2.elasticsearch-head 安装以及踩过的坑 https://blog.csdn.net/weixin_38937840/article/details/103897606

3.kibana安装以及踩过的坑https://blog.csdn.net/weixin_38937840/article/details/103897715
下方有免费书籍的地址,大家可以看看是否对自己有用!

今天的分享就到这里,希望对大家有所帮助

关注 Java有货领取更多资料

联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料
在这里插入图片描述

技术博客:https://blog.csdn.net/weixin_38937840

免费书籍:https://github.com/Dylan-haiji/Programmer-Learning-materials

SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud

Redis、Mongo、Rabbitmq、Kafka学习代码: https://github.com/Dylan-haiji/javayh-middleware

AlibabaCloud学习代码:https://github.com/Dylan-haiji/javayh-cloud-nacos

SpringBoot+SpringSecurity实现自定义登录学习代码:https://github.com/Dylan-haiji/javayh-distribution

你可能感兴趣的:(Zipkin,Elasticsearch)