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

Prerequisites

Before you roll back an application to a historical version, make sure that the following prerequisites are met:

Roll back an application to a historical version

The following code provides an example on how to roll back an application to a historical version:

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.RollbackApplicationRequest;
import com.aliyuncs.edas.model.v20170801.RollbackApplicationResponse;

public class RollbackApplication {

    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 where the application to be rolled back resides. 
        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. 
        RollbackApplicationRequest request = new RollbackApplicationRequest();
        // The ID of the application to be rolled back to a historical version. 
        request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
        // The historical version to which you want to roll back the application. You can obtain the information about the historical version to which you want to roll back the application by calling the ListHistoryDeployVersion operation. 
        request.setHistoryVersion("20210417.154931");
        // The ID of the instance group for which the application is to be rolled back. To roll back the application for all instance groups, set the parameter to all. 
        // To roll back the application for a specific instance group, call the ListDeployGroup operation to obtain the group ID. 
        request.setGroupId("all");
        // The number of phases for rolling back the application for the specified instances. Valid values: 1 to 5. The value of 1 indicates that the application is to be rolled back for the specified instances at a time. The values 2 to 4 indicate that the application is to be rolled back for the specified instances in phases. 
        request.setBatch(1);
        // The wait duration between rollback phases for the application. Unit: minutes. Default value: 0. Maximum value: 5. The value of 0 indicates that the next phase is immediately triggered after the current phase is complete. For an at-a-time rollback, you do not need to set this parameter. 
        request.setBatchWaitTime(0);

        try {
            RollbackApplicationResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage() + "\nChangeOrderId=" + response.getChangeOrderId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
Message=success
ChangeOrderId=b3496bf0-0b6b-42c3-921c-f5250af****

Verify the result

After you roll back the application to a historical version, 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": "EF09BCD3-3D37-4D76-9CF3-12B71A9EDE7C",
  "Code": 200,
  "changeOrderInfo": {
    "Status": 2,
    "Desc": "Version: 20210417.154931 | Package Name: sc-consumer-D-0.0.1-SNAPSHOT.jar?Expires=161864****&amp;OSSAccessKeyId=TMP.3KgjU****fSsJ&amp;Signature=LrUEz***X9c...",
    "PipelineInfoList": {
      "PipelineInfo": [
        {
          "PipelineStatus": 2,
          "PipelineName": "Batch 1 Change",
           ...... 
           // 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 rolled back to a historical version. 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 rollback
  • 9: wait to trigger the next phase during an automatic phased rollback
  • 10: failed due to a system exception