Call the EDAS API with the SDK for Java

更新时间:
复制 MD 格式

Enterprise Distributed Application Service (EDAS) provides a Java SDK that wraps the EDAS API into typed request and response objects. This guide covers SDK installation, authentication setup, and a working example that lists applications and their deploy groups.

Prerequisites

Before you begin, make sure that you have:

  • Java Development Kit (JDK) 1.6 or later. Download it from Oracle JDK

  • Apache Maven (required for the Maven installation method)

Install the SDK

Choose a method based on whether your build machine has Internet access.

Option 1: Add Maven dependencies (recommended)

If your build machine has Internet access, add the following dependencies to your pom.xml:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-edas</artifactId>
    <version>3.18.0</version>
</dependency>

Option 2: Import JAR packages manually

If your build machine does not have Internet access, download the JAR packages from a connected machine and copy them to your project:

  1. Download Alibaba Cloud SDK for Java (the core SDK).

  2. Download Alibaba Cloud EDAS SDK for Java (the EDAS-specific SDK).

  3. Add both JAR files to your project classpath.

Set up authentication

Every API call requires an AccessKey ID and an AccessKey secret that belong to your Alibaba Cloud account or a RAM user. For details on these and other common parameters, see Common parameters for API calls.

Create a DefaultProfile with your region ID and credentials, then pass it to a DefaultAcsClient:

// Replace with your region ID, AccessKey ID, and AccessKey secret
String regionId = "cn-hangzhou";
String accessKeyId = "<your-access-key-id>";
String accessKeySecret = "<your-access-key-secret>";

DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
<your-access-key-id>The AccessKey ID of your Alibaba Cloud account or RAM userLTAI5tXxx
<your-access-key-secret>The AccessKey secret of your Alibaba Cloud account or RAM userxXxXxXx
Note

To avoid hardcoding credentials in your source code, store the AccessKey ID and AccessKey secret in environment variables and read them at runtime.

Request and response pattern

Each EDAS API operation maps to a pair of Java classes:

ClassPurpose
<OperationName>RequestConstructs the API request. Set required parameters on this object.
<OperationName>ResponseCarries the result returned by the API.

Call DefaultAcsClient.getAcsResponse() with a request object to run the API operation and receive the corresponding response object.

For example, the ListApplication operation uses ListApplicationRequest and ListApplicationResponse. The ListDeployGroup operation uses ListDeployGroupRequest and ListDeployGroupResponse.

Example: List applications and deploy groups

The following example authenticates with EDAS, retrieves all applications in a region, and prints each application's deploy groups.

import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.edas.model.v20170801.ListApplicationRequest;
import com.aliyuncs.edas.model.v20170801.ListApplicationResponse;
import com.aliyuncs.edas.model.v20170801.ListDeployGroupRequest;
import com.aliyuncs.edas.model.v20170801.ListDeployGroupResponse;


public class ListApplicationsSimpleInfo {

    public static void main(String args[]) {

        // Step 1: Set up credentials and create the API client
        String accessKeyId = "<your-access-key-id>";       // AccessKey ID of your Alibaba Cloud account or RAM user
        String accessKeySecret = "<your-access-key-secret>"; // AccessKey secret of your Alibaba Cloud account or RAM user
        String regionId = "cn-hangzhou";                     // Region where your applications are deployed

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // Step 2: Build and send the ListApplication request
        ListApplicationRequest appListRequest = new ListApplicationRequest();
        try {
            ListApplicationResponse appListResponse = client.getAcsResponse(appListRequest);

            // Step 3: Check the response and iterate over applications
            if (appListResponse.getCode() == 200) {
                List<ListApplicationResponse.Application> apps = appListResponse.getApplicationList();
                if (apps != null && apps.size() > 0) {
                    for (ListApplicationResponse.Application app : apps) {
                        String appName = app.getName();
                        String appId = app.getAppId();
                        System.out.println("Application name: " + appName + ", Application ID: " + appId);

                        // Step 4: For each application, retrieve its deploy groups
                        ListDeployGroupRequest dgRequest = new ListDeployGroupRequest();
                        dgRequest.setAppId(appId);
                        ListDeployGroupResponse dgResponse = client.getAcsResponse(dgRequest);

                        if (dgResponse.getCode() == 200) {
                            List<ListDeployGroupResponse.DeployGroup> groups = dgResponse.getDeployGroupList();
                            for (ListDeployGroupResponse.DeployGroup group : groups) {
                                String groupName = group.getGroupName();
                                if ("_DEFAULT_GROUP".equals(groupName)) {
                                    groupName = "Default group";
                                }
                                String groupId = group.getGroupId();
                                System.out.println("\tGroup name: " + groupName + ", Group ID: " + groupId);
                            }
                        }
                    }
                } else {
                    System.out.println("No applications found. Verify that applications exist in the " + regionId + " region.");
                }
            } else {
                // Print the error details
                System.out.println("API call failed.\nMessage: " + appListResponse.getMessage()
                        + "\nRequestId: " + appListResponse.getRequestId());
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

Automatic endpoint discovery

When aliyun-java-sdk-core is version 4.4.3 or later and aliyun-java-sdk-edas is version 2.52.1 or later, the SDK resolves the correct API endpoint automatically based on the region ID. No manual endpoint configuration is required.