All Products
Search
Document Center

Enterprise Distributed Application Service:Perform a canary release for an application in an ECS cluster by calling an API operation

Last Updated:Jun 19, 2023

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

Prerequisites

Before you call the API operation to perform a canary release for 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 the application is created 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.

  • The application is created in an ECS cluster. For more information, see Create an application in an ECS cluster by calling an API operation.

    If the application is already created in an ECS cluster, you can call the ListApplication operation to query applications and obtain the application ID from the AppId parameter. In this example, the application ID is 6bbc57a2-a017-4bec-b521-49a15bd3****.

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

  • The ID of the application instance group that is used to perform a canary release is obtained. You can call the ListDeployGroup operation to query application instance groups and obtain the ID of the application instance group from the GroupId parameter. In this example, the ID of the instance group is 941be68c-4aac-48a1-88fe-c9ad1502****.

  • If you want to release 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 EDAS Container version from the ConfigId parameter. In this example, the EDAS Container version is 57.

Background information

When you perform a canary release for an application by calling an API operation, you must manually trigger the remaining phases after the first phase of the canary release is complete. Before you manually trigger the update on the application instances of the remaining phases, you can verify the update on a small number of application instances. If the verification is successful, you can proceed with the update in the remaining phases. This way, you can update all application instances of the application to the new version.

When you release the application to the remaining ECS instances, we recommend that you perform an automatic phased release. If you choose to perform a manual phased release, you must call the ContinuePipeline operation to manually trigger the update on the application instances of the next phase.

Perform a canary release for an application

  1. Run the code to perform a canary release for an application.

    The following sample code provides an example on how to perform a canary release for an application in an ECS cluster. The sample code does not contain the settings of advanced parameters such as environment variables. For more information about operation-specific parameters, see DeployApplication.

    Note

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

    // The build package number of EDAS Container. This parameter must be specified when you release 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.DeployApplicationRequest;
    import com.aliyuncs.edas.model.v20170801.DeployApplicationResponse;
    
    public class DeployApplication {
    
        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 saved 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 deploy 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. 
            DeployApplicationRequest request = new DeployApplicationRequest();
            // The ID of the application. 
            request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
            // The description of the application. 
            request.setDesc("Canary release");
            // The method to deploy the application. Set the value to url. 
            request.setDeployType("url");
            // The URL of the JAR package or WAR package that you want to use to deploy the application. We recommend that you store the deployment package of the application in OSS and use the OSS storage path. 
            request.setWarUrl("https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.1-SNAPSHOT.jar");
            // The version of the deployment package. The value can be up to 64 characters in length. We recommend that you use a timestamp. 
            request.setPackageVersion("20210417.173831");
            // Specify whether to perform a canary release for the application. A value of true specifies that a canary release is performed for the application. A value of false specifies that a canary release is not performed for the application. To release the application in canary mode, you must specify the ID of the instance group that is used to preform a canary release and the Batch parameter. The Batch parameter specifies the number of phases to release the application to the remaining ECS instances. 
            request.setGray(true);
            // The traffic adjustment policy that is used for the canary release. In this example, the following traffic throttling policy is used: Forward 50% of the traffic to the instance group that is used for the canary release. 
            request.setTrafficControlStrategy("{\"http\":{\"rules\":[{\"conditionType\":\"percent\",\"percent\":50}]}}");
            // The ID of the instance group in which you want to deploy the application. If you want to deploy the application to all instance groups, set this parameter to all. To release the application in canary mode, you must specify the ID of the instance group that is used to preform a canary release. 
            // If you want to deploy the application in a specific instance group, call the ListDeployGroup operation and obtain the group ID from the GroupId parameter. 
            request.setGroupId("941be68c-4aac-48a1-88fe-c9ad1502****");
            // The number of phases in which the application is released to the specified instance group. If you set the value to a number greater than 1, a phased release is performed. 
            request.setBatch(2);
            // The mode of the phased release. A value of 0 specifies that the next phase is automatically triggered. A value of 1 specifies that the next phase must be manually triggered. We recommend that you set this parameter to 0 to perform an automatic phased release. 
            request.setReleaseType((long) 1);
            // The amount of time for which the system waits between two phases. Unit: minutes. The default value is 0. A value of 0 specifies no waiting time between two phases. The maximum value is 5. 
            request.setBatchWaitTime(1);
            // The ID of the application component. This parameter is required if the application runs in Apache Tomcat or in a standard Java application runtime environment. The Apache Tomcat application runtime environment is applicable to Dubbo applications that are deployed by using WAR packages. A standard Java application runtime environment is applicable to Spring Boot or Spring Cloud applications that are deployed by using JAR packages. 
            // 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");
    
            try {
                DeployApplicationResponse response = client.getAcsResponse(request);
                System.out.println("Message=" + response.getMessage() + "\nChangeOrderId=" + response.getChangeOrderId());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

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

    Message=success
    ChangeOrderId=4a3329de-2495-4c51-be33-f69ff264****
  2. Call the GetChangeOrderInfo operation to obtain the values of the PipelineId parameter. This parameter specifies the ID of each phase during a canary release of an application.

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

    {
      "Message": "success",
      "RequestId": "0EB98A7D-50CE-4F5F-855D-70BAF61DFB38",
      "Code": 200,
      "changeOrderInfo": {
        "Status": 8,
        "Desc": "Canary release",
        "PipelineInfoList": {
          "PipelineInfo": [
            {
              ......
              "PipelineId": "09e7d9aa-e79d-49ef-99b9-5405532d****"
              ......
              "PipelineId": "1ea53844-2c60-46f1-b46d-df3b34d0****"
              ......
              "PipelineId": "d99da075-b87d-4fe7-bbb6-ae1c6c26****"
              ......
              // The sample response is incomplete and is only for reference.

    Note

    In this example, after the application is released to the instance group that is used for the canary release, the application is manually released to the remaining ECS instances in two phases. After the application is released to the instance group that is used for the canary release, you must manually trigger the release of the application to the remaining ECS instances. Three phase IDs are returned after you call the GetChangeOrderInfo operation. The second phase ID indicates the phase that you must manually trigger after the application is released to the instance group that is used for the canary release. The third phase ID indicates the phase that you must manually trigger after the first phase of the manual phased release is complete.

  3. Call the ContinuePipeline operation to manually trigger the release of the application to the remaining ECS instances.

    The following result is returned:

    {
      "Message": "success",
      "RequestId": "B8ECF6F7-672D-40E9-91DD-1C33F06D4FD8",
      "Code": 200
    }
  4. Call the ContinuePipeline operation to manually trigger the next phase.

    The following result is returned:

    {
      "Message": "success",
      "RequestId": "7BB4F043-7C28-4A0E-B6A5-D4023EB24388",
      "Code": 200
    }
    Note

    In this example, after the application is released to the instance group that is used for the canary release, the application is manually released to the remaining ECS instances in two phases. During the phased release to the remaining ECS instances, you need to manually trigger only one phase. If the canary release policy requires you to manually trigger multiple phases in the remaining phases, you must specify the correct phase ID (PipelineId) and repeat this step.

Check the result

After you perform the canary release for the application, you can call the GetChangeOrderInfo operation to view the change details and 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": "0EB98A7D-50CE-4F5F-855D-70BAF61DFB38",
  "Code": 200,
  "changeOrderInfo": {
    "Status": 2,
    "Desc": "Canary release",
    "PipelineInfoList": {
      "PipelineInfo": [
                {
                   ......
                   // The sample response is incomplete and is only for reference.

You can view the value of the changeOrderInfo.Status parameter to check whether the canary release of the application is successful. Valid values of the changeOrderInfo.Status parameter:

  • 0: The operation to perform a canary release for the application is ready.

  • 1: The operation to perform a canary release for the application is being performed.

  • 2: The operation to perform a canary release for the application is performed.

  • 3: The operation to perform a canary release for the application failed.

  • 6: The operation to perform a canary release for 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 operation to perform a canary release for the application failed due to a system exception.