Spingboot Resttemplate connection pool configuration

Spingboot Resttemplate connection pool configuration

  1. Why integrate HttpClient
    RestTemplate is a client that calls rest services in Spring. It provides a variety of convenient methods for accessing remote Http services, which can greatly improve the efficiency of client writing.
    RestTemplate uses the JDK native URLConnection by default, and the default timeout is -1, which means that there is no timeout period. This certainly cannot meet the needs of complex situations. The restTemplate factory supports the use of HttpClient and OkHttp as client implementations.

  2. Why use a connection pool?
    When calling a rest request, each request needs to establish a connection with the server, which is a three-way handshake. This is a time-consuming and laborious work. If we need to call a server frequently, do we need to keep going? Is the connection established?
    So using the connection pool can avoid multiple connection establishment operations and save resources.

package com.jshhxx.framework.config;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
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.web.client.RestTemplate;
/**
 * Restemplate 连接池配置
 *
 * */
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(httpRequestFactory());
    }

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {

        return new HttpComponentsClientHttpRequestFactory(httpClient());

    }

    /**
     * http链接池
     * 服务器返回数据(response)的时间,超过该时间抛出read timeout
     * @return
     */
    @Bean
    public HttpClient httpClient() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        //设置整个连接池最大连接数 根据自己的场景决定
        connectionManager.setMaxTotal(2000);
        //路由是对maxTotal的细分
        connectionManager.setDefaultMaxPerRoute(100);
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(100000) //服务器返回数据(response)的时间,超过该时间抛出read timeout
                .setConnectTimeout(50000)//连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
                .setConnectionRequestTimeout(10000)//从连接池中获取连接的超时时间,超过该时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
                .build();
        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }
}

Guess you like

Origin blog.csdn.net/qq_31142237/article/details/108828227