Docker部署Zipkin与SpringBoot集成
作者:互联网
Zipkin
Zipkin分布式跟踪系统;它可以帮助收集时间数据,解决在microservice架构下的延迟问题;它管理这些数据的收集和查找;Zipkin的设计是基于谷歌的Google Dapper论文。
每个应用程序向Zipkin报告定时数据,Zipkin UI呈现了一个依赖图表来展示多少跟踪请求经过了每个应用程序;如果想解决延迟问题,可以过滤或者排序所有的跟踪请求,并且可以查看每个跟踪请求占总跟踪时间的百分比。
官网
OpenZipkin · A distributed tracing system https://zipkin.io/
快速开始
Quickstart · OpenZipkin https://zipkin.io/pages/quickstart.html
为什么使用Zipkin
随着业务越来越复杂,系统也随之进行各种拆分,特别是随着微服务架构和容器技术的兴起,看似简单的一个应用,后台可能有几十个甚至几百个服务在支撑;一个前端的请求可能需要多次的服务调用最后才能完成;当请求变慢或者不可用时,我们无法得知是哪个后台服务引起的,这时就需要解决如何快速定位服务故障点,Zipkin分布式跟踪系统就能很好的解决这样的问题。
Docker部署Zipkin
Docker的介绍与安装可参考如下文章
docker run -d -p 9411:9411 openzipkin/zipkin
浏览器打开网址:
SpringBoot中整合Zipkin
maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-core</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-spancollector-http</artifactId>
<version>3.9.0</version>
</dependency>
<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>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
ZipkinBean
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.Brave.Builder;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.Sampler;
import com.github.kristofa.brave.SpanCollector;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpSpanCollector;
import com.github.kristofa.brave.http.HttpSpanCollector.Config;
import com.github.kristofa.brave.httpclient.BraveHttpRequestInterceptor;
import com.github.kristofa.brave.httpclient.BraveHttpResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;
@Configuration
public class ZipkinBean {
/**
* 配置收集器
*
* @return
*/
@Bean
public SpanCollector spanCollector() {
Config config = HttpSpanCollector.Config.builder().compressionEnabled(false).connectTimeout(5000)
.flushInterval(1).readTimeout(6000).build();
return HttpSpanCollector.create("http://192.168.43.18:9411", config, new EmptySpanCollectorMetricsHandler());
}
/**
* Brave各工具类的封装
*
* @param spanCollector
* @return
*/
@Bean
public Brave brave(SpanCollector spanCollector) {
Builder builder = new Builder("service1");// 指定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客户端,需要clientRequestInterceptor,clientResponseInterceptor分别完成cs和cr操作
*
* @param brave
* @return
*/
@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;
}
}
TestController
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
public String test(){
return "hello Zipkin";
}
}
MainApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
启动项目
然后浏览器中访问
http://localhost:8080/testhttp://localhost:8080/test
可以多访问几次,到时看看效果
观察zipkin web ui
浏览器打开网址:
由于我访问了三次/test接口,所以有三个
可以查看详情
标签:brave,SpringBoot,Zipkin,github,kristofa,import,Docker,com 来源: https://blog.csdn.net/zhiwenganyong/article/details/122792472