SpringBoot 性能优化




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 constantCache = new ConcurrentHashMap<>(1024 * 5); // token cache private static Map 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")

9.blob文件上传下载采用多线程

10.异步日志