今天就跟大家聊聊有关什么是spring-cloud-sleuth+zipkin源码,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联建站是一家专业提供南安企业网站建设,专注与做网站、成都做网站、H5建站、小程序制作等业务。10年已为南安众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
粗略看了下spring cloud sleuth core源码,发现内容真的有点多,它支持了很多类型的链路追踪,我就找其中一个比较有代表性的深入剖析下源码结构和内容
可以看到源码中支持的追踪类型有很多,支持async,hystrix,websocket,rxjava,Spring mvc,servlet,spring restTemplate,feign,zuul等等,这里我着重探讨spring web mvc的链路追踪
打开web包,找到TraceWebAutoConfiguration,这里配置了主要的初始化类
当启动初始化程序时,跟踪代码如下
@Bean public FilterRegistrationBean traceWebFilter(TraceFilter traceFilter) { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( traceFilter); filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST); filterRegistrationBean.setOrder(TraceFilter.ORDER); return filterRegistrationBean; } @Bean @ConditionalOnMissingBean public TraceFilter traceFilter(BeanFactory beanFactory, SkipPatternProvider skipPatternProvider) { return new TraceFilter(beanFactory, skipPatternProvider.skipPattern()); }
初始化traceFilter,进行过滤器注册
然后看TraceWebMvcConfigurer
类,它会进行拦截器的注册
@Configuration class TraceWebMvcConfigurer extends WebMvcConfigurerAdapter { @Autowired BeanFactory beanFactory; @Bean public TraceHandlerInterceptor traceHandlerInterceptor(BeanFactory beanFactory) { return new TraceHandlerInterceptor(beanFactory); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.beanFactory.getBean(TraceHandlerInterceptor.class)); } }
在TraceHandlerInterceptor
类中,preHandle
,afterCompletion
方法可以看出,这是对请求进行拦截进行span的包装
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String spanName = spanName(handler); boolean continueSpan = getRootSpanFromAttribute(request) != null; Span span = continueSpan ? getRootSpanFromAttribute(request) : getTracer().createSpan(spanName); if (log.isDebugEnabled()) { log.debug("Handling span ">
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { if (isErrorControllerRelated(request)) { if (log.isDebugEnabled()) { log.debug("Skipping closing of a span for error controller processing"); } return; } Span span = getRootSpanFromAttribute(request); if (ex != null) { getErrorParser().parseErrorTags(span, ex); } if (getNewSpanFromAttribute(request) != null) { if (log.isDebugEnabled()) { log.debug("Closing span " + span); } Span newSpan = getNewSpanFromAttribute(request); getTracer().continueSpan(newSpan); getTracer().close(newSpan); clearNewSpanCreatedAttribute(request); } }
这里首先会初始化HttpZipkinSpanReporter
类,,用来进行span
端点提交,然后初始化ZipkinSpanListener
span的监听器,用来监听并调用端点提交,以上配置再下图位置
首先进入TraceFilter
中的过滤方法doFilter
,这里会做span
的创建
private Span createSpan(HttpServletRequest request, boolean skip, Span spanFromRequest, String name) { if (spanFromRequest != null) { if (log.isDebugEnabled()) { log.debug("Span has already been created - continuing with the previous one"); } return spanFromRequest; } //加入调用链路ZipkinHttpSpanExtractor,此链路在TraceHttpAutoConfiguration中配置实例化,调用链还没有时,返回为空,作为头节点 Span parent = spanExtractor().joinTrace(new HttpServletRequestTextMap(request)); if (parent != null) { if (log.isDebugEnabled()) { log.debug("Found a parent span ">
在preHandle
方法中,对span
进行包装,然后把span放入请求头header中
最后再DefaultTracer
中进行span的关闭和spanReporter
的提交
看完上述内容,你们对什么是spring-cloud-sleuth+zipkin源码有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。