Configure an HTTP connection pool

更新时间:
复制 MD 格式

This topic describes how to configure an HTTP connection pool in Alibaba Cloud SDK V1.0.

When you initialize an SDK client in SDK V1.0, you can use HttpClientConfig to configure an HTTP connection pool. The HTTP connection pool processes API requests that are initiated from SDKs for multiple Alibaba Cloud services because you can use only one SDK client.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
public class Sample {
    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile(
                // The region ID.
                "<region-id>",
                // Get the AccessKey ID of the Resource Access Management (RAM) user from an environment variable.
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Get the AccessKey secret of the RAM user from an environment variable.
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

        /* Multiple SDK clients share one connection pool. Set the parameters for this pool, such as the maximum number of connections for each host and the timeout period. */
        HttpClientConfig clientConfig = HttpClientConfig.getDefault();
        clientConfig.setMaxRequestsPerHost(6);
        // Unit: milliseconds.
        clientConfig.setConnectionTimeoutMillis(30000L);
        clientConfig.setMaxIdleConnections(20);

        profile.setHttpClientConfig(clientConfig);
        IAcsClient client = new DefaultAcsClient(profile);
    }
}