All Products
Search
Document Center

Enterprise Distributed Application Service:Create a Java application from a JAR package or WAR package in a Kubernetes cluster by calling an API operation

Last Updated:Oct 24, 2023

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 Use EDAS SDK for Java to call EDAS API.

  • The region in which you want to deploy the application is determined.

  • 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.

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

    If a cluster is already imported to the microservice namespace, you can call the ListCluster operation to query clusters and obtain the cluster ID from the ClusterId parameter.

  • The JAR package or WAR package that is used to deploy the application is uploaded to the destination URL. In this example, the destination URL 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 sample code provides an example on how to create a Java application from a JAR package or WAR package in a Kubernetes cluster in EDAS. The sample code does not contain settings of advanced parameters such as scheduling rules, startup commands, and environment variables. For more information about operation-specific parameters, 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 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 included 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 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 configure the related parameters. 
        InsertK8sApplicationRequest request = new InsertK8sApplicationRequest();
        // The name of the application. The name can be up to 36 characters in length, and can contain digits, letters, and hyphens (-). The name must start with a letter.  
        request.setAppName("edas-SDK0414java");
        // The ID of the cluster. 
        request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
        // The number of application instances. 
        request.setReplicas(2);
        // The CPU quota and memory quota for application instances when the application runs. A value of 0 specifies that no quotas are set. 
        request.setLimitCpu(0);
        request.setLimitMem(0);
        request.setRequestsCpu(0);
        request.setRequestsMem(0);
        // The ID of the EDAS microservice namespace. This parameter is required 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 FatJar 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 the 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();
        }
    }
}

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

AppId=8ae9cfdb-c20c-4de0-9b21-2b31e7b6****
AppName=edas-SDK0414java
ChangeOrderId=44a1402a-9e59-4494-baa9-f18877e0****

Check 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 operation-specific parameters, see GetChangeOrderInfo.

The following result 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 check whether the application is created based on the value of the changeOrderInfo.Status parameter. Valid values of the changeOrderInfo.Status parameter:

  • 0: The application is ready to be created.

  • 1: The application is being created.

  • 2: The application is created.

  • 3: The application failed to be created.

  • 6: The creation of the application is terminated.

  • 8: The application is waiting for you to manually trigger the next phase during a manual phased release.

  • 9: The application is waiting for the system to trigger the next phase during an automatic phased release.

  • 10: The application failed to be created due to a system exception.

Note

If the value of the changeOrderInfo.Status parameter is 3 in the preceding result, the application failed 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.