This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to create a Java application from a JAR package or WAR package in a Kubernetes 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:
  • EDAS SDK for Java is installed. For more information, see Guide of using EDAS SDK for Java to call the EDAS API.
  • The ID of the region in which you want to deploy 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.

  • The cluster in which you want to create an application is imported to the microservice namespace. For example, the ID of the cluster is da60f685-558a-4e00-b549-15e9143d****. For more information, see Import a Kubernetes cluster.

    If a cluster has been imported to the microservice namespace, you can call the ListCluster operation to query clusters and obtain the ID of the cluster in which you want to create an application.

  • The JAR package or WAR package that is used to deploy the application is uploaded to the destination storage system. For example, the path of the JAR package in Object Storage Service (OSS) is https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.1-SNAPSHOT.jar.

Create a Java application from a JAR package or WAR package

The following code provides an example on how to create a Java application from a JAR package or WAR package in a Kubernetes cluster in EDAS. For more information about advanced parameters, such as parameters that are related to scheduling rules, startup commands, and environment variables, see InsertK8sApplication.
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.InsertK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.InsertK8sApplicationResponse;

public class InsertK8sApplication {

    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 Alibaba Cloud account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which you want to create an 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. 
        InsertK8sApplicationRequest request = new InsertK8sApplicationRequest();
        // The name of the application. The name must start with a letter and can contain digits, letters, and hyphens (-). The name must be 1 to 36 characters in length. 
        request.setAppName("edas-SDK0414java");
        // The ID of the cluster. 
        request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
        // The number of instances for the application. 
        request.setReplicas(2);
        // The CPU quota and memory quota for application instances. A value of 0 indicates that no quotas are set. 
        request.setLimitCpu(0);
        request.setLimitMem(0);
        request.setRequestsCpu(0);
        request.setRequestsMem(0);
        // The ID of the EDAS microservice namespace. Set this parameter if you do not want to use the default microservice namespace. 
        request.setLogicalRegionId("cn-hangzhou:doc");
        // The type, URL, and version of the deployment package of the application. A fat JAR package or WAR package is supported. 
        request.setPackageType("FatJar");
        request.setPackageUrl("https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.1-SNAPSHOT.jar");
        request.setPackageVersion("20210423:1442");
        // The version of Java Development Kit (JDK) on which the deployment package of the application depends. Valid values: Open JDK 7 and Open JDK 8. 
        request.setJDK( "Open JDK 8");

        try {
            InsertK8sApplicationResponse response = client.getAcsResponse(request);
            System.out.println("AppId=" + response.getApplicationInfo().getAppId() + "\nAppName=" + response.getApplicationInfo().getAppName() + "\nChangeOrderId=" + response.getApplicationInfo().getChangeOrderId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
AppId=8ae9cfdb-c20c-4de0-9b21-2b31e7b6****
AppName=edas-SDK0414java
ChangeOrderId=44a1402a-9e59-4494-baa9-f18877e0****

Verify the result

After you create the application, you can call the GetChangeOrderInfo operation to view the change details and obtain the change status of the application. For more information about the API operation, see GetChangeOrderInfo.

Sample response that is returned after you call the GetChangeOrderInfo operation:
{
    "Message": "success",
    "RequestId": "C26DBA1C-9A38-442D-A4D5-587515C5E522",
    "Code": 200,
    "changeOrderInfo": {
        "Status": 3,
        "Desc": "Version: 20210423:1442 | Package Name: sc-provider-D-0.0.1-SNAPSHOT.jar",
        "PipelineInfoList": {
            "PipelineInfo": [
                {
                ......
                "TaskMessage": "Apply success. <br>application is ready at desired state, version: 1"
                ......
                                // The sample response is incomplete and is only for reference. 
You can view the value of the changeOrderInfo.Status parameter to check whether the application is created. Valid values of the changeOrderInfo.Status parameter:
  • 0: ready
  • 1: in progress
  • 2: successful
  • 3: failed
  • 6: terminated
  • 8: wait for manual confirmation to trigger the next phase during a manual phased release
  • 9: wait to trigger the next phase during an automatic phased release
  • 10: failed due to a system exception
Note If a value of 3 is returned for the changeOrderInfo.Status parameter, the application fails to be created. To find the cause of the failure, check the value of the TaskMessage parameter. After the issue is fixed, create the application again.