In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the way to integrate springboot restTemplate connection pool". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the integration mode of springboot restTemplate connection pool?"
Catalogue
Springboot restTemplate connection pooling consolidation
RestTemplate
Introduction of apache httpclient
RestTemplate configuration class
RestTemplate connection pool configuration parameters
Test RestTemplate with connection pool
RestTemplate configure http connection pooling
Springboot restTemplate connection pooling integrates restTemplate
Using http connection pooling can reduce the time of connection establishment and release, and improve the performance of http requests. If the client has to establish a new connection with the server for each request, the three-way handshake will be very time-consuming. This paper introduces how to integrate http connection pool in Springboot and implement it based on restTemplate+httpclient.
Introduction of apache httpclient org.apache.httpcomponents httpclient 4.5.6 RestTemplate configuration class import org.apache.http.client.HttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.ClientHttpRequestFactory;import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.http.client.SimpleClientHttpRequestFactory Import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.StringHttpMessageConverter;import org.springframework.web.client.RestTemplate;import java.nio.charset.Charset;import java.util.List;import java.util.concurrent.TimeUnit / * in actual development, it is necessary to avoid instantiating httpclient every http request * restTemplate reuses connections by default, ensuring that restTemplate singletons can be * referenced: * https://www.cnblogs.com/xrq730/p/10963689.html * https://halfrost.com/advance_tcp/ * / @ Configurationpublic class RestTemplateConfig {@ Bean RestTemplate restTemplate (ClientHttpRequestFactory clientHttpRequestFactory) {RestTemplate restTemplate = new RestTemplate (clientHttpRequestFactory); List > messageConverters = restTemplate.getMessageConverters () Iterator converter = iterator.next (); if (converter instanceof StringHttpMessageConverter) {iterator.remove ();}} messageConverters.add (new StringHttpMessageConverter (Charset.forName ("UTF-8")); return restTemplate;} @ Bean public HttpClientConnectionManager poolingConnectionManager () {PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager (); poolingConnectionManager.setMaxTotal (1000) / / maximum number of connections in connection pool poolingConnectionManager.setDefaultMaxPerRoute; / / concurrent return poolingConnectionManager;} @ Bean public HttpClientBuilder httpClientBuilder () per host {HttpClientBuilder httpClientBuilder = HttpClientBuilder.create (); / / set HTTP connection manager httpClientBuilder.setConnectionManager (poolingConnectionManager ()); return httpClientBuilder } @ Bean public ClientHttpRequestFactory clientHttpRequestFactory () {HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory (); clientHttpRequestFactory.setHttpClient (httpClientBuilder (). Build ()); clientHttpRequestFactory.setConnectTimeout (6000); / / connection timeout, millisecond clientHttpRequestFactory.setReadTimeout (6000); / / read and write timeout, millisecond return clientHttpRequestFactory }} at this point, I believe you have a deeper understanding of "what is the integration mode of springboot restTemplate connection pool". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.