All Products
Search
Document Center

Auto Scaling:Auto Scaling SDK for Java: Procedures and usage examples

Last Updated:Apr 09, 2025

This topic describes how to use Auto Scaling SDK for Java to query Elastic Compute Service (ECS) instances in a scaling group.

Background information

  • Alibaba Cloud Developer Center provides Auto Scaling SDK for Java and the Maven project dependencies of the Alibaba Cloud SDK core library. You can write code to call Alibaba Cloud SDKs to access Alibaba Cloud services.

  • The example provided in this topic is used to describe how to query the list of ECS instances in a specific scaling group by using Auto Scaling SDK for Java. For information about scaling groups, see Overview.

  • Earlier and later SDK versions use different methods, classes, and objects. If you are currently using an earlier SDK version, we recommend that you update to the latest SDK version. This way, you can take advantage of the new features and enhancements.

Prerequisites

  • An AccessKey pair is created. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For information about how to create an AccessKey pair, see Create an AccessKey pair.

    Important
    • To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you create a Resource Access Management (RAM) user, grant the RAM user the permissions to access Auto Scaling, and then use the AccessKey pair of the RAM user to call Auto Scaling SDK for Java. For more information, see System policies for Auto Scaling.

    • The AccessKey secret of a RAM user is displayed only when you create the AccessKey pair. You cannot view the AccessKey secret after you create the AccessKey pair. After you create the AccessKey pair, keep the AccessKey secret confidential.

  • Environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows. In this topic, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are used to configure identity verification for the Alibaba Cloud Credentials tool. This allows you to access Alibaba Cloud OpenAPI Explorer without the need for a hard-coded plaintext AccessKey pair.

  • Java Development Kit (JDK) V1.8 or later is downloaded and installed. For more information, see Install the JDK on Windows.

Use Auto Scaling SDK for Java

Manually integrate Maven project dependencies

This section describes how to use Auto Scaling SDK for Java to query the list of ECS instances in a scaling group.

Step 1: Install Auto Scaling SDK for Java

  1. Use one of the following methods to configure Maven in IntelliJ IDEA:

    • Method 1: Use Maven that is integrated in IntelliJ IDEA.

    • Method 2: Download Apache Maven based on your OS and manually configure Maven.

  2. Visit Alibaba Cloud SDK Center, enter Auto Scaling in the search box, and obtain the SDK core library of Alibaba Cloud and Auto Scaling Maven project dependencies.

  3. Create a Maven project by using one of the following methods:

    • Method 1: Add a Maven project to IntelliJ IDEA.maven1

    • Method 2: Convert an existing project into a Maven project.

      1. Right-click the project that you want to convert and select Add Framework Support....maven2

      2. In the Add Frameworks Support window, select Maven and click OK.maven3

  4. In the pom.xml file, create a <dependencies></dependencies> tag and add the following dependencies:

    After you add the dependencies, click the Refresh button in the upper-right corner, or right-click the project name and choose Maven > Reload project to download Maven dependencies to the project.

    Note

    SDK packages are frequently updated. We recommend that you obtain the latest version of dependencies from the Alibaba Cloud SDK official website.

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>0.2.8</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>ess20220222</artifactId>
      <version>1.5.2</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>credentials-java</artifactId>
      <version>0.2.11</version>
    </dependency>

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. Use the Credentials tool to obtain the AccessKey pair from environment variables.

    Sample code:

    com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
    com.aliyun.teaopenapi.models.Config config = new Config();
    config.setCredential(credentialClient);
    config.setEndpoint("ess.aliyuncs.com");
  2. Generate the client object.

    Sample code:

    com.aliyun.ess20220222.Client client = new com.aliyun.ess20220222.Client(config);
  3. Create a request and use a constructor to generate a default request class.

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

    DescribeScalingInstancesRequest request = new DescribeScalingInstancesRequest();
  4. Configure parameters for the request class.

    Call the setXxx method of the request class to configure the required parameters for the API 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 specifies that the ID of the ECS instance is i-bp15itgtxemxy7v9****.

    request.setInstanceId("i-bp15itgtxemxy7v9****");
  5. Call the describeScalingInstancesWithOptions class of the client object to obtain the response to the request.

    Sample code:

    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    DescribeScalingInstancesResponse response = client.describeScalingInstancesWithOptions(request,runtime);
    Note

    For information about the specific procedure, see the Procedure section on the Java tab of Auto Scaling in Alibaba Cloud SDK Center.

Sample code

The following sample code provides an example of a complete code of Auto Scaling SDK for Java:

import com.aliyun.ess20220222.models.DescribeScalingInstancesRequest;
import com.aliyun.ess20220222.models.DescribeScalingInstancesResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

public class DescribeScalingInstances {
    public static void main(String[] args) throws Exception {
        com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new Config();
        config.setCredential(credentialClient);
        config.setEndpoint("ess.aliyuncs.com");
        com.aliyun.ess20220222.Client client = new com.aliyun.ess20220222.Client(config);
        DescribeScalingInstancesRequest request = new DescribeScalingInstancesRequest();
        request.setRegionId("cn-hangzhou");
        request.setScalingGroupId("asg-XXXXXXXX");
        request.setScalingConfigurationId("asc-XXXXXXXXX");
        request.setPageNumber(1);
        request.setPageSize(10);

        List<String> instanceIdList = new ArrayList<String>();
        instanceIdList.add("i-XXXXXX");
        request.setInstanceIds(instanceIdList);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        DescribeScalingInstancesResponse response = client.describeScalingInstancesWithOptions(request,runtime);
        System.out.println(new Gson().toJson(response));
    }
}

Execution 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
}

Download the SDK demo from OpenAPI Explorer

This section describes how to use Auto Scaling SDK for Java to create a scaling group.

Step 1: Obtain the SDK sample code

You can use Alibaba Cloud OpenAPI Explorer to call Auto Scaling API operations to automatically generate SDK sample code.

  1. Click OpenAPI Explorer.

  2. Download the complete SDK project by following the steps shown in the following figure: Decompress the downloaded SDK project.

2024-07-09_10-47-58.png

Step 2: Run the SDK sample code

  1. Open IntelliJ IDEA, choose File > Open, select the decompressed project folder, and then wait for Maven to automatically install dependencies.

  2. Run the sample code

    Double-click Sample. Confirm that no error occurs and run the sample code.

  3. View the result.

    You can log on to the Auto Scaling console to view the created scaling group.

Sample code

To view the complete sample code, see SDK Sample Code.

What to do next

A scaling group does not immediately take effect after you create the scaling group. You must call the EnableScalingGroup operation to enable the scaling group before scaling events can be triggered or scaling rules can be executed in the scaling group.

References

  • For more information about how to query ECS instances in a scaling group by calling an API operation, see DescribeScalingInstances.

  • For more information about how to create a scaling group by calling an API operation, see CreateScalingGroup.

  • For more information about Alibaba Cloud SDKs, see Alibaba Cloud SDKs.

  • You can decide whether to turn on Common Request when you download the sample code based on your business requirements. For more information, see Generic calls and specialized calls.