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, make sure that the following prerequisites are met:
  • The ID of the region in which you want to create the application is obtained. For example, the region ID is cn-hangzhou.
  • A microservice namespace is created. For example, a microservice namespace with the ID of cn-hangzhou:doc is created. For more information, see Create a namespace.

    If a microservice namespace already exists, you can call the ListUserDefineRegion operation to query custom microservice namespace and obtain the value of the RegionId parameter for the microservice namespace that you want to use. For example, the value is cn-hangzhou:doc.

  • 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. The ECU IDs are specified by the EcuID parameter. For example, the ECU IDs are 82a0ac57-433d-4ad1-ae89-0c6fcb70**** and e2674482-1214-4c93-b767-9b6405e2****.
  • The ID of the container version to be used is obtained if you want to create a High-Speed Service Framework (HSF) application. You can call the ListBuildPack operation to query container versions and obtain the ID of the specified container version. The ID of the container version is specified by the ConfigId parameter. For example, the ID of the container version is 57.

Create an application

The following code provides an example on how to create an application in an ECS cluster in EDAS. The code does not contain mount script information or JVM parameter settings. For more information about how to configure such information, see InsertApplication.

Note You can use the code to create a Spring Cloud or Dubbo application. To create an HSF application, you must add the following EDAS Container information to the code:
// The build package number of EDAS Container. This parameter is required to create an HSF application. You can call the ListBuildPack operation to query the build package number. The build package number is specified by 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 ID of your Alibaba Cloud account or a RAM user within the Alibaba Cloud account. 
        String aliyun_user_ak = "<yourAccessKeyId>";
        // The AccessKey secret of your Alibaba Cloud account or a RAM user within the account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which 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 set the 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 must start with a letter and can contain letters, digits, underscores (_), and hyphens (-). The name can be up to 36 characters in length. 
        request.setApplicationName("ECSAPP_API");
        // The region 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 component that is used by the application. You need to specify the component ID when you create a Spring Cloud, Spring Boot, or Dubbo application. You do not need to set this parameter when you create an HSF application. 
        // A value of 4 indicates Apache Tomcat 7.0.91. A value of 7 indicates Apache Tomcat 8.5.42. A value of 5 indicates OpenJDK 1.8.x. A value of 6 indicates OpenJDK 1.7.x. 
        request.setComponentIds("5");
        // Specifies whether to enable the health check URL. A value of true indicates that the URL is enabled. A value of false indicates that the URL is disabled. 
        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();
        }
    }
}
Sample response:
Message=success
AppName=ECSAPP_API
AppId=6275ffd0-d577-4b5a-894f-e92991b8****

Verify the result

After you create the application in the ECS cluster in EDAS, you can call the ListApplication operation to query applications. Check the parameters such as AppId and Appname in the response and determine whether the application is created.

Sample response that 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
}