Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the integration method of springboot restTemplate connection pool?

2025-02-28 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report