Spring boot 中配置线程池例子

模拟场景:

1.一个方法中如果需要调用多个服务,如果使用传统同步的方法会让用户等待太久。

2.这时,我们需要开多个线程来完成各种微服务得调用。这会大大降低用户等待的时间。

3.但是,如果这个方法还涉及到高并发的场景,会导致不断开线程,导致系统资源很容易撑爆得情况。

 

为解决以上场景出现的问题,使用线程池是比较有效的解决方案,以下介绍spring boot中配置线程池得简单配置如使用方案

 

1.在src/main/resources/application.properties 添加对应配置信息

gmall.pool.coreSize=8          #池的核心大小,
gmall.pool.maximumPoolSize=100 #池的最大线程数,
gmall.pool.queueSize=1000000   #池的队列长度,超出队列以外的请求拒绝

  

2.添加线程池配置文件 src\main\java\[XXX包]\config\ThreadPoolConfig.java

/**
 * 配置当前系统的线程池信息
 */
@Configuration
public class ThreadPoolConfig {
    @Value("${gmall.pool.coreSize}")
    private Integer coreSize;

    @Value("${gmall.pool.maximumPoolSize}")
    private Integer maximumPoolSize;

    @Value("${gmall.pool.queueSize}")
    private Integer queueSize;


  //核心业务线程池  
    @Bean("mainThreadPoolExecutor")
    public ThreadPoolExecutor mainThreadPoolExecutor(PoolProperties poolProperties){
        /**
         *   public ThreadPoolExecutor(int corePoolSize,
         *                               int maximumPoolSize,
         *                               long keepAliveTime,
         *                               TimeUnit unit,
         *                               BlockingQueue<Runnable> workQueue,
         *                               RejectedExecutionHandler handler) {
         */
        LinkedBlockingDeque<Runnable> deque = new LinkedBlockingDeque<>(poolProperties.getQueueSize());

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolProperties.getCoreSize(),
                poolProperties.getMaximumPoolSize(), 10,
                TimeUnit.MINUTES, deque);

        return threadPoolExecutor;
    }

  // 非核心业务线程池 @Bean("otherThreadPoolExecutor") public ThreadPoolExecutor otherThreadPoolExecutor(PoolProperties poolProperties){ LinkedBlockingDeque<Runnable> deque = new LinkedBlockingDeque<>(poolProperties.getQueueSize()); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolProperties.getCoreSize(), poolProperties.getMaximumPoolSize(), 10, TimeUnit.MINUTES, deque); return threadPoolExecutor; } }

  

3.在控制器中调用

@RestController
public class ProductItemController {


    @Qualifier("mainThreadPoolExecutor")
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Qualifier("otherThreadPoolExecutor")
    @Autowired
    ThreadPoolExecutor otherThreadPoolExecutor;

    /**
     * 数据库(商品的基本信息表、商品的属性表、商品的促销表)和  es(info/attr/sale)
     *
     * 查加缓存
     * 1、第一次查。肯定长。
     * @return
     */
    public EsProduct productInfo2(Long id){


        CompletableFuture.supplyAsync(()->{
            return "";
        },threadPoolExecutor).whenComplete((r,e)->{
            System.out.println("处理结果"+r);
            System.out.println("处理异常"+e);
        });
        //1、商品基本数据(名字介绍等) 100ms   异步


        //2、商品的属性数据  300ms
    
        //3、商品的营销数据  SmsService 1s 500ms

        //4、商品的配送数据  WuliuService 2s  700ms

        //5、商品的增值服务数据  SaleService  1s 1s


        //otherThreadPoolExecutor.submit()

     
        return null;
    }


}

  

 

posted @ 2020-04-10 15:46  shifu204  阅读(1923)  评论(0编辑  收藏  举报