压力测试时,每次接口调用均启动一个线程,依次累加,所有线程均不释放,线程状态均为阻塞
_thread_blocked:线程处于阻塞状态
经过排查,发现发送请求的工具类,在发送请求时创建线程,但是在请求完成后并未回收线程,导致线程只创建,不回收,最终导致进程挂掉。
将使用的RestTemplate 类改为Spring管理并配置超时时间,使用时从IOC容器中获取,这时创建的连接在使用完成后,会被回收。配置如下:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//ms
factory.setConnectTimeout(15000);//ms
return factory;
}
}