springboot优雅shutdown时如何保障异步线程的安全

我前面写了一篇springboot优雅shutdown的文章,看起来一切很美好。
https://blog.csdn.net/chenshm/article/details/139640775
那是因为没有进行多线程测试。如果一个请求中包括阻塞线程(主线程)和非阻塞线程(异步线程),会是什么效果?接下来我们就测试一番。

1. 验证优雅shutdown的异步线程安全性

  • 确认graceful shutdown配置

graceful shutdown config
查看源码可以看到springboot graceful shutdown默认只会等待30s,我这里设置更长的时间只是方便测试,实际设置还是需要根据你业务api最长执行时间来配置。

graceful shutdown default timeout setting

  • 准备测试代码
@Slf4j
@RestController
@RequestMapping("/api")
public class DemoController {

    @GetMapping("/{userId}")
    public ResultVo<Object> getUserInfo(@PathVariable String userId) throws InterruptedException {
        log.info("userId:{}", userId);
        Runnable runnable = () -> {
            for (int i = 0; i < 60; i++) {
                log.info("async thread to update user login info to other services, service num: {}", i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        for (int i = 0; i < 30; i++) {
            log.info("querying user info for {}, waiting times: {}", userId, i);
            Thread.sleep(1000);
        }
        return ResultVo.ok();
    }
}

这里我设置非阻塞线程的循环是60次,大概60s完成,阻塞线程循环只有30次,大概30s完成。主要是为了测试我的阻塞线程完成后,graceful shutdown能不能保证我的异步线程安全。

  • 请求api
Administrator@USER-20230930SH MINGW64 /d/git/micro-service-logs-tracing
$ curl http://localhost:8080/api/sandwich
  • shutdown app(Ctrl+F2)
  • 查看日志
    shutdown日志

可以看到shutdown信号发出之后,两个线程都还在跑,但是阻塞线程(0-29)结束之后,异步线程也跟着终结了。它的循环应该是从0到59才算结束,但是只跑到30,所以异步线程是不安全的。

  • 验证主线程返回结果
    阻塞线程还是安全的,response正常返回了。
    api response correctly but the async thread not yet finished

其实这种测试方法并不局限于解决springboot的问题,其他微服务也是类似的。过去我看到一些朋友测试release的安全性,只是不断call health api,只要release 期间health api没有返回异常就当作ok了,其实这只能验证你的负载均衡服务的可靠性,你自己app的安全问题还是没有得到解决。
既然问题找到了,接下来我来解决它。

2. 确保优雅shutdown app时异步线程也安全

2.1 优化代码

前面的异步线程只是简单地写个野线程,并不规范,我先优化一下。

  • 把野线程放到线程池执行;
  • 利用mbean的PreDestroy来在servcie销毁前先等待异步线程完成;
  • 利用ExecutorService 的awaitTermination方法预判断异步线程的最长等待时间,等待异步线程完成,如果线程没有按时完成再强制结束。
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {

    private final ExecutorService executorService;

    public AsyncServiceImpl() {
        this.executorService = Executors.newFixedThreadPool(10);
    }

    @Override
    public void feedUserInfoToOtherServices(String userId) {
        executorService.execute(() -> {
            for (int i = 0; i < 35; i++) {
                log.info("async thread to update {} login info to other services, service num: {}", userId, i+1);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    @PreDestroy
    public void tearDown() {
        if (null != executorService) {
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(50, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                log.info("PreDestroy executorService is interrupted", e);
                executorService.shutdownNow();
            }
        }
    }
}

api代码调整如下

@Slf4j
@RestController
@RequestMapping("/api")
public class DemoController {

    @Resource
    AsyncService asyncService;

    @GetMapping("/{userId}")
    public ResultVo<Object> getUserInfo(@PathVariable String userId) throws InterruptedException {
        log.info("userId:{}", userId);
        asyncService.feedUserInfoToOtherServices(userId);
        for (int i = 0; i < 30; i++) {
            log.info("updating user info for {}, waiting times: {}", userId, i+1);
            Thread.sleep(1000);
        }
        return ResultVo.ok();
    }
}

2.2 验证shutdown过程异步线程的安全

从新代码看来,我们期待的结果是一个api请求,主线程循环从1到30,异步线程是从1到35,主线程先完成,异步线程会在AsyncServiceImpl servcie bean销毁前先等待异步线程完成。接下来是验证步骤。

  • 重启服务
  • call api
Administrator@USER-20230930SH MINGW64 /d/git/micro-service-logs-tracing
$ curl http://localhost:8080/api/sandwich
  • shutdown app(Ctrl + F2)
  • 查看日志
    异步线程安全退出日志

分析日志发现一切如代码所料,app graceful shutdown的时候,异步线程的安全性得到保障。
这个过程看起来非常完美,其实还不够完美,解决方案没最好,只有更好。请先关注我,容我研究一下,下期告诉你为什么。

相关推荐

  1. SpringBoot 线异步调用

    2024-06-16 09:06:03       43 阅读
  2. Qt如何保证控件调用时候线安全

    2024-06-16 09:06:03       40 阅读
  3. 分布式集群如何保证线安全

    2024-06-16 09:06:03       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 09:06:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 09:06:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 09:06:03       82 阅读
  4. Python语言-面向对象

    2024-06-16 09:06:03       91 阅读

热门阅读

  1. 面试题——Redis

    2024-06-16 09:06:03       29 阅读
  2. RPC协议

    RPC协议

    2024-06-16 09:06:03      38 阅读
  3. 【对话型应用 API详细介绍】

    2024-06-16 09:06:03       33 阅读
  4. HTML 颜色名:网页设计的调色板

    2024-06-16 09:06:03       29 阅读
  5. SQL RIGHT JOIN 详解

    2024-06-16 09:06:03       38 阅读
  6. 医疗图像自动轮廓勾画

    2024-06-16 09:06:03       31 阅读
  7. 大数据开发语言Scala入门:新手小白学习指南

    2024-06-16 09:06:03       34 阅读
  8. git如何将本地仓库的代码提交到远程仓库

    2024-06-16 09:06:03       33 阅读
  9. 了解版本管理系统

    2024-06-16 09:06:03       55 阅读
  10. 如何使用React的state来管理组件的内部状态?

    2024-06-16 09:06:03       26 阅读
  11. Flink 容错

    2024-06-16 09:06:03       38 阅读
  12. LeetCode 189.轮转数组

    2024-06-16 09:06:03       39 阅读