In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about what is the spring-cloud-sleuth+zipkin source code, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1.1. Preface
took a cursory look at the spring cloud sleuth core source code and found that there is really a lot of content. It supports many types of link tracking. I will find one of the more representative in-depth analysis of the source code structure and content.
1.2. Spring-cloud-sleuth-core source code parsing 1.2.1. structure
You can see that there are many tracking types supported in the source code, such as async,hystrix,websocket,rxjava,Spring mvc,servlet,spring restTemplate,feign,zuul, and so on. Here I focus on spring web mvc link tracing.
Open the web package and find TraceWebAutoConfiguration, where the main initialization classes are configured
1.2.2. Filter registration
When you start the initialization program, the tracking code is as follows
@ 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 ());}
Initialize traceFilter for filter registration
1.2.3. Interceptor registration
Then look at the TraceWebMvcConfigurer class, which registers the interceptor
@ Configurationclass 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));}}
In the TraceHandlerInterceptor class, the preHandle,afterCompletion method can see that this is the wrapper for span to intercept the request
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);}} 1.2.4. Zipkin Endpoint submission
Here we first initialize the HttpZipkinSpanReporter class, which is used to make the span endpoint submission, and then initialize the ZipkinSpanListenerspan listener, which is used to listen and invoke the endpoint submission. The above configuration is located in the following figure.
1.2.5. When calling the http interface, enter the filter
First go to the filter method doFilter in TraceFilter, where the span will be created.
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 } / / join the calling link ZipkinHttpSpanExtractor, which is instantiated in TraceHttpAutoConfiguration. If the call chain is not available, it returns empty as the header node Span parent = spanExtractor (). JoinTrace (new HttpServletRequestTextMap (request)); if (parent! = null) {if (log.isDebugEnabled ()) {log.debug ("Found a parent span" > 1.2.6). Enter the interceptor
In the preHandle method, wrap the span, and then put the span in the request header header
Finally, span is closed and spanReporter is submitted in DefaultTracer.
After reading the above, do you have any further understanding of what is the spring-cloud-sleuth+zipkin source code? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.