本教程详细介绍如何使用Alibaba Cloud SDK for Java变更ECS实例规格(可修改公网宽带大小)。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationResponse.AvailableZone.AvailableResource.SupportedResource;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceSpecRequest;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceSpecResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;

/**
 * DescribeResourcesModification    查询升级和降配实例规格或者系统盘时,某一可用区的可用资源信息
 * ModifyInstanceSpec    调整一台按量付费ECS实例的实例规格和公网带宽大小
 */
public class ModifyInstance {

    // 已关机状态的ECS实例
    private static String instancenId = "i-bp1gh*****";

    public static void main(String[] args) {
        Gson gson = new Gson();
        IAcsClient client = Initialization();
        // 查询某实例升级实例规则时的可用资源信息
        List<DescribeResourcesModificationResponse.AvailableZone> availableZones = DescribeResourcesModification(client);
        System.out.println("--------------------Available resource information for instance rules--------------------");
        System.out.println(gson.toJson(availableZones));
        // 获取实例的可用资源信息
        // (此处应根据实际情况或者结合前端业务来进行)
        SupportedResource supportedResource = availableZones.get(0).getAvailableResources().get(0).getSupportedResources().get(0);
        // 支持的可供创建的具体资源
        ModifyInstanceSpecResponse response = ModifyInstanceSpec(client, supportedResource.getValue());
        System.out.println("--------------------Instance specification changes successfully--------------------");
        System.out.println(gson.toJson(response));
    }

    /**
     * ModifyInstanceSpec    调整一台按量付费ECS实例的实例规格和公网带宽大小
     */
    private static ModifyInstanceSpecResponse ModifyInstanceSpec(IAcsClient client, String value) {
        ModifyInstanceSpecRequest request = new ModifyInstanceSpecRequest();
        // 指定的实例ID
        request.setInstanceId(instancenId);
        /**
         * 是否支持跨集群升级实例规格。默认值:false
         * 当参数AllowMigrateAcrossZone取值为true时,一旦您根据返回信息升级了云服务器,请留意以下注意事项:
         *
         * 经典网络类型实例:
         * 对于已停售的实例规格,非I/O优化实例变配到I/O优化实例时,实例私网IP地址、磁盘设备名和软件授权码会发
         * 生变化。对于Linux实例,普通云盘(cloud)会被识别为xvda或者xvdb等,高效云盘(cloud_efficiency)
         * 和SSD云盘(cloud_ssd)会被识别为vda或者vdb等。
         * 对于正常售卖的实例规格族,实例的私网IP地址会发生变化。
         *
         * 专有网络VPC类型实例:
         * 对于已停售的实例规格,非I/O优化实例变配到I/O优化实例时,云服务器磁盘设备名和软件授权码会发生变化。Linux
         * 实例的普通云盘(cloud)会被识别为xvda或者xvdb等,高效云盘(cloud_efficiency)和SSD云盘(cloud_ssd)
         * 会被识别为vda或者vdb等。
         */
        request.setAllowMigrateAcrossZone(false);
        request.setAsync(true);
        request.setInstanceType(value);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * DescribeResourcesModification    查询升级和降配实例规格或者系统盘时,某一可用区的可用资源信息
     */
    private static List<DescribeResourcesModificationResponse.AvailableZone> DescribeResourcesModification(IAcsClient client) {
        DescribeResourcesModificationRequest request = new DescribeResourcesModificationRequest();
        //
        request.setDestinationResource("InstanceType");
        request.setResourceId(instancenId);
        try {
            DescribeResourcesModificationResponse response = client.getAcsResponse(request);
            return response.getAvailableZones();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }


    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient Initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}
				

执行结果

正确的返回结果类似如下:
--------------------Available resource information for instance rules--------------------
[
    {
        "regionId": "cn-hangzhou-dg-a01", 
        "zoneId": "cn-hangzhou-h", 
        "status": "Available", 
        "statusCategory": "WithStock", 
        "availableResources": [
            {
                "type": "InstanceType", 
                "supportedResources": [
                    {
                        "value": "ecs.hfc5.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.t5-c1m1.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.ic5.3xlarge", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.c5.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.t5-c1m1.xlarge", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }
                ]
            }
        ]
    }
]
--------------------Instance specification changes successfully--------------------
{"requestId":"BAC795D9-D8F1-4E76-9F69-75EA47B5FD4B"}