项目访问量大,频繁调取其他系统接口经常出现项目后台假死现象,发现其他系统掉线重启一段时间必现。查看调用接口,同事直接引用了RestTemplate但是没有设置超时时间->_<-。
两种方式:
1.添加 RestTemplateConfig
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate() {
//配置HTTP超时时间
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(6000);
httpRequestFactory.setConnectTimeout(6000);
httpRequestFactory.setReadTimeout(6000);
return new RestTemplate(httpRequestFactory);
}
}
12345678910111213
- 用上面和其他微服务模块有冲突,重写Bean
@Slf4j
public class LocalHttpUtil {
public static HttpClient httpClient() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
try {
//设置信任ssl访问
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
httpClientBuilder.setSSLContext(sslContext);
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
// 注册http和https请求
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory).build();
//使用Httpclient连接池的方式配置(推荐),同时支持netty,okHttp以及其他http框架
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
// 最大连接数
poolingHttpClientConnectionManager.setMaxTotal(1000);
// 同路由并发数
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
//配置连接池
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
// 重试次数
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, true));
//设置默认请求头
List<Header> headers = new ArrayList<>();
httpClientBuilder.setDefaultHeaders(headers);
return httpClientBuilder.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static ClientHttpRequestFactory clientHttpRequestFactory(int connectTimeOut, int readTimeOut) {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
// 连接超时(毫秒),配置动态设置(秒)
clientHttpRequestFactory.setConnectTimeout(connectTimeOut * 1000);
// 数据读取超时时间(毫秒),配置动态设置(秒)
clientHttpRequestFactory.setReadTimeout(readTimeOut * 1000);
// 从连接池获取请求连接的超时时间(毫秒),不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
clientHttpRequestFactory.setConnectionRequestTimeout(connectTimeOut * 1000);
return clientHttpRequestFactory;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
Service实现类中引用
public RestTemplate restTemplate(){
//创建RestTemplate的时候,指定ClientHttpRequestFactory
return new RestTemplate(LocalHttpUtil.clientHttpRequestFactory(mainConfigProperties.getHttpConnectTimeout(),mainConfigProperties.getHttpReadTimeout()));
}
//调用例子
String restStr = restTemplate.getForObject(url, String.class);
//调用例子 带请求头的请求
//POST请求——参数拼接类型
RestTemplate restTemplate = this.restTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Authorization", Common.BASIC_AUTHORIZATION);
String rawText = "username="+ username +"&password=" +mainConfigProperties.gePsd();
HttpEntity<String> formEntity = new HttpEntity<String>(rawText, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, formEntity, String.class);
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
ResponseEntity<String> responseEntity = null;
if(Common.HTTP_METHOD.GET.equals(httpMethod)){
//GET请求
headers.add("Authorization", queryInfo.getAuthorization());
HttpEntity<String> formEntity = new HttpEntity<String>( headers);
responseEntity = restTemplate.exchange(url, HttpMethod.GET, formEntity, String.class);
}else{
//POST请求——参数JSON类型
HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSON(参数实体).toString(),headers);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", queryInfo.getAuthorization());
responseEntity = restTemplate.exchange(url, HttpMethod.POST, formEntity, String.class);
}
原文链接:https://blog.csdn.net/move_on_on/article/details/126544887
标签:restTemplate,RestTemplate,headers,clientHttpRequestFactory,转发,new,超时 From: https://www.cnblogs.com/zhangruifeng/p/17026687.html