<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          性能爆表:SpringBoot利用ThreadPoolTaskExecutor批量插入百萬(wàn)級(jí)數(shù)據(jù)實(shí)測(cè)!

          共 7363字,需瀏覽 15分鐘

           ·

          2024-04-19 08:05

          關(guān)注我們,設(shè)為星標(biāo),每天7:40不見(jiàn)不散,架構(gòu)路上與您共享

          回復(fù)架構(gòu)師獲取資源


          大家好,我是你們的朋友架構(gòu)君,一個(gè)會(huì)寫(xiě)代碼吟詩(shī)的架構(gòu)師。


          來(lái)源:azdebug.blog.csdn.net/

          article/details/103697108
          • 前言
          • 具體實(shí)現(xiàn)細(xì)節(jié)
          • 測(cè)試結(jié)果
          • 總結(jié)


          前言

          • 開(kāi)發(fā)目的: 提高百萬(wàn)級(jí)數(shù)據(jù)插入效率。
          • 采取方案: 利用ThreadPoolTaskExecutor多線程批量插入。
          • 采用技術(shù): springboot2.1.1+mybatisPlus3.0.6+swagger2.5.0+Lombok1.18.4+postgresql+ThreadPoolTaskExecutor等。

          具體實(shí)現(xiàn)細(xì)節(jié)

          application-dev.properties添加線程池配置信息


          > 基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實(shí)現(xiàn)的后臺(tái)管理系統(tǒng) + 用戶小程序,支持 RBAC 動(dòng)態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
          >
          > * 項(xiàng)目地址:<https://github.com/YunaiV/yudao-cloud>
          > * 視頻教程:<https://doc.iocoder.cn/video/>

          # 異步線程配置
          # 配置核心線程數(shù)
          async.executor.thread.core_pool_size = 30
          # 配置最大線程數(shù)
          async.executor.thread.max_pool_size = 30
          # 配置隊(duì)列大小
          async.executor.thread.queue_capacity = 99988
          # 配置線程池中的線程的名稱前綴
          async.executor.thread.name.prefix = async-importDB-

          spring容器注入線程池bean對(duì)象

          @Configuration
           
          @EnableAsync
           
          @Slf4j
           
          public class ExecutorConfig {
              @Value("${async.executor.thread.core_pool_size}")
              private int corePoolSize;
              @Value("${async.executor.thread.max_pool_size}")
              private int maxPoolSize;
              @Value("${async.executor.thread.queue_capacity}")
              private int queueCapacity;
              @Value("${async.executor.thread.name.prefix}")
              private String namePrefix;
           
              @Bean(name = "asyncServiceExecutor")
              public Executor asyncServiceExecutor() {
                  log.warn("start asyncServiceExecutor");
                  //在這里修改
                  ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
                  //配置核心線程數(shù)
                  executor.setCorePoolSize(corePoolSize);
                  //配置最大線程數(shù)
                  executor.setMaxPoolSize(maxPoolSize);
                  //配置隊(duì)列大小
                  executor.setQueueCapacity(queueCapacity);
                  //配置線程池中的線程的名稱前綴
                  executor.setThreadNamePrefix(namePrefix);
                  // rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)
                  // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來(lái)執(zhí)行
                  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
                  //執(zhí)行初始化
                  executor.initialize();
                  return executor;
              }
          }

          創(chuàng)建異步線程 業(yè)務(wù)類

          @Service
          @Slf4j
          public class AsyncServiceImpl implements AsyncService {
          @Override
              @Async("asyncServiceExecutor")
              public void executeAsync(List<LogOutputResult> logOutputResults, LogOutputResultMapper logOutputResultMapper, CountDownLatch countDownLatch) {
                  try{
                      log.warn("start executeAsync");
                      //異步線程要做的事情
                      logOutputResultMapper.addLogOutputResultBatch(logOutputResults);
                      log.warn("end executeAsync");
                  }finally {
                      countDownLatch.countDown();// 很關(guān)鍵, 無(wú)論上面程序是否異常必須執(zhí)行countDown,否則await無(wú)法釋放
                  }
              }
          }

          創(chuàng)建多線程批量插入具體業(yè)務(wù)方法

          @Override
              public int testMultiThread() {
                  List<LogOutputResult> logOutputResults = getTestData();
                  //測(cè)試每100條數(shù)據(jù)插入開(kāi)一個(gè)線程
                  List<List<LogOutputResult>> lists = ConvertHandler.splitList(logOutputResults, 100);
                  CountDownLatch countDownLatch = new CountDownLatch(lists.size());
                  for (List<LogOutputResult> listSub:lists) {
                      asyncService.executeAsync(listSub, logOutputResultMapper,countDownLatch);
                  }
                  try {
                      countDownLatch.await(); //保證之前的所有的線程都執(zhí)行完成,才會(huì)走下面的;
                      // 這樣就可以在下面拿到所有線程執(zhí)行完的集合結(jié)果
                  } catch (Exception e) {
                      log.error("阻塞異常:"+e.getMessage());
                  }
                  return logOutputResults.size();
              }

          模擬2000003 條數(shù)據(jù)進(jìn)行測(cè)試

          多線程 測(cè)試 2000003  耗時(shí)如下:耗時(shí)1.67分鐘

          本次開(kāi)啟30個(gè)線程,截圖如下:

          單線程測(cè)試2000003  耗時(shí)如下:耗時(shí)5.75分鐘

          檢查多線程入庫(kù)的數(shù)據(jù),檢查是否存在重復(fù)入庫(kù)的問(wèn)題:

          根據(jù)id分組,查看是否有id重復(fù)的數(shù)據(jù),通過(guò)sql語(yǔ)句檢查,沒(méi)有發(fā)現(xiàn)重復(fù)入庫(kù)的問(wèn)題

          檢查數(shù)據(jù)完整性:通過(guò)sql語(yǔ)句查詢,多線程錄入數(shù)據(jù)完整

          測(cè)試結(jié)果

          不同線程數(shù)測(cè)試:

          總結(jié)

          通過(guò)以上測(cè)試案列,同樣是導(dǎo)入2000003  條數(shù)據(jù),多線程耗時(shí)1.67分鐘,單線程耗時(shí)5.75分鐘。通過(guò)對(duì)不同線程數(shù)的測(cè)試,發(fā)現(xiàn)不是線程數(shù)越多越好,具體多少合適,網(wǎng)上有一個(gè)不成文的算法:

          CPU核心數(shù)量*2 +2 個(gè)線程。

          附:測(cè)試電腦配置


          到此文章就結(jié)束了。Java架構(gòu)師必看一個(gè)集公眾號(hào)、小程序、網(wǎng)站(3合1的文章平臺(tái),給您架構(gòu)路上一臂之力)。如果今天的文章對(duì)你在進(jìn)階架構(gòu)師的路上有新的啟發(fā)和進(jìn)步,歡迎轉(zhuǎn)發(fā)給更多人。歡迎加入架構(gòu)師社區(qū)技術(shù)交流群,眾多大咖帶你進(jìn)階架構(gòu)師,在后臺(tái)回復(fù)“加群”即可入群。



          這些年小編給你分享過(guò)的干貨


          1.idea2023.3.4永久激活碼(親測(cè)可用)

          2.優(yōu)質(zhì)ERP系統(tǒng)帶進(jìn)銷存財(cái)務(wù)生產(chǎn)功能(附源碼)

          3.優(yōu)質(zhì)SpringBoot帶工作流管理項(xiàng)目(附源碼)

          4.最好用的OA系統(tǒng),拿來(lái)即用(附源碼)

          5.SBoot+Vue外賣(mài)系統(tǒng)前后端都有(附源碼

          6.SBoot+Vue可視化大屏拖拽項(xiàng)目(附源碼)


          轉(zhuǎn)發(fā)在看就是最大的支持??

          瀏覽 118
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产v在线 | 免费人成视频在线播放 | 欧洲精品成人AV在线蜜芽 | 精品欧美无人区乱码毛片 | 青青草视频在线免费看 |