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>