使用 Hikari 池的 Spring 应用程序。
现在,对于来自客户端的单个请求,我必须查询 10 个表(业务需要),然后将结果组合在一起。而查询每个表可能要花费50ms到200ms。为了加快响应时间,我FixedThreadPool在服务中创建一个来查询不同线程中的每个表(伪代码):
class MyService{
final int THREAD_POOL_SIZE = 20;
final int CONNECTION_POOL_SIZE = 10;
final ExecutorService pool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
protected DataSource ds;
MyClass(){
Class.forName(getJdbcDriverName());
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(CONNECTION_POOL_SIZE);
ds = new HikariDataSource(config);
}
public Items doQuery(){
String[] tables=["a","b"......]; //10+ tables
Items result=new Items();
CompletionService<Items> executorService = new ExecutorCompletionService<Items>(pool);
for (String tb : tables) {
Callable<Item> c = () -> {
Items items = ds.getConnection().query(tb); ......
return Items;
};
executorService.submit(c);
}
for (String tb: tables) {
final Future<Items> future = executorService.take();
Items items = future.get();
result.addAll(items);
}
}
}
现在对于单个请求,平均响应时间可能是 500ms。
但对于并发请求,平均响应时间会迅速增加,请求越多,响应时间就越长。
我想知道如何设置正确的连接池大小和线程池大小以使应用程序有效工作?
顺便说一句,数据库在云中使用 RDS,具有 4 个 cpu 16GB 内存,最大连接数为 2000,最大 IOPS 为 8000。
DIEA
慕的地10843
守着一只汪
相关分类