This topic describes the general process of compiling Auto Scaling SDK for Java. In this topic, the DescribeScalingInstances operation is used.

Prerequisites

  • An AccessKey pair is created. For more information, see Obtain an AccessKey pair.
    Note To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user, grant the RAM user the permissions to access Elastic Compute Service (ECS) instances, and then use the AccessKey pair of the RAM user to call Auto Scaling SDK for Java. For more information, see Control access to resources by using RAM users.
  • Auto Scaling SDK for Java and the Alibaba Cloud SDK core library are installed. For more information, see Step 1: Install Auto Scaling SDK for Java.

Background information

  • The Alibaba Cloud Developer Center provides Auto Scaling SDK for Java. It also provides the Maven project dependencies and JAR packages of the Alibaba Cloud SDK core library. You can write code to call Alibaba Cloud SDKs to access Alibaba Cloud services.
  • In this example, the DefaultProfile and IAcsClient classes are included in aliyun-java-sdk-core, and other classes are included in aliyun-java-sdk-ess.
  • In this example, ECS instances in a scaling group are queried. For information about scaling groups, see Overview.
  • The earlier and latest SDK versions differ in methods, classes, and objects. If you use an earlier SDK version, we recommend that you switch to the latest version to obtain new features.

Step 1: Install Auto Scaling SDK for Java

In this example, the Windows 10 64-bit operating system and the IntelliJ IDEA Java development tool are used.

  1. Use one of the following methods to configure Maven in IntelliJ IDEA:
    • Method 1: Use Maven that is integrated into IntelliJ IDEA.
    • Method 2: Download Apache Maven based on your OS and manually configure Maven.
  2. Go to Alibaba Cloud SDKs to obtain the Alibaba Cloud SDK core library and Maven dependencies of Auto Scaling.
  3. Use one of the following methods to create a Maven project:
    • Method 1: Create a Maven project in IntelliJ IDEA. maven1
    • Method 2: Convert an existing project to a Maven project.
      1. Right-click the project that you want to convert and select Add Framework Support.... maven2
      2. Select Maven and click OK. maven3
  4. Add the aliyun-java-sdk-core and aliyun-java-sdk-ess dependencies to the pom.xml file in the project directory.
    After the dependencies are added, Maven automatically downloads the relevant JAR packages.
    Note SDK packages are frequently updated. We recommend that you obtain the latest version of dependencies from the official Alibaba Cloud SDKs website.
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ess</artifactId>
            <version>2.3.6</version>
        </dependency>
    </dependencies>

Step 2: Compile Auto Scaling SDK for Java

In this step, the DescribeScalingInstances operation is used as an example to describe how to compile Auto Scaling SDK for Java.

  1. Generate the profile object from the DefaultProfile class.

    The profile object stores the AccessKey ID, AccessKey secret, and region ID. In this example, cn-hangzhou is used. For more information about regions, see Regions and zones.

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyID>", "<yourAccessKeySecret>");
  2. Generate the client object of IAcsClient from the DefaultProfile class.

    You can obtain subsequent responses from DefaultProfile.

    IAcsClient client = new DefaultAcsClient(profile);
  3. Create a request and use a constructor to generate a default request class.

    The class is named after the operation followed by Request. In this example, the operation used to query ECS instances in a scaling group is named DescribeScalingInstances. Therefore, the request class is named DescribeScalingInstancesRequest.

    DescribeScalingInstancesRequest request = new DescribeScalingInstancesRequest();
  4. Configure request parameters.
    Call the setXxx method of the request class to configure the required parameters for the operation. Parameters in this example:
    • The DescribeScalingInstances operation uses the RegionId parameter to specify the region ID.
    • The DescribeScalingInstances operation uses the InstanceId parameter to specify the ID of the ECS instance in the scaling group that you want to query. In this example, the setInstanceId parameter is set to i-bp15itgtxemxy7v9****, which indicates that the ID of the ECS instance that you want to query is i-bp15itgtxemxy7v9****.
    request.setInstanceId("i-bp15itgtxemxy7v9****");
  5. Use the client object to obtain the response to the request.
    DescribeScalingInstancesResponse response = client.getAcsResponse(request);
    System.out.println(new Gson().toJson(response));
  6. Call the catch() method to handle server and client errors.
    • Server error
      catch (ServerException e) {
                  e.printStackTrace();
                                 }
    • Client error
      catch (ClientException e) {
                  System.out.println("ErrCode:" + e.getErrCode());
                  System.out.println("ErrMsg:" + e.getErrMsg());
                  System.out.println("RequestId:" + e.getRequestId());
                                 }

Result

  • Sample success response:
    {
        "TotalCount": 2,
        "RequestId": "5E65F238-4B45-54B6-BE15-A34956FA0D96",
        "PageSize": 10,
        "PageNumber": 1,
        "ScalingInstances": {
            "ScalingInstance": [
                {
                    "LoadBalancerWeight": 50,
                    "CreatedTime": "2022-04-27T03:24:58Z",
                    "WarmupState": "NoNeedWarmup",
                    "ZoneId": "cn-hangzhou-i",
                    "InstanceId": "i-bp1h3dog530vvnqm****",
                    "ScalingActivityId": "asa-bp16rgo6477ojn3u****",
                    "ScalingGroupId": "asg-bp10uuhy2wbb2tip****",
                    "CreationTime": "2022-04-27T03:24Z",
                    "HealthStatus": "Healthy",
                    "LifecycleState": "Protected",
                    "Entrusted": false,
                    "CreationType": "Attached"
                },
                {
                    "LoadBalancerWeight": 50,
                    "CreatedTime": "2022-04-27T06:38:21Z",
                    "ZoneId": "cn-hangzhou-i",
                    "InstanceId": "i-bp15itgtxemxy7v9****",
                    "ScalingActivityId": "asa-bp1ejteup0bntltl****",
                    "ScalingGroupId": "asg-bp10uuhy2wbb2tip****",
                    "HealthStatus": "Healthy",
                    "LifecycleState": "Stopped",
                    "CreationType": "AutoCreated",
                    "WarmupState": "NoNeedWarmup",
                    "CreationTime": "2022-04-27T06:38Z",
                    "Entrusted": true,
                    "ScalingConfigurationId": "asc-bp1433dp4gktwkmi****"
                }
            ]
        },
        "TotalSpotCount": 0
    }
  • Obtain the values of specific response parameters, such as InstanceId and ScalingGroupId.
    i-bp15itgtxemxy7v9****
    asg-bp10uuhy2wbb2tip****

Sample code

The following example shows the complete code of Auto Scaling SDK for Java:

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.ess.model.v20140828.*;

public class DescribeScalingInstances {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeScalingInstancesRequest request = new DescribeScalingInstancesRequest();
        request.setRegionId("cn-hangzhou");
        request.setScalingGroupId("asg-bp10uuhy2wbb2tip****");
        request.setScalingConfigurationId("asc-bp1433dp4gktwkmi****");
        request.setPageNumber(1);
        request.setPageSize(10);

        List<String> instanceIdList = new ArrayList<String>();
        instanceIdList.add("i-bp15itgtxemxy7v9****");
        request.setInstanceIds(instanceIdList);

        try {
            DescribeScalingInstancesResponse 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());
        }

    }
}

References

DescribeScalingInstances