本教程详细介绍如何使用Alibaba Cloud SDK for Java修改实例的带宽配置,返回ECS实例的详细信息。

前提条件

在使用本教程之前,请确保已完成以下操作:

  • 使用ECS管理控制台或通过API获取您需要的ECS实例ID,API接口请参见DescribeInstances
  • 使用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.DescribeInstancesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceNetworkSpecRequest;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceNetworkSpecResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

/**
 * ModifyInstanceNetworkSpec    修改实例的带宽配置
 * DescribeInstances    查询一台或多台实例的详细信息
 */
public class TestModifyInstanceNetworkSpec {

    // ECS实例ID(请自行配置)
    private static String instanceId = "i-bp1332juxxxxxxx";

    public static void main(String[] args) {
        IAcsClient client = Initialization();
        // 修改实例的带宽配置
        ModifyInstanceNetworkSpec(client, instanceId);
        // 查询一台或多台实例的详细信息
        DescribeInstances(client, instanceId);
    }

    private static void DescribeInstances(IAcsClient client, String instanceId) {
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        JsonArray elements = new JsonArray();
        elements.add(instanceId);
        request.setInstanceIds(elements.toString());
        try {
            DescribeInstancesResponse response = client.getAcsResponse(request);
            System.out.println("--------------------实例的详细信息--------------------");
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    private static void ModifyInstanceNetworkSpec(IAcsClient client, String instanceId) {
        ModifyInstanceNetworkSpecRequest request = new ModifyInstanceNetworkSpecRequest();
        // 需要修改网络配置的实例ID。
        request.setInstanceId(instanceId);
        // 公网入带宽最大值,单位:Mbps(Megabit per second)。取值范围:1~200
        request.setInternetMaxBandwidthIn(50);
        // 公网出带宽最大值,单位:Mbps(Megabit per second)。取值范围:0~100
        request.setInternetMaxBandwidthOut(50);
        // 转换网络计费方式。取值范围:
        // PayByTraffic:按使用流量计费
        // PayByBandwidth:按固定带宽计费
        request.setNetworkChargeType("PayByTraffic");
        // 临时带宽升级开始时间。按照ISO8601标准表示,并使用UTC +0时间,格式为yyyy-MM-ddThh:mmZ。精确到分钟(mm)。
        request.setStartTime("2019-09-16T16:00Z");
        // 临时带宽升级结束时间。按照ISO8601标准表示,并使用UTC +0时间,格式为yyyy-MM-ddThhZ。精确到小时(hh)。
        request.setEndTime("2019-09-17T16:00Z");
        // 是否分配公网 IP 地址。默认值:false
        request.setAllocatePublicIp(false);
        // 是否自动支付。取值范围:
        // true:变更带宽配置后,自动扣费。当您将参数Autopay置为true时,您需要确保账户余额充足,如果账户余额不足会生成异常订单,此订单暂时不支持通过ECS控制台支付,只能作废。
        // false:变更带宽配置后,只生成订单不扣费。如果您的支付方式余额不足,可以将参数Autopay置为false,即取消自动支付,此时调用该接口会生成正常的未支付订单,此订单可登录ECS管理控制台支付。
        // 默认值:true
        request.setAutoPay(true);
        try {
            ModifyInstanceNetworkSpecResponse response = client.getAcsResponse(request);
            System.out.println("--------------------实例的带宽配置修改成功--------------------");
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }


    /**
     * 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);
    }
}
			

执行结果

正确的返回结果类似如下:
--------------------实例的带宽配置修改成功--------------------
{"requestId":"3E5B95DA-F3FC-472B-B1BB-7F923AF937E7","orderId":"2044823XXXXXX"}
--------------------实例的详细信息--------------------
{
    "requestId": "88289E05-3B7F-4A5E-8734-21F9531CCAE0", 
    "totalCount": 1, 
    "pageNumber": 1, 
    "pageSize": 10, 
    "instances": [
        {
            "instanceId": "i-bp1332junxxxx", 
            "instanceName": "launch-advisor-201xxxx", 
            "description": "", 
            "imageId": "centos_7_05_64_20G_alibase_20181210.vhd", 
            "oSName": "CentOS  7.5 64位", 
            "oSType": "linux", 
            "regionId": "cn-hangzhou", 
            "zoneId": "cn-hangzhou-i", 
            "clusterId": "", 
            "instanceType": "ecs.gn6v-c8g1.2xlarge", 
            "cpu": 8, 
            "memory": 32768, 
            "hostName": "iZbp1332junn5uiqg7ekr3Z", 
            "status": "Running", 
            "serialNumber": "955be26e-9761-4fde-8b01-337xxxxx", 
            "internetChargeType": "PayByTraffic", 
            "internetMaxBandwidthIn": 2500, 
            "internetMaxBandwidthOut": 50, 
            "vlanId": "", 
            "creationTime": "2019-09-16T07:23Z", 
            "startTime": "2019-09-16T07:23Z", 
            "instanceNetworkType": "vpc", 
            "instanceChargeType": "PrePaid", 
            "saleCycle": "", 
            "expiredTime": "2019-09-23T16:00Z", 
            "autoReleaseTime": "", 
            "ioOptimized": true, 
            "deviceAvailable": true, 
            "instanceTypeFamily": "ecs.gn6v", 
            "gPUAmount": 1, 
            "gPUSpec": "NVIDIA V100", 
            "spotStrategy": "NoSpot", 
            "spotPriceLimit": 0, 
            "resourceGroupId": "", 
            "recyclable": false, 
            "stoppedMode": "Not-applicable", 
            "creditSpecification": "", 
            "networkInterfaces": [...], 
            "operationLocks": [...], 
            "tags": [...], 
            "securityGroupIds": [...], 
            "publicIpAddress": [...], 
            "innerIpAddress": [...], 
            "rdmaIpAddress": [...], 
            "vpcAttributes": {...}, 
            "eipAddress": {...}, 
            "dedicatedHostAttribute": {...}
        }
    ]
}