connection pool shutdown错误修改

近几天在做一个项目时,遇到一个问题:前端如果在短时间内(2秒内)有多个相同的请求发到后台,后台再去通过restTemplate发送请求给其他服务的时候,会报错“connetion pool shutdown”。

通过查阅资料,大概是因为由于rest连接过于频繁,导致上一个获取不到连接。具体原因需要进一步分析。

stackoverflow:

1:

try to change your code like this:

HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(true).build();

the setConnectionManagerShared will defines the connection manager is to be shared by multiple client instances.

2:

This behavior is due to a bug in HC 4.3. It has already been fixed in HC 4.4a1. As of 4.4 CloseableHttpClient#close should automatically shut down the connection pool only if exclusively owned by the client

解决方式:

第一步:将httpclient升级至4.4及以上版本,我的环境升级到了4.5.5.

第二步:httpclient配置共享连接池,供剩下打开的连接去请求。

client = HttpClients.custom()
                .setConnectionManager(ccm)
                .setDefaultCredentialsProvider(credentialsProvider)
                .setDefaultRequestConfig(defaultRequestConfig)
                .setConnectionManagerShared(true) //设置共享连接池
                .build();
经验分享 程序员 微信小程序 职场和发展