当前位置:网站首页>RestTemplate 连接池配置
RestTemplate 连接池配置
2022-07-27 06:55:00 【white_while】
通过 RestTemplateBuilder 的方式生成 RestTemplate
@Bean(name = "template")
RestTemplate template(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
//单位s, 设置连接时间,避免线程过多被阻塞挂起
.setConnectTimeout(Duration.ofSeconds(1))
.setReadTimeout(Duration.ofSeconds(1))
.additionalMessageConverters(new MyMappingJackson2HttpMessageConverter())
.requestFactory(() -> {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
// 连接池
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//设置HTTP连接管理器
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(500); // 连接池最大连接数
poolingConnectionManager.setDefaultMaxPerRoute(200); // 每个主机的并发
httpClientBuilder.setConnectionManager(poolingConnectionManager);
// 设置连接池
clientHttpRequestFactory.setHttpClient(httpClientBuilder.build());
return clientHttpRequestFactory;
})
.build();
}
restTemplate的连接由 RequestFactory 创建
HttpComponentsClientHttpRequestFactory 是 RequestFactory 实现类,连接池化,有 httpClient 引用
RestTemplateBuilder 用于生成 restTeamplte
configure 方法初始化了 RequestFactory 对象通过 buildRequestFactory
public ClientHttpRequestFactory buildRequestFactory() {
ClientHttpRequestFactory requestFactory = null;
if (this.requestFactory != null) {
requestFactory = (ClientHttpRequestFactory)this.requestFactory.get();
} else if (this.detectRequestFactory) {
// 默认连接池工厂
requestFactory = (new ClientHttpRequestFactorySupplier()).get();
}
if (requestFactory != null && this.requestFactoryCustomizer != null) {
this.requestFactoryCustomizer.accept(requestFactory);
}
return requestFactory;
}
边栏推荐
- TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)
- Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
- JS regular expression implementation adds a comma to every three digits
- sql语句批量更新 时间减去1天
- 杂谈:高考
- A small cotton padded jacket with air leakage
- C winfrom common function integration-2
- Cadence(十一)丝印调整和后续事项
- Panabit SNMP配置
- 杂谈:最近好多朋友谈出国……
猜你喜欢

flink1.14 sql基础语法(一) flink sql表查询详解

Mysql: increase the maximum number of connections

Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk

User unlock sm04 sm12

连接MySQL时报错:Public Key Retrieval is not allowed 【解决方法】

在kettle中快速更新一个字段中的信息

闭散列和开散列解决哈希冲突

Regular expression foundation sorting
![Error when connecting to MySQL: public key retrieval is not allowed [solution]](/img/b3/41523d44924ec203e40453bace6627.png)
Error when connecting to MySQL: public key retrieval is not allowed [solution]

基于Arduino的温度、湿度测量显示装置
随机推荐
Plato farm is expected to further expand its ecosystem through elephant swap
Guava的基础功能与集合
drawImage方法第一次调用不显示图片的解决方式
单臂路由(讲解+实验)
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
Gossip: Recently, many friends talk about going abroad
电子量产项目框架--基本思想
Pg_ relation_ Size question
VLAN trunk实验
flink去重(一)flink、flink-sql中常见的去重方案总结
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
在rhel7.3中编译和使用log4cxx
Systematic explanation of unit testing: mockito
Bash: create a function that returns a Boolean value
在mac中使用docker来搭建oracle数据库服务器
shell企业面试题练习
Quickly update the information in a field in kettle
STM32_ Find the cause of entering hardfault_ Handler's function
Flynk de duplication (2) solve the hot issues in the process of flynk and flynk SQL de duplication
(2022 Hangdian multi school III) 1009.package delivery (greedy)