zipkin-server
1. File-->new spring starter project
2.add dependency
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE com.smile zipkin-server 0.0.1 zipkin-server Demo project for Spring Boot 1.8 Greenwich.SR1 true io.zipkin.java zipkin-server 2.11.8 org.apache.logging.log4j log4j-slf4j-impl io.zipkin.java zipkin-autoconfigure-ui 2.11.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-devtools org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
3.Edit application.yml
server: port: 9000spring: application: name: zipkin-servereureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #zipkin启动报错无法访问的解决方法management: metrics: web: server: auto-time-requests: false
4.program
package com.smile;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import zipkin2.server.internal.EnableZipkinServer;@SpringBootApplication@EnableZipkinServer@EnableAutoConfiguration@EnableEurekaClientpublic class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); }}
5.Run
visit:
这个项目各种报错,成功踩了两个坑
项目添加zipkin支持
在项目producer
和consumer-feign-hystrix
中添加zipkin的支持。创建项目 zipkin-producer 和zipkin-consumer
org.springframework.cloud spring-cloud-starter-zipkin
Spring应用在监测到Java依赖包中有sleuth和zipkin后,会自动在RestTemplate的调用过程中向HTTP请求注入追踪信息,并向Zipkin Server发送这些信息。
同时配置文件中添加如下代码:
spring: zipkin: base-url: http://localhost:9000 sleuth: sampler: percentage: 1.0
spring.zipkin.base-url指定了Zipkin服务器的地址,spring.sleuth.sampler.percentage将采样比例设置为1.0,也就是全部都需要。
Spring Cloud Sleuth有一个Sampler策略,可以通过这个实现类来控制采样算法。采样器不会阻碍span相关id的产生,但是会对导出以及附加事件标签的相关操作造成影响。 Sleuth默认采样算法的实现是Reservoir sampling,具体的实现类是PercentageBasedSampler,默认的采样比例为: 0.1(即10%)。不过我们可以通过spring.sleuth.sampler.percentage来设置,所设置的值介于0.0到1.0之间,1.0则表示全部采集。
这两个项目添加zipkin之后,依次进行启动。
进行验证
这样我们就模拟了这样一个场景,通过zipkin-consumer调用zipkin-producer提供的服务。
四个项目均启动后,在浏览器中访问地址:http://localhost:8007/hello/smile 两次,然后再打开地址: http://localhost:9000/zipkin/
点击对应按钮进行查看。
点击查找看到有两条记录
点击记录进去页面,可以看到每一个服务所耗费的时间和顺序
点击依赖分析,可以看到项目之间的调用关系