美文网首页
Http服务器设置超时时间

Http服务器设置超时时间

作者: 等你足够强了再说吧 | 来源:发表于2022-05-13 17:12 被阅读0次

    一.实现功能
    解释三者的connectionRequestTimeout 、connectionTimeout、socketTimeout代表意义,进而了解三者的区别.

    二.具体区别
    1.connectionRequestTimout(单位是ms):指从连接池获取连接的timeout超出预设时间(

    从连接池获取连接的超时时间,如果连接池里连接都被用了,且超过设定时间,就会报错connectionrequesttimeout,会抛出超时异常.

    2.connetionTimeout(单位是ms):指客户端和服务器建立连接的timeout.

    就是http请求的三个阶段,一:建立连接;二:数据传送;三,断开连接。如果与服务器(这里指数据库)请求建立连接的时间超过ConnectionTimeOut,就会抛 ConnectionTimeOutException,即服务器连接超时,没有在规定的时间内建立连接。

    3.socketTimeout(单位是ms):指客户端从服务器读取数据的timeout超出预期设定时间,超出后会抛出SocketTimeOutException.

    public class RequestConfig implements Cloneable {
     
        /**
         * Returns the timeout in milliseconds used when requesting a connection
         * from the connection manager. A timeout value of zero is interpreted
         * as an infinite timeout.
         * <p>
         * A timeout value of zero is interpreted as an infinite timeout.
         * A negative value is interpreted as undefined (system default).
         * </p>
         * <p>
         * Default: {@code -1}
         * </p>
         */
        public int getConnectionRequestTimeout() {
            return connectionRequestTimeout;
        }
     
        /**
         * Determines the timeout in milliseconds until a connection is established.
         * A timeout value of zero is interpreted as an infinite timeout.
         * <p>
         * A timeout value of zero is interpreted as an infinite timeout.
         * A negative value is interpreted as undefined (system default).
         * </p>
         * <p>
         * Default: {@code -1}
         * </p>
         */
        public int getConnectTimeout() {
            return connectTimeout;
        }
     
        /**
         * Defines the socket timeout ({@code SO_TIMEOUT}) in milliseconds,
         * which is the timeout for waiting for data  or, put differently,
         * a maximum period inactivity between two consecutive data packets).
         * <p>
         * A timeout value of zero is interpreted as an infinite timeout.
         * A negative value is interpreted as undefined (system default).
         * </p>
         * <p>
         * Default: {@code -1}
         * </p>
         */
        public int getSocketTimeout() {
            return socketTimeout;
        }
    }
    
    

    示例:

    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(30000)
    .build();
    
    
    
    private static final PoolingHttpClientConnectionManager POOL_CONN_MANAGER =
          new PoolingHttpClientConnectionManager();
    private static final RequestConfig REQ_CFG;   
     
    POOL_CONN_MANAGER.setMaxTotal(500);
    POOL_CONN_MANAGER.setDefaultMaxPerRoute(POOL_CONN_MANAGER.getMaxTotal());
     
    REQ_CFG =
            RequestConfig.custom()
                // 请求连接超时时间
                .setConnectTimeout(100 * 1000)
                // 获取数据超时时间
                .setSocketTimeout(360 * 1000)
                // 从链接池获取连接超时时间
                .setConnectionRequestTimeout(5000)
                .build();
     
     
    CloseableHttpClient client=HttpClients.custom()
            .setConnectionManager(POOL_CONN_MANAGER)
            .setDefaultRequestConfig(REQ_CFG)
            .build();
    

    1.RestTemplate超时

    设置配置HttpComponentsClientHttpRequestFactory中的RequestConfig属性

    RestTemplateConfig:

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
     
    /**
     * Created by qhong on 2018/9/10 17:53
     **/
    @Slf4j
    @Configuration
    public class RestTemplateConfig {
     
        @Bean
        @ConfigurationProperties(prefix = "rest.connection")
        public HttpComponentsClientHttpRequestFactory httpRequestFactory() {
            return new HttpComponentsClientHttpRequestFactory();
        }
     
        @Bean
        public RestTemplate customRestTemplate(){
            return new RestTemplate(httpRequestFactory());
        }
    }
    

    application.properties:

    #restTemplate配置
    rest.connection.connectionRequestTimeout=30000
    rest.connection.connectTimeout=30000
    rest.connection.readTimeout=30000
    

    #restTemplate配置
    rest.connection.connection-request-timeout=30000
    rest.connection.connect-timeout=30000
    rest.connection.read-timeout=30000
    

    spring.mvc.async.request-timeout

    spring.mvc.async.request-timeout=20000,意思是设置超时时间为20000ms即20s
    

    相关文章

      网友评论

          本文标题:Http服务器设置超时时间

          本文链接:https://www.haomeiwen.com/subject/mykuurtx.html