1.Tomcat覆盖默认配置 server: tomcat: max-connections: 2000 accept-count: 100 threads: max: 800 min-spare: 100 max-http-header-size: 131072 2.优化线程池配置 @EnableAsync @Configuration public class AsyncConfig { public static final int CPUNMU = Runtime.getRuntime().availableProcessors(); @Bean(name = "taskExecutor") public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(CPUNMU); executor.setMaxPoolSize(CPUNMU * 25); executor.setQueueCapacity(5000); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("Async-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); return executor; } } 3.增加数据库连接池 @Primary @Bean(name = "dataSource") public DataSource dsDataSource(Environment environment) throws IOException { SQLServerDataSource ds = new SQLServerDataSource(); HikariDataSource ds2 = new HikariDataSource(); ds2.setDataSource(ds); ds2.setMaximumPoolSize(100); ds2.setConnectionTimeout(1800000); ds2.setPoolName("DataSource-"); ds2.setMinimumIdle(20); } catch (Exception e) { logger.error("Config error: {}", e.getMessage()); } return ds2; 4.增加缓存(SpringBoot Cache) // Constant info cache private static Map<String, String> constantCache = new ConcurrentHashMap<>(1024 * 5); // token cache private static Map<String, TokenCache> tokenCache = new ConcurrentHashMap<>(1024 * 5); 5.优化索引 5.增加Pod数量 6.增加Ingress数量 7.升级数据库的DTU 8.第三方API采用多线程调用
创建 completedFuture supplyAsync 无参有返回值 runAsync 无参无返回值 whenComplete thenApply 有参有返回值 thenAccept 有参无返回值 thenRun 无参无返回值 Scheduled job 增加@Async指定线程池,避免获取不到线程等等 @Async("xxxThreadPool")
ExecutorService pool = new ThreadPoolExecutor(0, 4,
20L, TimeUnit.SECONDS,
new SynchronousQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
long start = System.currentTimeMillis();
List<CompletableFuture> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final int t = i;
System.out.println("---" + t);
list.add(CompletableFuture.supplyAsync(() -> {
System.out.println("A- " + t + " - " + Thread.currentThread().getName());
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "A- " + t;
}, pool));
}
CompletableFuture<Void> all = CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()]));
try {
all.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
boolean done = all.isDone();
System.out.println("all done = " + done + " use:" + (System.currentTimeMillis() - start));
System.out.println(LocalDateTime.now());
9.blob文件上传下载采用多线程
10.异步日志
<Loggers> <AsyncLogger name="com.xx.Main" level="trace" additivity="false"> <appender-ref ref="RollingFile"/> </AsyncLogger> <AsyncLogger name="RollingFile2" level="trace" additivity="false"> <appender-ref ref="RollingFile2"/> </AsyncLogger> <Root level="debug"> <AppenderRef ref="Console"/> <AppenderRef ref="RollingFile"/> </Root> </Loggers>
相互学习,共同进步!