本教程详细介绍如何使用Alibaba Cloud SDK for Java申请一个弹性公网IP(EIP)并将弹性公网IP(EIP)绑定到同地域的NAT网关实例上。
前提条件
在使用本教程前,请确保已完成以下操作:
- 使用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.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.AllocateEipAddressRequest;
import com.aliyuncs.vpc.model.v20160428.AllocateEipAddressResponse;
import com.aliyuncs.vpc.model.v20160428.AssociateEipAddressRequest;
import com.aliyuncs.vpc.model.v20160428.AssociateEipAddressResponse;
import com.google.gson.Gson;
import java.util.UUID;
/**
* AllocateEipAddress 申请弹性公网IP(EIP)
* AssociateEipAddress 将弹性公网IP(EIP)绑定到同地域的云产品实例上
*/
public class Demo {
private static String instanceId = "ngw-uf64hax****";
public static void main(String[] args) {
IAcsClient client = initialization();
// 申请弹性公网IP(EIP)
AllocateEipAddressResponse allocateEipAddressResponse = allocateEipAddress(client);
// 获取刚申请的EIP实例Id
String allocationId = allocateEipAddressResponse.getAllocationId();
// 将弹性公网IP(EIP)绑定到同地域的云产品实例上
associateEipAddress(client, allocationId);
}
/**
* AssociateEipAddress 将弹性公网IP(EIP)绑定到同地域的云产品实例上
*/
private static void associateEipAddress(IAcsClient client, String allocationId) {
AssociateEipAddressRequest request = new AssociateEipAddressRequest();
// EIP的ID
request.setAllocationId(allocationId);
// 要绑定的实例ID
request.setInstanceId(instanceId);
// 要绑定的云产品实例的类型,取值:Nat / SlbInstance / EcsInstance / NetworkInterface
request.setInstanceType("Nat");
// 绑定模式,取值:NAT / MULTI_BINDED
request.setMode("NAT");
try {
AssociateEipAddressResponse response = client.getAcsResponse(request);
System.out.println("------------------associateEipAddress-------------------");
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());
}
}
/**
* AllocateEipAddress 申请弹性公网IP(EIP)
*/
private static AllocateEipAddressResponse allocateEipAddress(IAcsClient client) {
AllocateEipAddressRequest request = new AllocateEipAddressRequest();
// 是否自动付费,取值:
// false:不开启自动付费,生成订单后需要到订单中心完成支付。
// true:开启自动付费,自动支付订单。
// 当InstanceChargeType参数的值为PrePaid时,该参数必选;当InstanceChargeType参数的值为PostPaid时,该参数可不填。
request.setAutoPay(true);
// EIP的带宽峰值,单位为Mbps,默认值为5。
request.setBandwidth("5");
// 线路类型,默认值为BGP。
//
// 对于已开通单线带宽白名单的用户,ISP字段可以设置为ChinaTelecom、ChinaUnicom和ChinaMobile,用来开通中国电信、中国联通、中国移动的单线EIP。
// 如果是杭州金融云用户,该字段必填,取值:BGP_FinanceCloud。
request.setISP("BGP");
// EIP的计费方式,取值:
// PrePaid:包年包月。
// PostPaid(默认值):按量计费。
// 当InstanceChargeType取值为PrePaid时,InternetChargeType必须取值PayByBandwidth;当InstanceChargeType取值为PostPaid时,InternetChargeType可取值PayByBandwidth或PayByTraffic。
// 包年包月和按量计费的详细信息,请参见包年包月和按量计费。
request.setInstanceChargeType("PostPaid");
// EIP的计量方式,取值:
//
// PayByBandwidth(默认值):按带宽计费。
//
// PayByTraffic:按流量计费。
//
// 当InstanceChargeType取值为PrePaid时,InternetChargeType必须取值PayByBandwidth。详细信息,请参见包年包月。
//
// 当InstanceChargeType取值为PostPaid时,InternetChargeType可取值PayByBandwidth或PayByTraffic。详细信息,请参见按使用流量和按固定带宽。
request.setInternetChargeType("PayByTraffic");
// 网络类型,默认值为public。
request.setNetmode("public");
// 客户端token,用于保证请求的幂等性。由客户端生成该参数值,要保证在不同请求间唯一,最大值不超过64个ASCII字符。
request.setClientToken(UUID.randomUUID().toString());
try {
AllocateEipAddressResponse response = client.getAcsResponse(request);
System.out.println("------------------allocateEipAddress-------------------");
System.out.println(new 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);
}
}
执行结果
正确的执行结果类似如下:
------------------allocateEipAddress-------------------
{
"requestId": "0677CF20-E6BA-429B-9616-A2456C5CA7FF",
"allocationId": "eip-uf6l5m4qrcxypyt4e6epg",
"eipAddress": "47.**.**.**",
"resourceGroupId": "rg-acfmxazb4ph6aiy"
}
------------------associateEipAddress-------------------
{"requestId":"7774E707-EC57-45F5-B22C-EEABA25FC125"}
在文档使用中是否遇到以下问题
更多建议
匿名提交