本教程详细介绍如何使用Alibaba Cloud SDK for Java创建和删除SLB实例。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用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-slb -->
      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-slb</artifactId>
        <version>3.2.13</version>
      </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.slb.model.v20140515.CreateLoadBalancerRequest;
import com.aliyuncs.slb.model.v20140515.CreateLoadBalancerResponse;
import com.aliyuncs.slb.model.v20140515.DeleteLoadBalancerRequest;
import com.aliyuncs.slb.model.v20140515.DeleteLoadBalancerResponse;
import com.google.gson.Gson;

/**
 * 使用Java SDK创建和删除负载均衡实例使用示例
 */
public class CreateAndDeleteSlbDemo {


    /**
     * 创建负载均衡实例
     *
     * @param "CreateLoadBalancerRequest"
     * @param "IAcsClient"
     * @return "CreateLoadBalancerResponse"
     */
    private CreateLoadBalancerResponse createLoadBalancer(CreateLoadBalancerRequest request, IAcsClient client) {

        // 初始化返回对象
        CreateLoadBalancerResponse response = null;
        try {
            // 调用SDK发送请求
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
        return response;
    }

    /**
     * 删除负载均衡实例
     *
     * @param "DeleteLoadBalancerRequest"
     * @param "IAcsClient"
     * @return "DeleteLoadBalancerResponse"
     */
    private DeleteLoadBalancerResponse deleteLoadBalancer(DeleteLoadBalancerRequest request, IAcsClient client) {
        // 初始化返回对象
        DeleteLoadBalancerResponse response = null;
        try {
            // 调用SDK发送请求
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
        return response;
    }

    public static void main(String[] args) {

        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-zhangjiakou",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        CreateAndDeleteSlbDemo createAndDeleteSlbDemo = new CreateAndDeleteSlbDemo();

        // 初始化创建SLB实例方法入参对象
        CreateLoadBalancerRequest createLoadBalancerRequest = new CreateLoadBalancerRequest();
        // 设置新建SLB实例的主可用区为cn-zhangjiakou-a
        createLoadBalancerRequest.setMasterZoneId("cn-zhangjiakou-a");
        // 设置新建SLB实例的备可用区为cn-zhangjiakou-b
        createLoadBalancerRequest.setSlaveZoneId("cn-zhangjiakou-b");
        // 设置新建SLB实例的名称为SLB1
        createLoadBalancerRequest.setLoadBalancerName("SLB1");
        // 设置新建SLB实例的计费类型为按量计费
        createLoadBalancerRequest.setPayType("PayOnDemand");
        // 设置新建SLB实例的规格为slb.s1.small
        createLoadBalancerRequest.setLoadBalancerSpec("slb.s1.small");
        // 设置监听的带宽峰值
        createLoadBalancerRequest.setBandwidth(1);

        // 调用创建SLB实例方法,获取新实例参数
        CreateLoadBalancerResponse createLoadBalancerResponse = createAndDeleteSlbDemo.createLoadBalancer(createLoadBalancerRequest, client);
        System.out.println("-------------------------------createLoadBalancer-------------------------------");
        System.out.println(new Gson().toJson(createLoadBalancerResponse));

        // 初始化删除SLB实例方法入参对象
        DeleteLoadBalancerRequest deleteLoadBalancerRequest = new DeleteLoadBalancerRequest();
        // 设置要删除的SLB实例ID,此处使用刚才创建的实例ID
        deleteLoadBalancerRequest.setLoadBalancerId(createLoadBalancerResponse.getLoadBalancerId());
        // 调用删除SLB实例方法
        DeleteLoadBalancerResponse deleteLoadBalancerResponse = createAndDeleteSlbDemo.deleteLoadBalancer(deleteLoadBalancerRequest, client);
        System.out.println("-------------------------------deleteLoadBalancer-------------------------------");
        System.out.println(new Gson().toJson(deleteLoadBalancerResponse));
    }
}

运行结果

正确的运行结果类似如下:
-------------------------------createLoadBalancer-------------------------------
{
    "requestId": "A10A122E-5527-4642-A20B-12D6F8BB1D3D", 
    "loadBalancerId": "lb-8vbmp1b3xxxxxxxxkp9l0", 
    "resourceGroupId": "rg-acfxxxxxxxx6aiy", 
    "address": "39.xx.xx.164", 
    "loadBalancerName": "SLB1", 
    "vpcId": "", 
    "vSwitchId": "", 
    "networkType": "classic", 
    "addressIPVersion": "ipv4"
}
-------------------------------deleteLoadBalancer-------------------------------
{
    "requestId": "B78D96B9-0685-4CC7-9CF2-00A793ACC1B2"
}