All Products
Search
Document Center

Enterprise Distributed Application Service:Create an application in an ECS cluster by calling an API operation

Last Updated:Jun 16, 2023

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to create an application in an Elastic Compute Service (ECS) cluster by calling an API operation.

Prerequisites

Before you call the API operation to create an application in an ECS cluster, make sure that the following prerequisites are met:

  • EDAS SDK for Java is installed. For more information, see "Use EDAS SDK for Java to call EDAS API".

  • The ID of the region in which you want to create the application is obtained. In this example, the region ID is cn-hangzhou.

  • A microservice namespace is created. For more information, see Create a microservice namespace.

    If a microservice namespace already exists, you can call the ListUserDefineRegion operation to query custom microservice namespaces and obtain the ID of the microservice namespace from the RegionId parameter.

  • An ECS cluster is created. For more information how to create an ECS cluster, see Create an ECS cluster by calling an API operation.

    If you have created an ECS cluster, you can call the ListCluster operation to query ECS clusters and obtain the ID of the specified ECS cluster. The ID of the ECS cluster is specified by the ClusterId parameter. For example, the ID of the ECS cluster is 369d06d7-450b-4f3d-bf75-9536fcd9****.

  • ECS instances are added to the ECS cluster. For more information, see Scale out an ECS cluster by calling an API operation.

  • The unique IDs of Elastic Compute Units (ECUs) are obtained. You can call the ListScaleOutEcu operation to query the ECUs in the ECS cluster and obtain the ECU IDs from the EcuId parameter. In this example, the ECU IDs are 82a0ac57-433d-4ad1-ae89-0c6fcb70**** and e2674482-1214-4c93-b767-9b6405e2****.

  • If you want to create a High-Speed Service Framework (HSF) application, make sure that the version of EDAS Container is obtained. You can call the ListBuildPack operation to query EDAS Container versions and obtain the version from the ConfigId parameter. In this example, the EDAS Container version is 57.

Create an application

The following sample code provides an example on how to create an application in an ECS cluster in EDAS. The sample code does not contain the settings of mounted scripts and Java Virtual Machine (JVM) parameters. For more information about operation-specific parameters, see InsertApplication.

Note

You can use the sample code to create a Spring Cloud or Dubbo application. To create an HSF application, you must add the following information about EDAS Container to the sample code:

// The build package number of EDAS Container. You must specify this parameter when you create an HSF application. You can call the ListBuildPack operation and obtain the build package number from the ConfigId parameter. 
request.setBuildPackId(57);
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.InsertApplicationRequest;
import com.aliyuncs.edas.model.v20170801.InsertApplicationResponse;

public class InsertApplication {

    public static void main(String[] args)  {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in EDAS is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables. You can save your AccessKey ID and AccessKey secret in your configuration file based on your business requirements. 
        // To prevent key leaks, we recommend that you do not save your AccessKey ID and AccessKey secret in the code. 
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
        // The ID of the region where you want to create the application. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Create an API request and configure the related parameters. 
        InsertApplicationRequest request = new InsertApplicationRequest();
        // The ID of the ECS cluster in which you want to create the application. 
        request.setClusterId("369d06d7-450b-4f3d-bf75-9536fcd993dc");
        // The description of the application. 
        request.setDescription("create by edas pop api");
        // The custom name of the application. The name can be up to 36 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). The name must start with a letter. 
        request.setApplicationName("ECSAPP_API");
        // The ID of the microservice namespace. 
        request.setLogicalRegionId("cn-hangzhou:doc");
        // The ECU IDs of ECS instances in the ECS cluster. An ECU ID uniquely identifies an ECS instance that is imported to EDAS. Separate multiple ECU IDs with commas (,). 
        request.setEcuInfo("82a0ac57-433d-4ad1-ae89-0c6fcb70****,e2674482-1214-4c93-b767-9b6405e2****");
        // The type of the application deployment package. Valid values: war and jar. 
        request.setPackageType("jar");
        // The ID of the application component. You must specify this parameter when you create a Spring Cloud or Dubbo application. You do not need to specify this parameter when you create an HSF application. 
        // A value of 4 specifies Apache Tomcat 7.0.91. A value of 7 specifies Apache Tomcat 8.5.42. A value of 5 specifies OpenJDK 1.8.x. A value of 6 specifies OpenJDK 1.7.x. 
        request.setComponentIds("5");
        // Specifies whether to enable the health check URL. A value of true specifies to enable the URL. A value of false specifies to disable the URL. 
        request.setEnableUrlCheck(true);
        // The health check URL of the application. 
        request.setHealthCheckUrl("http://127.0.0.1:8080/_ehc.html");



        try {
            InsertApplicationResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage() + "\nAppName=" + response.getApplicationInfo().getAppName() + "\nAppId=" + response.getApplicationInfo().getAppId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

The following result is returned after you run the sample code:

Message=success
AppName=ECSAPP_API
AppId=6275ffd0-d577-4b5a-894f-e92991b8****

Check the result

After you create the application in the ECS cluster in EDAS, call the ListApplication operation to query applications. View the values of parameters such as AppId and Appname in the response and check whether the application is created.

The following result is returned after you call the ListApplication operation:

{
  "Message": "success",
  "RequestId": "9363A94A-96C5-4B41-93A9-001A4E9DDEE1",
  "ApplicationList": {
    "Application": [
      {
        "BuildPackageId": 0,
        "ApplicationType": "FatJar",
        "AppId": "6275ffd0-d577-4b5a-894f-e92991b8****",
        "ResourceGroupId": "rg-acfm3umruf2****",
        "ClusterId": "369d06d7-450b-4f3d-bf75-9536fcd9****",
        "RegionId": "cn-hangzhou:doc",
        "ClusterType": 2,
        "RunningInstanceCount": 0,
        "Name": "ECSAPP_API"
      }
    ]
  },
  "Code": 200
}