RestTemplateConfig

【自取】最近整理的,有需要可以领取学习:

ConnPoolControl

对于链接池的配置,可以看到源代码主要在这个界面中。

[En]

For the configuration of the link pool, you can see that the source code is mainly in this interface.

package org.apache.http.pool;
/**
 * Interface to control runtime properties of a {@link ConnPool} such as
 * maximum total number of connections or maximum connections per route
 * allowed.

 *
 * @param  the route type that represents the opposite endpoint of a pooled
 *   connection.

 * @since 4.2
 */
public interface ConnPoolControl<T> {
    void setMaxTotal(int max);
    int getMaxTotal();
    void setDefaultMaxPerRoute(int max);
    int getDefaultMaxPerRoute();
    void setMaxPerRoute(final T route, int max);
    int getMaxPerRoute(final T route);
    PoolStats getTotalStats();
    PoolStats getStats(final T route);
}

配置项

RestTemplateConfig
  • 上面提到的路由指的是按主机对MaxTotal进行再分配。比如:MaxtTotal=400,DefaultMaxPerRoute=200,而服务只连接到https://g.cn时,到这个主机的并发最多只有200;而不是400;而我连接到https://baidu.comhttps://g.cn时,到每个主机的并发最多只有200;即加起来是400(但不能超过400)
  • 而DefaultMaxPerRoute与MaxPerRoute的区别是:前者是所有路由的默认值,后者是可以单独为特定的路由进行配置。

SocketConfig

package org.apache.http.config;

/**
 * Socket configuration.

 *
 * @since 4.3
 */
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class SocketConfig implements Cloneable {
    public static final SocketConfig DEFAULT = new Builder().build();
    private final int soTimeout;
    private final boolean soReuseAddress;
    private final int soLinger;
    private final boolean soKeepAlive;
    private final boolean tcpNoDelay;
    private final int sndBufSize;
    private final int rcvBufSize;
    private final int backlogSize;
}

RestTemplateConfig

RequestConfig

对于请求的配置,您可以看到源代码主要在这个界面中。

[En]

For the requested configuration, you can see that the source code is mainly in this interface.

package org.apache.http.client.config;
/**
 *  Immutable class encapsulating request configuration items.

 *  The default setting for stale connection checking changed
 *  to false, and the feature was deprecated starting with version 4.4.

 */
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class RequestConfig implements Cloneable {
    public static final RequestConfig DEFAULT = new Builder().build();
    private final boolean expectContinueEnabled;
    private final HttpHost proxy;
    private final InetAddress localAddress;
    private final boolean staleConnectionCheckEnabled;
    private final String cookieSpec;
    private final boolean redirectsEnabled;
    private final boolean relativeRedirectsAllowed;
    private final boolean circularRedirectsAllowed;
    private final int maxRedirects;
    private final boolean authenticationEnabled;
    private final Collection<String> targetPreferredAuthSchemes;
    private final Collection<String> proxyPreferredAuthSchemes;
    private final int connectionRequestTimeout;
    private final int connectTimeout;
    private final int socketTimeout;
    private final boolean contentCompressionEnabled;
}

配置项

RestTemplateConfig

优化建议

上述配置项并非全部使用,常用配置如下:

[En]

Not all the configuration items described above are used, and the commonly used configurations are as follows:

  • MaxTotal——最大连接数
  • DefaultMaxPerRoute——每个路由的默认最大连接数
  • connectionRequestTimeout——连接池获取链接超时时间
  • connectTimeout——建立连接的超时时间
  • socketTimeout——Socket的读超时时间

在Spring中的配置方式

@Configuration
public class RestTemplateConfig {
    @Bean
    public HttpClientConnectionManager poolingConnectionManager() {
        PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
        poolingConnectionManager.setMaxTotal(1000); // 连接池最大连接数
        poolingConnectionManager.setDefaultMaxPerRoute(500); // 每个主机的并发
        return poolingConnectionManager;
    }
    @Bean
    public HttpClientBuilder httpClientBuilder() {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //设置HTTP连接管理器
        httpClientBuilder.setConnectionManager(poolingConnectionManager());
        return httpClientBuilder;
    }
    @Bean("restTemplate")
    @LoadBalanced
    public RestTemplate restTemplate(){
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setHttpClient(httpClientBuilder().build());
        httpRequestFactory.setConnectionRequestTimeout(3000);//获取链接超时时间
        httpRequestFactory.setConnectTimeout(3000);//指客户端和服务器建立连接的timeout
        httpRequestFactory.setReadTimeout(120000);//读取数据的超时时间
        RestTemplate restTemplate=new RestTemplate(httpRequestFactory);
        return restTemplate;
    }

MaxTotal和DefaultMaxPerRoute

这两个是一组影响服务器并发性的相关配置。当您需要增加并发性时,通常会增加MaxTOTAL,而忽略DefaultMaxPerroute。因此,并发性不会增加。因此,在改进MaxTOTAL参数时,我们需要考虑系统架构中的主机数量,并相应地改进DefaultMaxPerRouting。一般的建议是DefaultMaxPerroute大约是MaxTOTAL的一半。

[En]

These two are a set of related configurations that affect server concurrency. When you need to increase the concurrency, you will often increase the MaxTotal while ignoring the DefaultMaxPerRoute. As a result, the concurrency will not increase. Therefore, when improving the MaxTotal parameters, we need to consider the number of hosts in the system architecture and improve the DefaultMaxPerRoute accordingly. The general advice is that DefaultMaxPerRoute is about half of MaxTotal.

超时时间设置

其他三个设置主要用于超时设置。建议不要将ConnectionRequestTimeout和ConnectTimeout设置得太长。在网络条件较差的情况下,会占用大量连接。SocketTimeout需要设置更多服务。太长会导致连接被占用,太短会导致下载时大数据超时。对于公共接口服务,socketTimeout不应太长。接口设计要优化,不能一次返回大量数据。文件服务中,有大量的文件下载需求,socketTimeout可以根据网络情况设置得稍大一些。

[En]

The other three settings are mainly for timeout settings. ConnectionRequestTimeout and connectTimeout are not recommended to set too long. In the case of poor network conditions, a large number of connections will be occupied. SocketTimeout needs more services to set. Too long will cause connections to be occupied, and too short will cause big data to time out when downloading. For public interface services, the socketTimeout should not be too long. The interface design should be optimized and large amounts of data should not be returned at once. File service, there are a large number of file download requirements, socketTimeout can be set a little larger according to the network situation.

总结

最终设置的调整仍然是一个持续的过程,需要在服务发布过程中进行持续的监控和调整。根据具体业务情况进行优化配置。

[En]

The tuning of the final setting is still a continuous process, which requires continuous monitoring and tuning during the service release process. Make the optimal configuration according to the specific business situation.

Original: https://www.cnblogs.com/austinspark-jessylu/p/15352826.html
Author: 爷的眼睛闪亮
Title: RestTemplateConfig

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7223/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部