【Spring Cloud Sleuth 线程池的跟踪】

Spring Cloud Sleuth 线程池的跟踪

概述

  1. 解决异步线程池和内部线程池,链路跟踪丢失问题,Sleuth提供一个包装类:LazyTraceAsyncCustomizer包装,通过他可以实现异步线程池或线程内部的链路跟踪信息
  2. 异步线程池稍微复杂一点,此处使用自定义AsyncConfigurer实现,详情参考代码和配置

线程池定义-JAVA代码

#1.  代码实现

import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceAsyncCustomizer;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 线程池初始化配置 <br>
 *
 * @date: 2022/1/20 <br>
 * @author: llxiao <br>
 * @since: 1.0 <br>
 * @version: 1.0 <br>
 */
@Configuration
public class ThreadPoolConfig {

    @Autowired
    private BeanFactory beanFactory;

    /**
     * 解决异步线程池,链路跟踪丢失问题
     * <p>
     * 不通过实现接口的方式配置AsyncConfigurer,而是通过@Configuration 的方式注册一个sleuth中的LazyTraceAsyncCustomizer包装类型的AsyncConfigurer
     * <p>
     * 需要配置application.yaml spring: sleuth: async: configurer: enabled: false 将sleuth中默认的org.springframework.cloud.sleuth.instrument.async.AsyncDefaultAutoConfiguration.DefaultAsyncConfigurerSupport
     * 异步线程池的配置忽略掉。
     *
     * @return
     */
    @Bean
    public AsyncConfigurer asyncConfigurer() {
        //  在这里返回一个 LazyTraceAsyncCustomizer 类型的AsyncConfigurer
        return new LazyTraceAsyncCustomizer(this.beanFactory, new AsyncConfigurer() {
            @Override
            public Executor getAsyncExecutor() {
                ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
                //配置核心线程数
                executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
                //配置最大线程数
                executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 20);
                //配置队列大小
                executor.setQueueCapacity(500);
                //配置线程池中的线程的名称前缀
                executor.setThreadNamePrefix("Yk-async-task-");
                //配置保存时间
                executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
                //执行初始化
                executor.initialize();
                return executor;
            }

            @Override
            public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
                return (throwable, method, obj) -> {
                    System.err.println("zidingyi " + throwable);
                };

            }
        });
    }

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("Yk-task-");
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.setQueueCapacity(1000);
        threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
        threadPoolTaskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 20);
        threadPoolTaskExecutor.initialize();
        // sleuth 中延时跟踪执行
        return new LazyTraceThreadPoolTaskExecutor(beanFactory, threadPoolTaskExecutor);
    }
}

application文件配置

spring:
  sleuth:
    async:
      configurer:
        enabled: false
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值