zoukankan      html  css  js  c++  java
  • springboot RestTemplate连接池配置

    package com.chinaunicom.nvr.area.controller;
    
    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.http.client.ClientHttpRequestFactory;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    public class RestTemplateConfig {
    
    
        /**
         *
         * @return
         */
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate(httpRequestFactory());
        }
    
        /**
         *
         * @return
         */
        @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(200);
            //路由是对maxTotal的细分
            connectionManager.setDefaultMaxPerRoute(100);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(10000) //服务器返回数据(response)的时间,超过该时间抛出read timeout
                    .setConnectTimeout(5000)//连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
                    .setConnectionRequestTimeout(1000)//从连接池中获取连接的超时时间,超过该时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
                    .build();
            return HttpClientBuilder.create()
                    .setDefaultRequestConfig(requestConfig)
                    .setConnectionManager(connectionManager)
                    .build();
        }
    }
    

      

  • 相关阅读:
    Windows下做7层软负载方案分析
    蛙蛙推荐:C语言入门之二——编写第一个有意义的小程序
    蛙蛙推荐:C语言入门之一——Linux下的C开发环境搭建小节
    蛙蛙推荐:动手做个网页游戏五子棋
    学习园地:微博项目
    com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 异常 规格严格
    PostgreSQL Update 根据B表更新A表 规格严格
    SQL 删除重复数据[转] 规格严格
    ntoskrnl.exe占用cpu高 规格严格
    PostgreSQL中RECURSIVE递归查询使用总结[转] 规格严格
  • 原文地址:https://www.cnblogs.com/zxf330301/p/13158883.html
Copyright © 2011-2022 走看看