背景
您在阿里云云服务器ECS的选型过程中,可以参考选型最佳实践,其中对比实例性能时,可以调用DescribeInstanceTypes API获取最新的规格性能参数。
当前ECS实例规格数量越来越多,为保证查询时可以获得符合条件的所有规格,及避免调用时触发限流,强烈推荐您使用分页查询的方式调用。
关于DescribeInstanceTypes API的接口文档可以参考DescribeInstanceTypes。
推荐方式
-
采用分页查询:查询非首页时,将上页返回的 NextToken值作为请求参数
-
设置 MaxResults以限制返回信息的条目数,推荐设置 MaxResults为 100及以下(当前最大为1600,后续最大值将只支持100)
-
尽可能指定过滤条件,如设置 InstanceTypeFamily指定实例规格所属的实例规格族,或设置 InstanceTypes指定查询的实例规格。
示例代码
需求举例:查询期望最小vCPU内核的数目为4,期望最小内存为8GiB的规格,示例代码中进行了分页查询,每次查询的MaxResults设置为100,并将查询结果进行整合。
package com.example.demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.ecs20140526.models.*;
import com.aliyun.sdk.service.ecs20140526.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
import org.apache.commons.lang3.StringUtils;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
public class DescribeInstanceTypes {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<your-proxy-hostname>", 9001))
.setCredentials("<your-proxy-username>", "<your-proxy-password>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId("<your-accessKeyId>")
.accessKeySecret("<your-accessKeySecret>")
//.securityToken("<your-token>") // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("cn-zhangjiakou") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("ecs.cn-zhangjiakou.aliyuncs.com")
//.setReadTimeout(Duration.ofSeconds(30))
)
.build();
List<DescribeInstanceTypesResponseBody.InstanceType> results = new ArrayList<>();
// Parameter settings for API request
DescribeInstanceTypesRequest describeInstanceTypesRequest = DescribeInstanceTypesRequest.builder()
.minimumCpuCoreCount(4)
.minimumMemorySize(8F)
.maxResults(100L)
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
List<DescribeInstanceTypesResponseBody.InstanceType> results = new ArrayList<>();
// Parameter settings for API request
DescribeInstanceTypesRequest describeInstanceTypesRequest;
// Asynchronously get the return value of the API request
CompletableFuture<DescribeInstanceTypesResponse> response;
// Synchronously get the return value of the API request
DescribeInstanceTypesResponse resp;
String nextToken = null;
do {
describeInstanceTypesRequest = DescribeInstanceTypesRequest.builder()
.minimumCpuCoreCount(4)
.minimumMemorySize(8F)
.maxResults(100L)
.nextToken(nextToken)
.build();
response = client.describeInstanceTypes(describeInstanceTypesRequest);
resp = response.get();
nextToken = resp.getBody().getNextToken();
results.addAll(resp.getBody().getInstanceTypes().getInstanceType());
} while (StringUtils.isNotEmpty(nextToken));
System.out.println(new Gson().toJson(results));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}