1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot中使用異步方法優(yōu)化Service邏輯,提高接口響應(yīng)速度

        共 6004字,需瀏覽 13分鐘

         ·

        2021-11-08 18:45

        來源:blog.csdn.net/weixin_43441509/

        article/details/119855613

        1. 為什么需要異步方法?

        先說結(jié)論: 合理使用異步方法可以讓業(yè)務(wù)接口快到飛起!

        異步方法適用于邏輯與邏輯之間可以相互分割互不影響的業(yè)務(wù)中, 如生成驗證碼和發(fā)送驗證碼組成的業(yè)務(wù), 其實無需等到真正發(fā)送成功驗證碼才對客戶端進行響應(yīng), 可以讓短信發(fā)送這一耗時操作轉(zhuǎn)為異步執(zhí)行, 解耦耗時操作和核心業(yè)務(wù);

        同理還有文章閱讀的業(yè)務(wù)邏輯 = 查詢文章詳情 + 更新文章閱讀量后再響應(yīng)客戶端, 其實也無需等到閱讀量更新后才響應(yīng)文章詳情給客戶端, 用戶查看文章是主要邏輯, 而文章閱讀量更新是次要邏輯, 況且閱讀量就算更新失敗一點數(shù)據(jù)偏差也不會影響用戶閱讀因此這兩個數(shù)據(jù)庫操作之間的一致性是較弱的, 這類都能用異步事件去優(yōu)化.

        所以說: 恰當(dāng)?shù)脑谖覀兊腟ervice中加入異步方法能大大提高接口的響應(yīng)速度, 提升用戶體驗!

        同步執(zhí)行(同在一個線程中):

        異步執(zhí)行(開啟額外線程來執(zhí)行):

        2. SpringBoot中的異步方法支持

        其實, 在SpringBoot中并不需要我們自己去創(chuàng)建維護線程或者線程池來異步的執(zhí)行方法, SpringBoot已經(jīng)提供了異步方法支持注解.

        @EnableAsync?//?使用異步方法時需要提前開啟(在啟動類上或配置類上)
        @Async?//?被async注解修飾的方法由SpringBoot默認線程池(SimpleAsyncTaskExecutor)執(zhí)行

        比如使用Spring的異步支持實現(xiàn)文章查詢并增加閱讀量

        Service層:

        @Service
        public?class?ArticleServiceImpl?{

        ????//?查詢文章
        ????public?String?selectArticle()?{
        ????????//?TODO?模擬文章查詢操作
        ????????System.out.println("查詢?nèi)蝿?wù)線程"+Thread.currentThread().getName());
        ????????return?"文章詳情";
        ????}

        ????//?文章閱讀量+1
        ????@Async
        ????public?void?updateReadCount()?{
        ????????//?TODO?模擬耗時操作
        ????????try?{
        ????????????Thread.sleep(3000);
        ????????}?catch?(InterruptedException?e)?{
        ????????????e.printStackTrace();
        ????????}
        ????????System.out.println("更新任務(wù)線程"+Thread.currentThread().getName());
        ????}
        }

        Controller層:

        @RestController
        public?class?AsyncTestController?{

        ????@Autowired
        ????private?ArticleServiceImpl?articleService;

        ????/**
        ?????*?模擬獲取文章后閱讀量+1
        ?????*/

        ????@PostMapping("/article")
        ????public?String?getArticle()?{
        ????????//?查詢文章
        ????????String?article?=?articleService.selectArticle();
        ????????//?閱讀量+1
        ????????articleService.updateReadCount();
        ????????System.out.println("文章閱讀業(yè)務(wù)執(zhí)行完畢");
        ????????return?article;
        ????}

        }

        測試結(jié)果: 我們可以感受到接口響應(yīng)速度大大提升, 而且從日志中key看到兩個執(zhí)行任務(wù)是在不同的線程中執(zhí)行的

        3. 自定義線程池執(zhí)行異步方法

        SpringBoot為我們默認提供了線程池(SimpleAsyncTaskExecutor)來執(zhí)行我們的異步方法, 我們也可以自定義自己的線程池.

        第一步配置自定義線程池

        @EnableAsync?//?開啟多線程,?項目啟動時自動創(chuàng)建
        @Configuration
        public?class?AsyncConfig?{
        ????@Bean("customExecutor")
        ????public?ThreadPoolTaskExecutor?asyncOperationExecutor()?{
        ????????ThreadPoolTaskExecutor?executor?=?new?ThreadPoolTaskExecutor();
        ????????//?設(shè)置核心線程數(shù)
        ????????executor.setCorePoolSize(8);
        ????????//?設(shè)置最大線程數(shù)
        ????????executor.setMaxPoolSize(20);
        ????????//?設(shè)置隊列大小
        ????????executor.setQueueCapacity(Integer.MAX_VALUE);
        ????????//?設(shè)置線程活躍時間(秒)
        ????????executor.setKeepAliveSeconds(60);
        ????????//?設(shè)置線程名前綴+分組名稱
        ????????executor.setThreadNamePrefix("AsyncOperationThread-");
        ????????executor.setThreadGroupName("AsyncOperationGroup");
        ????????//?所有任務(wù)結(jié)束后關(guān)閉線程池
        ????????executor.setWaitForTasksToCompleteOnShutdown(true);
        ????????//?初始化
        ????????executor.initialize();
        ????????return?executor;
        ????}
        }

        第二步, 在@Async注解上指定執(zhí)行的線程池即可

        //?文章閱讀量+1
        @Async("customExecutor")
        public?void?updateReadCount()?{
        ????//?TODO?模擬耗時操作
        ????try?{
        ????????Thread.sleep(3000);
        ????}?catch?(InterruptedException?e)?{
        ????????e.printStackTrace();
        ????}
        ????System.out.println("更新文章閱讀量線程"+Thread.currentThread().getName());
        }

        5. 如何捕獲(無返回值的)異步方法中的異常

        以實現(xiàn)AsyncConfigurer接口的getAsyncExecutor方法和getAsyncUncaughtExceptionHandler方法改造配置類

        自定義異常處理類CustomAsyncExceptionHandler

        @EnableAsync?//?開啟多線程,?項目啟動時自動創(chuàng)建
        @Configuration
        public?class?AsyncConfig?implements?AsyncConfigurer?{
        ????@Override
        ????public?Executor?getAsyncExecutor()?{
        ????????ThreadPoolTaskExecutor?executor?=?new?ThreadPoolTaskExecutor();
        ????????//?設(shè)置核心線程數(shù)
        ????????executor.setCorePoolSize(8);
        ????????//?設(shè)置最大線程數(shù)
        ????????executor.setMaxPoolSize(20);
        ????????//?設(shè)置隊列大小
        ????????executor.setQueueCapacity(Integer.MAX_VALUE);
        ????????//?設(shè)置線程活躍時間(秒)
        ????????executor.setKeepAliveSeconds(60);
        ????????//?設(shè)置線程名前綴+分組名稱
        ????????executor.setThreadNamePrefix("AsyncOperationThread-");
        ????????executor.setThreadGroupName("AsyncOperationGroup");
        ????????//?所有任務(wù)結(jié)束后關(guān)閉線程池
        ????????executor.setWaitForTasksToCompleteOnShutdown(true);
        ????????//?初始化
        ????????executor.initialize();
        ????????return?executor;
        ????}

        ????@Override
        ????public?AsyncUncaughtExceptionHandler?getAsyncUncaughtExceptionHandler()?{
        ????????return?new?CustomAsyncExceptionHandler();
        ????}
        }
        public?class?CustomAsyncExceptionHandler?implements?AsyncUncaughtExceptionHandler?{
        ?
        ????@Override
        ????public?void?handleUncaughtException(Throwable?throwable,?Method?method,?Object...?obj)?{
        ????????System.out.println("異常捕獲---------------------------------");
        ????????System.out.println("Exception?message?-?"?+?throwable.getMessage());
        ????????System.out.println("Method?name?-?"?+?method.getName());
        ????????for?(Object?param?:?obj)?{
        ????????????System.out.println("Parameter?value?-?"?+?param);
        ????????}
        ????????System.out.println("異常捕獲---------------------------------");
        ????}
        ?????
        }

        5. 如何獲取(有返回值)異步方法的返回值

        使用Future類及其子類來接收異步方法返回值

        注意:

        • 無返回值的異步方法拋出異常不會影響Controller的主要業(yè)務(wù)邏輯
        • 有返回值的異步方法拋出異常會影響Controller的主要業(yè)務(wù)邏輯
        //?異步方法---------------------------------------------------------------------
        @Async
        ????public?CompletableFuture?updateReadCountHasResult()?{
        ????????//?TODO?模擬耗時操作
        ????????try?{
        ????????????Thread.sleep(3000);
        ????????}?catch?(InterruptedException?e)?{
        ????????????e.printStackTrace();
        ????????}
        ????????System.out.println("更新文章閱讀量線程"+Thread.currentThread().getName());
        ????????return?CompletableFuture.completedFuture(100?+?1);
        ????}

        //?Controller調(diào)用---------------------------------------------------------------------
        @GetMapping("/article")
        public?String?getArticle()?throws?ExecutionException,?InterruptedException?{
        ????//?查詢文章
        ????String?article?=?articleService.selectArticle();
        ????//?閱讀量+1
        ????CompletableFuture?future?=?articleService.updateReadCountHasResult();
        ????int?count?=?0;
        ????//?循環(huán)等待異步請求結(jié)果
        ????while?(true)?{
        ????????if(future.isCancelled())?{
        ????????????System.out.println("異步任務(wù)取消");
        ????????????break;
        ????????}
        ????????if?(future.isDone())?{
        ????????????count?=?future.get();
        ????????????System.out.println(count);
        ????????????break;
        ????????}
        ????}
        ????System.out.println("文章閱讀業(yè)務(wù)執(zhí)行完畢");
        ????return?article?+?count;
        }

        6. 異步方法帶來的問題/拓展

        • 異步方法只能聲明在Service方法中在Controller直接調(diào)用才會生效, 異步方法被同級Service方法調(diào)用不會生效, 很奇怪?
        • 異步方法 + 事務(wù)能順利執(zhí)行嗎? 或許事務(wù)操作應(yīng)該和異步操作分離開, 被Controller層調(diào)用時事務(wù)操作在前, 異步操作在后
        • 異步方法執(zhí)行失敗后對Controller前半部分的非異步操作無影響, 因此說異步方法在整個業(yè)務(wù)邏輯中不是100%可靠的, 對于強一致性的業(yè)務(wù)來說不適用
        • 還是消息中間件更為強大, RabbitMQ, Kafka…
        瀏覽 56
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            影音先锋偷拍自拍 | 成人午夜激情视频 | 少妇大战黑人46厘米 | 亚洲一级a免费在线观看 | 国产黄色视频在线观看 | 最新中文字幕MV第三季歌词完整版 | 国产精品久久久久久久裸模 | 国产三点都露的大尺度电影 | 日韩三级视频 | 男女洗澡视频网站 |