继上一篇异步操作优化IO 密集型API 接口,这一篇将会加上自定义线程池和对线程池的监控,用到springboot的actuator/health。前面提到用CompletableFuture来优化接口,其实CompletableFuture内部自己维护了一个线程池,这个线程池的个数是电脑核心数-1,显然这个并不适合IO密集型的API。原因是IO密集型的API有很多IO的操作,比如call DB,call third API等等,最大的瓶颈就是IO的等待时间,这是要把尽可能多的请求发出去,然后我们API就等待返回结果,所以我们得自己配置一个线程,来使得我们的API的性能进一步提升。actuator默认大家都会配置哈。太简单了,不会问度娘也行。
1,线程池的配置
@Configuration
@EnableAsync
public class ExecutorConfig {
@Bean(name = "asyncServiceExecutor")
public ThreadPoolTaskExecutor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
executor.setCorePoolSize(25);
executor.setMaxPoolSize(25);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix("eb-core-thread-po