开发人员可以通过SDK创建阿里云国际版的海外云服务器ECS实例,下面Unirech小编将简单介绍了如何通过Java SDK创建海外云服务器ECS实例。
一、准备环节
在使用Java SDK创建ECS实例之前,您需要配置Java SDK环境,并向Maven项目pom.xml文件添加Aliyun-java-sdk-core、海外云服务器aliyun-java-sdk-ecs、专有网络aliyun-java-sdk-vpc和fastjson依赖项。
在pom.xml文件中添加专有网络aliyun-java-sdk-vpc依赖项,如下所示。
<dependencies><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.4.3</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-ecs</artifactId><version>4.17.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.60</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-vpc</artifactId><version>3.0.9</version></dependency></dependencies>
二、获取 ACCESSKEY 信息
若要创建AccessKey操作,请参考下图获取AccessKey。
说明为了避免AccessKey从主账号泄露带来的安全隐患,建议您创建一个RAM用户,授予RAM用户与海外云服务器ECS相关的访问权限,然后使用RAM用户的AccessKey调用SDK。
三、创建海外云服务器ECS实例分析所需信息资源
在创建 海外云服务器ECS 实例之前,需要创建一个私有网络 VPC 和 SecurityGroup。
注意,如果私有网络 VPC 和 Security Group 已经存在,您还可以获取交换机 ID 和 Security Group ID,并直接购买海外云服务器 ECS 实例。
1.创建VPC。
创建一个VPC网段为192.168.0.0u002F16的专用网络华东VPC 1(杭州)。
以下代码示例表示创建VPC。
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.google.gson.Gson;
import com.aliyuncs.vpc.model.v20160428.*;
public class CreateVpc {public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","<accessKeyId>", "<accessSecret>");IAcsClient client = new DefaultAcsClient(profile);CreateVpcRequest request = new CreateVpcRequest();request.setRegionId("cn-hangzhou");request.setCidrBlock("192.168.0.0/16");try {CreateVpcResponse response = client.getAcsResponse(request);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());}}
}
返回结果如下所示。{"requestId":"5BE6AEA4-347F-46A9-9808-B429EF02****","vpcId":"vpc-bp1h99qfh290thxml****","vRouterId":"vrt-bp1cbum5ozelljyet****","routeTableId":"vtb-bp1qm6p3yoww2cv10****","resourceGroupId":"rg-acfmzw2jz2z****"
}
2.创建交换机。
在VPC中创建一个交换机,交换机段为192.168.0.0/24。
下面的代码示例表示创建交换机。
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.google.gson.Gson;
import java.util.*;
import com.aliyuncs.vpc.model.v20160428.*;
public class CreateVSwitch {public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");IAcsClient client = new DefaultAcsClient(profile);CreateVSwitchRequest request = new CreateVSwitchRequest();request.setRegionId("cn-hangzhou");request.setCidrBlock("192.168.0.0/24");request.setVpcId("vpc-bp1h99qfh290thxml****");request.setZoneId("cn-hangzhou-i");try {CreateVSwitchResponse response = client.getAcsResponse(request);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());}}
}
返回结果如下所示。{"requestId": "BAFBC8C4-3C65-427B-B470-3D257288****","vSwitchId": "vsw-bp1mihse903i05oxn****"
}
3.创建安全组
下面的代码示例显示了安全组的创建。
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.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class CreateSecurityGroup {public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");IAcsClient client = new DefaultAcsClient(profile);CreateSecurityGroupRequest request = new CreateSecurityGroupRequest();request.setRegionId("cn-hangzhou");request.setVpcId("vpc-bp1h99qfh290thxml****");try {CreateSecurityGroupResponse response = client.getAcsResponse(request);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());}}
}
返回结果如下所示。
{"requestId": "718D29C6-6183-4196-AD76-A53F6A6E****","securityGroupId": "sg-bp1dve08xy2c8y9g****"
}
4.在安全组中添加入发展方向进行放行规则。
下面的代码示例表示添加安全组规则。
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.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class AuthorizeSecurityGroup {public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");IAcsClient client = new DefaultAcsClient(profile);AuthorizeSecurityGroupRequest request = new AuthorizeSecurityGroupRequest();request.setRegionId("cn-hangzhou");request.setSecurityGroupId("sg-bp1dve08xy2c8y9g****");request.setIpProtocol("tcp");request.setPortRange("22/22");request.setSourceCidrIp("0.0.0.0/0");try {AuthorizeSecurityGroupResponse response = client.getAcsResponse(request);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());}}
}
返回结果如下所示。
{"requestId": "7052E70F-4678-4400-81CF-E0133CCB****"
}
四、购买海外云服务器ECS实例
购买一个包年包月的海外云服务器 ECS 实例。
下面的代码示例显示了如何创建每月订阅的海外云服务器ECS实例。
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.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class RunInstances {public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");IAcsClient client = new DefaultAcsClient(profile);RunInstancesRequest request = new RunInstancesRequest();request.setRegionId("cn-hangzhou");request.setImageId("aliyun_2_1903_x64_20G_alibase_20200324.vhd");request.setInstanceType("ecs.s6-c1m2.small");request.setSecurityGroupId("sg-bp1dve08xy2c8y9g****");request.setVSwitchId("vsw-bp1mihse903i05oxn****");request.setInstanceName("ecs_sdk_demo");request.setInternetMaxBandwidthOut(1);request.setPassword("<yourPassword>");request.setPeriod(1);request.setPeriodUnit("Month");request.setInstanceChargeType("PrePaid");try {RunInstancesResponse response = client.getAcsResponse(request);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());}}
}
返回结果如下所示。
{"requestId": "9582F9F2-349C-438E-A6A2-3E7B6B56****","tradePrice": ****,"instanceIdSets": ["i-bp1hcv43i3glqxbv****"]
}
五、连接ECS实例
此示例进行介绍可以通过Cloud Shell登录Linux实例。
1.查询实例公网IP地址
下面的代码示例表示查询实例公共网络IP。
aliyun ecs DescribeInstances
--RegionId cn-hangzhou
--InstanceIds '["i-bp1hcv43i3glqxbv****"]'
在返回的结果中找到了以下公共网络 IP 信息。
2.通过SSH登录ECS实例。
六、释放ECS实例
包年包月海外云服务器ecs实例进行到期后,您可以通过手动控制释放。如果一直未续费,实例也会自动释放。
Unirech是阿里云国际版金牌分销商,提供多币种支付代充值,关联Unirech注册无需绑定paypal,阿里云国际站相对于国内站限制更少,账户不易被风控,注册、充值更加便捷,[阿里云代充](https://t.me/kimkim7777就找Unirech!