本教程详细介绍如何使用Alibaba Cloud SDK for Java为NAT网关添加和修改共享带宽。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您的NAT网关实例已绑定弹性公网IP(EIP),请参见为NAT网关绑定弹性公网IP
  • 确保您已经安装了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.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-vpc -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vpc</artifactId>
            <version>3.0.6</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vpc.model.v20160428.*;
import com.google.gson.Gson;
import java.util.UUID;

/**
 * 1.绑定EIP          AssociateEipAddress
 *     参见:https://help.aliyun.com/document_detail/140808.htm
 * 2.购买共享宽带      CreateCommonBandwidthPackage
 * 3.绑定NAT网关IP    AddCommonBandwidthPackageIp
 * 4.修改带宽         ModifyCommonBandwidthPackageSpec
 */
public class Demo {

    private static String eipId = "eip-uf6l5m4qrcxypyt4e6epg";

    private static Gson gson = new Gson();

    public static void main(String[] args) {

        IAcsClient client = initialization();
        // 购买共享宽带
        CreateCommonBandwidthPackageResponse createCommonBandwidthPackageResponse = createCommonBandwidthPackage(client);
        String bandwidthPackageId = createCommonBandwidthPackageResponse.getBandwidthPackageId();
        // 绑定NAT网关IP
        addCommonBandwidthPackageIp(client, bandwidthPackageId, eipId);
        // 修改带宽
        modifyCommonBandwidthPackageSpec(client, bandwidthPackageId);
    }

    /**
     * 修改带宽         ModifyCommonBandwidthPackageSpec
     */
    private static void modifyCommonBandwidthPackageSpec(IAcsClient client, String bandwidthPackageId) {
        ModifyCommonBandwidthPackageSpecRequest request = new ModifyCommonBandwidthPackageSpecRequest();
        // 共享带宽实例的ID
        request.setBandwidthPackageId(bandwidthPackageId);
        // 共享带宽实例的带宽峰值,单位为Mbps
        request.setBandwidth("500");
        System.out.println("------------------modifyCommonBandwidthPackageSpec-------------------");
        try {
            ModifyCommonBandwidthPackageSpecResponse response = client.getAcsResponse(request);
            System.out.println(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());
            throw new RuntimeException(e);
        }
    }

    /**
     * 添加EIP到共享带宽中
     */
    private static void addCommonBandwidthPackageIp(IAcsClient client, String bandwidthPackageId,String eipId) {

        AddCommonBandwidthPackageIpRequest request = new AddCommonBandwidthPackageIpRequest();
        // 共享带宽的ID
        request.setBandwidthPackageId(bandwidthPackageId);
        // EIP实例的ID
        request.setIpInstanceId(eipId);
        System.out.println("------------------addCommonBandwidthPackageIp-------------------");
        try {
            AddCommonBandwidthPackageIpResponse response = client.getAcsResponse(request);
            System.out.println(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());
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建共享带宽实例     CreateCommonBandwidthPackage
     */
    private static CreateCommonBandwidthPackageResponse createCommonBandwidthPackage(IAcsClient client) {
        CreateCommonBandwidthPackageRequest request = new CreateCommonBandwidthPackageRequest();
        // 共享带宽的带宽峰值,单位为Mbps
        request.setBandwidth(5);
        // 用于保证请求的幂等性。由客户端生成该参数值,要保证在不同请求间唯一,最大值不超过64个ASCII字符
        request.setClientToken(UUID.randomUUID().toString());
        // 共享带宽的描述信息。长度为2~256个字符,必须以字母或中文开头,但不能以http://或https://开头
        request.setDescription("This is a ....");
        // 共享带宽的计费方式,取值:
        // PayByBandwidth(默认值):按带宽计费
        // PayBy95:按增强型95计费
        // 如需PayByTraffic计费方式,请提交工单
        request.setInternetChargeType("PayByBandwidth");
        // 共享带宽的名称
        // 长度为2~128个字符,必须以字母或中文开头,可包含数字,点号(.),下划线(_)和短横线(-),但不能以http://或https://开头
        request.setName("Test");
        System.out.println("------------------createCommonBandwidthPackage-------------------");
        try {
            CreateCommonBandwidthPackageResponse response = client.getAcsResponse(request);
            System.out.println(gson.toJson(response));
            return 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());
            throw new RuntimeException(e);
        }
        return null;
    }

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

执行结果

正确的执行结果类似如下:
------------------createCommonBandwidthPackage-------------------
{
  "requestId": "8EB45C27-B94A-4493-8E51-ECBA9C82BA35",
  "bandwidthPackageId": "cbwp-uf6bt*****",
  "resourceGroupId": "rg-acfm*****"
}
------------------addCommonBandwidthPackageIp-------------------
{"requestId":"0F19E025-97C8-4E4A-86EF-3ACA1452ABB6"}
------------------modifyCommonBandwidthPackageSpec-------------------
{"requestId":"DE8049BA-0756-4456-BE80-2C7A24AD6AC6"}