本文介绍了如何使用阿里云 ECS SDK 合理快速地创建并管理抢占式实例。
准备工作
在执行操作之前,您需要:
- 了解能满足您业务要求的实例规格和地域。
- 熟悉了解阿里云 ECS SDK 的基础知识和调用方法。详细信息,请参考 SDK 使用说明。
注意:抢占式实例代码需要依赖的 ECS SDK 版本 4.2.0 以上。以 Java POM 依赖为例,修改引入 pom 依赖:
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-core</artifactId>
- <version>3.2.8</version>
- </dependency>
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-ecs</artifactId>
- <version>4.2.0</version>
- </dependency>
查询地域及可用的实例规格
使用 OpenAPI
DescribeZones 查询可以创建抢占式实例的地域以及可用的实例规格。示例代码如下所示。
OpenApiCaller.java
- public
class
OpenApiCaller
{
- IClientProfile profile;
- IAcsClient client;
- public OpenApiCaller() {
- profile = DefaultProfile.getProfile("cn-hangzhou", AKSUtil.accessKeyId, AKSUtil.accessKeySecret);
- client = new DefaultAcsClient(profile);
- }
- public <T extends AcsResponse> T doAction(AcsRequest<T> var1) {
- try {
- return client.getAcsResponse(var1);
- } catch (Throwable e) {
- e.printStackTrace();
- return null;
- }
- }
- }
DescribeZonesSample.java
- public
class
DescribeZonesSample
{
- public static void main(String[] args) {
- OpenApiCaller caller = new OpenApiCaller();
- DescribeZonesRequest request = new DescribeZonesRequest();
- request.setRegionId("cn-zhangjiakou");//可以通过 DescribeRegionsRequest 获取每个地域的 RegionId
- request.setSpotStrategy("SpotWithPriceLimit");//对于查询是否可购买抢占式实例此项必填
- request.setInstanceChargeType("PostPaid");//后付费模式,抢占式实例必须是后付费模式
- DescribeZonesResponse response = caller.doAction(request);
- System.out.println(JSON.toJSONString(response));
- }
- }
以下为输出结果,可以查看每个地域各个地域可供选择的实例规格、磁盘类型、网络类型等信息。
- {
- "requestId": "388D6321-E587-470C-8CFA-8985E2963DAE",
- "zones": [
- {
- "localName": "华北 3 可用区 A",
- "zoneId": "cn-zhangjiakou-a",
- "availableDiskCategories": [
- "cloud_ssd",
- "cloud_efficiency"
- ],
- "availableInstanceTypes": [
- "ecs.e4.large",
- "ecs.n4.4xlarge",
- "ecs.sn2.medium",
- "ecs.i1.2xlarge",
- "ecs.se1.2xlarge",
- "ecs.n4.xlarge",
- "ecs.se1ne.2xlarge",
- "ecs.se1.large",
- "ecs.sn2.xlarge",
- "ecs.se1ne.xlarge",
- "ecs.xn4.small",
- "ecs.sn2ne.4xlarge",
- "ecs.se1ne.4xlarge",
- "ecs.sn1.medium",
- "ecs.n4.8xlarge",
- "ecs.mn4.large",
- "ecs.e4.2xlarge",
- "ecs.mn4.2xlarge",
- "ecs.mn4.8xlarge",
- "ecs.n4.2xlarge",
- "ecs.e4.xlarge",
- "ecs.sn2ne.large",
- "ecs.sn2ne.xlarge",
- "ecs.sn1ne.large",
- "ecs.n4.large",
- "ecs.sn1.3xlarge",
- "ecs.e4.4xlarge",
- "ecs.sn1ne.2xlarge",
- "ecs.e4.small",
- "ecs.i1.4xlarge",
- "ecs.se1.4xlarge",
- "ecs.sn2ne.2xlarge",
- "ecs.sn2.3xlarge",
- "ecs.i1.xlarge",
- "ecs.n4.small",
- "ecs.sn1ne.4xlarge",
- "ecs.mn4.4xlarge",
- "ecs.sn1ne.xlarge",
- "ecs.se1ne.large",
- "ecs.sn2.large",
- "ecs.i1-c5d1.4xlarge",
- "ecs.sn1.xlarge",
- "ecs.sn1.large",
- "ecs.mn4.small",
- "ecs.mn4.xlarge",
- "ecs.se1.xlarge"
- ],
- "availableResourceCreation": [
- "VSwitch",
- "IoOptimized",
- "Instance",
- "Disk"
- ],
- "availableResources": [
- {
- "dataDiskCategories": [
- "cloud_ssd",
- "cloud_efficiency"
- ],
- "instanceGenerations": [
- "ecs-3",
- "ecs-2"
- ],
- "instanceTypeFamilies": [
- "ecs.mn4",
- "ecs.sn1",
- "ecs.sn2",
- "ecs.sn1ne",
- "ecs.xn4",
- "ecs.i1",
- "ecs.se1",
- "ecs.e4",
- "ecs.n4",
- "ecs.se1ne",
- "ecs.sn2ne"
- ],
- "instanceTypes": [
- "ecs.n4.4xlarge",
- "ecs.sn2.medium",
- "ecs.i1.2xlarge",
- "ecs.se1.2xlarge",
- "ecs.n4.xlarge",
- "ecs.se1ne.2xlarge",
- "ecs.se1.large",
- "ecs.sn2.xlarge",
- "ecs.se1ne.xlarge",
- "ecs.xn4.small",
- "ecs.sn2ne.4xlarge",
- "ecs.se1ne.4xlarge",
- "ecs.sn1.medium",
- "ecs.n4.8xlarge",
- "ecs.mn4.large",
- "ecs.mn4.2xlarge",
- "ecs.mn4.8xlarge",
- "ecs.n4.2xlarge",
- "ecs.sn2ne.large",
- "ecs.sn2ne.xlarge",
- "ecs.sn1ne.large",
- "ecs.n4.large",
- "ecs.sn1.3xlarge",
- "ecs.sn1ne.2xlarge",
- "ecs.e4.small",
- "ecs.i1.4xlarge",
- "ecs.se1.4xlarge",
- "ecs.sn2ne.2xlarge",
- "ecs.sn2.3xlarge",
- "ecs.i1.xlarge",
- "ecs.n4.small",
- "ecs.sn1ne.4xlarge",
- "ecs.mn4.4xlarge",
- "ecs.sn1ne.xlarge",
- "ecs.se1ne.large",
- "ecs.sn2.large",
- "ecs.i1-c5d1.4xlarge",
- "ecs.sn1.xlarge",
- "ecs.sn1.large",
- "ecs.mn4.small",
- "ecs.mn4.xlarge",
- "ecs.se1.xlarge"
- ],
- "ioOptimized": true,
- "networkTypes": [
- "vpc"
- ],
- "systemDiskCategories": [
- "cloud_ssd",
- "cloud_efficiency"
- ]
- }
- ],
- "availableVolumeCategories": [
- "san_ssd",
- "san_efficiency"
- ]
- }
- ]
- }
查询抢占式实例的历史价格
使用 OpenAPI
DescribeSpotPriceHistory 查询抢占式实例最近 30 天的价格变化数据,获得最佳性价比的地域和规格信息,示例代码(DescribeSpotPriceHistorySample.java)如下。
- public class DescribeSpotPriceHistorySample {
- public static void main(String[] args) {
- OpenApiCaller caller = new OpenApiCaller();
- List<DescribeSpotPriceHistoryResponse.SpotPriceType> result = new ArrayList<DescribeSpotPriceHistoryResponse.SpotPriceType>();
- int offset = 0;
- while (true) {
- DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest();
- request.setRegionId("cn-hangzhou");//可以通过 DescribeRegionsRequest 获取可购买的每个地域的 RegionId
- request.setZoneId("cn-hangzhou-b");//可用区必填
- request.setInstanceType("ecs.sn2.medium");//参考 DescribeZones 返回的实例类型,必填
- request.setNetworkType("vpc");//参考 DescribeZones 返回的网络类型,必填
- // request.setIoOptimized("optimized");//是否 I/O 优化类型,DescribeZones 返回的 IoOptimized,选填
- // request.setStartTime("2017-09-20T08:45:08Z");//价格开始时间,选填,默认 3 天内数据
- // request.setEndTime("2017-09-28T08:45:08Z");//价格结束时间,选填
- request.setOffset(offset);
- DescribeSpotPriceHistoryResponse response = caller.doAction(request);
- if (response != null && response.getSpotPrices() != null) {
- result.addAll(response.getSpotPrices());
- }
- if (response.getNextOffset() == null || response.getNextOffset() == 0) {
- break;
- } else {
- offset = response.getNextOffset();
- }
- }
- if (!result.isEmpty()) {
- for (DescribeSpotPriceHistoryResponse.SpotPriceType spotPriceType : result) {
- System.out.println(spotPriceType.getTimestamp() + "--->spotPrice:" + spotPriceType.getSpotPrice() + "---->originPrice:" + spotPriceType.getOriginPrice());
- }
- System.out.println(result.size());
- } else {
- }
- }
- }
以下为返回结果示例。
- 2017-09-26T06:28:55Z--->spotPrice:0.24---->originPrice:1.2
- 2017-09-26T14:00:00Z--->spotPrice:0.36---->originPrice:1.2
- 2017-09-26T15:00:00Z--->spotPrice:0.24---->originPrice:1.2
- 2017-09-27T14:00:00Z--->spotPrice:0.36---->originPrice:1.2
- 2017-09-27T15:00:00Z--->spotPrice:0.24---->originPrice:1.2
- 2017-09-28T14:00:00Z--->spotPrice:0.36---->originPrice:1.2
- 2017-09-28T15:00:00Z--->spotPrice:0.24---->originPrice:1.2
- 2017-09-29T06:28:55Z--->spotPrice:0.24---->originPrice:1.2
重复以上步骤,您可以判断出该规格资源在可用区的价格变化趋势和最近价格。
说明:您可以通过平均价格和最高价格来决定是否可以接受购买该抢占式实例,也可以通过更加合理的数据模型来分析历史价格数据,随时调整创建资源的规格和可用区,到达最佳性价比。