本教程详细介绍如何使用Alibaba Cloud SDK for Java修改指定EIP的带宽峰值。

前提条件

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

  • 使用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 --&gt;     <dependency&gt;         <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.DescribeEipAddressesRequest; import com.aliyuncs.vpc.model.v20160428.DescribeEipAddressesResponse; import com.aliyuncs.vpc.model.v20160428.ModifyEipAddressAttributeRequest; import com.aliyuncs.vpc.model.v20160428.ModifyEipAddressAttributeResponse; import com.google.gson.Gson;  /**  * ModifyEipAddressAttribute     修改指定EIP的带宽峰值  * DescribeEipAddresses     查询指定地域已创建的EIP信息  */ public class Demo {      private static String eipId = "eip-uf6****";      public static void main(String[] args) {         // 初始化公公共请求参数         IAcsClient client = initialization();         // 更改共享带宽的带宽峰值         ModifyEipAddressAttribute(client,eipId);         // 查询指定EIP信息         describeEipAddresses(client,eipId);     }      /**      * DescribeEipAddresses     查询指定地域已创建的EIP信息      */     private static void describeEipAddresses(IAcsClient client, String eipId) {         DescribeEipAddressesRequest request = new DescribeEipAddressesRequest();         // EIP的ID。         request.setAllocationId(eipId);         try {             DescribeEipAddressesResponse response = client.getAcsResponse(request);             System.out.println("------------------describeEipAddresses-------------------");             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());             throw new RuntimeException(e);         }     }      /**      * ModifyEipAddressAttribute     修改指定EIP的带宽峰值      */     private static void ModifyEipAddressAttribute(IAcsClient client, String eipId) {         ModifyEipAddressAttributeRequest request = new ModifyEipAddressAttributeRequest();         // EIP的带宽峰值,单位为Mbps。         request.setBandwidth("100");         // 弹性公网IP的ID。         request.setAllocationId(eipId);         try {             ModifyEipAddressAttributeResponse response = client.getAcsResponse(request);             System.out.println("------------------ModifyEipAddressAttribute-------------------");             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());             throw new RuntimeException(e);         }     }      /**      * 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);     } }

执行结果

正确的执行结果类似如下:

------------------ModifyEipAddressAttribute------------------- {"requestId":"B7814D2F-2978-47BA-A057-52A50EFF8D4E"} ------------------describeEipAddresses------------------- {     "requestId": "589CDA8D-E3F2-4624-BE0D-0EF8750C49FB",      "totalCount": 1,      "pageNumber": 1,      "pageSize": 10,      "eipAddresses": [         {             "regionId": "cn-shanghai",              "ipAddress": "47.102.218.198",              "privateIpAddress": "",              "allocationId": "eip-uf6l5*****",              "status": "InUse",              "instanceId": "ngw-uf64h*****",              "bandwidth": "500",              "eipBandwidth": "100",              "internetChargeType": "PayByTraffic",              "allocationTime": "2019-10-08T07:32:43Z",              "instanceType": "Nat",              "instanceRegionId": "cn-shanghai",              "chargeType": "PostPaid",              "expiredTime": "",              "hDMonitorStatus": "OFF",              "name": "",              "iSP": "BGP",              "descritpion": "",              "bandwidthPackageId": "cbwp-uf6f*****",              "bandwidthPackageType": "CommonB*****",              "bandwidthPackageBandwidth": "500",              "resourceGroupId": "rg-acf*****",              "hasReservationData": "false",              "mode": "NAT",              "secondLimited": false,              "operationLocks": [ ],              "tags": [ ],              "availableRegions": [                 "cn-shanghai"             ]         }     ] }