This topic describes how to switch the network type of a Redis instance from a classic network to a virtual private cloud (VPC) network by using the console or by calling the relevant API operation. When you switch the network type, you can specify a retention period for the classic network endpoint. After the retention period expires, the classic network endpoint is automatically deleted.

Prerequisites

Before you can switch the network type, the following conditions must be met:
  • An Alibaba Cloud account is created. If you do not have an Alibaba Cloud account, create one. For more information,Create an Alibaba Cloud account.
  • The network type of the Redis instance is the classic network.
  • VPCs and VSwitches are available in the zone to which the Redis instance belongs. For more information, see Create a VPC.

Switch the network type in the console

  1. Log on to the ApsaraDB for Redis console.
  2. Select the region where the instance is located.
  3. Click the ID of the instance.
  4. On the Instance Information page, click Switch to VPC Network.
  5. In the dialog box that appears, perform the following steps:
    1. Select the VPC network and VSwitch that you want to use.
    2. You can choose to retain a classic network endpoint and specify a retention period.
      Note After you select to retain the classic network endpoint, the classic network endpoint and the VPC endpoint remain two different endpoints. ECS instances in the classic network can still access the database as normal and services are not affected. When the classic network endpoint expires, it is automatically deleted and you cannot access the database through the classic network endpoint.
    3. Click OK.
  6. You can click Refresh on the Instance Information page to view the endpoints of the classic network and VPC network.

Modify the retention period of the classic network endpoint by using the console

After you set the retention period for the connection address of a classic network, you can change the expiration date in the console to extend the retention period before the address expires.

During the period in which your instance can be connected over the classic network or VPCs, you can specify the expiration date for the endpoint of the classic network. The changes immediately take effect. For example, if the connection address of the classic network is about to expire on August 18, 2017 and you change the expiration date to 14 days later on August 15, 2017, the connection address of the classic network is released on August 29, 2017.

  1. Log on to the ApsaraDB for Redis console.
  2. Select the region where the instance is located.
  3. Click the ID of the instance.
  4. In the Retained Classic Network IP Address section, click Change Expiration Date.
  5. In the dialog box that appears, select a new expiration time and click OK.

Switch the network type by calling the relevant API operation

  1. Download relevant SDKs. ( The SDK of ApsaraDB for Memcache is the same as that of ApsaraDB for Redis.)
  2. Call the SwitchNetwork API operation to switch the network type.
    Request parameters
    Parameter Type Required Description
    Action String Yes The operation that you want to perform. Set the value to

    SwitchNetwork

    InstanceId String Yes The ID of the instance.
    TargetNetworkType String Yes The network type of the instance. Valid values:
    • VPC: a VPC network
    • Classic: a classic network
    VPCId String No The ID of the VPC network.
    VSwitchId String No

    The ID of the VSwitch.

    This parameter is required if the VPC ID is specified.

    RetainClassic String No
    Specifies whether to retain the classic network endpoint. Default: False:
    • True: The classic network endpoint is retained
    • False: The classic network endpoint is not retained
    ClassicExpiredDays String No

    The retention period of the classic network endpoint in days. Unit: days. Minimum value: 1. Maximum value: 120. Default value: 7.

    This parameter is required if RetainClassic is set to True.

    Response parameters
    Parameter Type Description
    RequestId String The ID of the request.
    TaskId String The ID of the task.
    Sample requests
    import com.aliyuncs.DefaultAcsClient;
     import com.aliyuncs.IAcsClient;
     import com.aliyuncs.exceptions.ClientException;
     import com.aliyuncs.exceptions.ServerException;
     import com.aliyuncs.profile.DefaultProfile;
     import com.aliyuncs.profile.IClientProfile;
     import com.aliyuncs.r_kvstore.model.v20150101.SwitchNetworkRequest;
     import com.aliyuncs.r_kvstore.model.v20150101.SwitchNetworkResponse;
     import org.junit.Test;
     /**
      * Created by wb259286 on 2017/6/9.
      */
     public class SwitchNetworkTest {
         @Test
         public  void switchNetwork_success() {
             SwitchNetworkRequest request=new SwitchNetworkRequest();
             request.setInstanceId("<<Your instance ID>");
             request.setTargetNetworkType("VPC");
             request.setVpcId("<VpcId: This parameter is required when TargetNetworkType is set to VPC.>");
             request.setVSwitchId("<VSwitchId: This parameter is required when the TargetNetworkType is VPC>");
             request.setRetainClassic("<Whether to retain the classic network endpoint>");
             request.setClassicExpiredDays("The retention period of the classic network endpoint");
             IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<Your AccessKey information>",
                 "<<Your AccessKey Secret>>");
             IAcsClient client = new DefaultAcsClient(profile);
             try {
                 SwitchNetworkResponse response
                     = client.getAcsResponse(request);
                 System.out.println(response.getRequestId());
         }catch (ServerException e) {
                 e.printStackTrace();
         }
             catch (ClientException e) {
                 e.printStackTrace();
             }
         }
     }
  3. Call the DescribeDBInstanceNetInfo API operation to view the classic network endpoint and the VPC endpoint.
    Request parameters
    Parameter Type Required Description
    Action String Yes The operation that you want to perform. Set the value to

    DescribeDBInstanceNetInfo

    InstanceId String Yes The ID of the instance.
    Response parameters
    Parameter Type Description
    NetInfoItems List The connection information of the instance.
    InstanceNetworkType String The network connection type of the instance. Valid values:
    • VPC: the VPC network type.

    • Classic: the classic network type.

    InstanceNetInfo
    Parameter Type Description
    ConnectionString String The DNS connection string.
    IPAddress String The IP address.
    IPType String

    The following IP address types of the classic network are supported: Inner and Public.

    The IP address type of the VPC network: Private and Public.

    Port String The port information.
    VPCId String The ID of the VPC to which the instances belong.
    VSwitchId String The ID of the VSwitch.
    ExpiredTime String The expiration date.
    Example code
    import com.aliyuncs.DefaultAcsClient;
     import com.aliyuncs.IAcsClient;
     import com.aliyuncs.exceptions.ClientException;
     import com.aliyuncs.exceptions.ServerException;
     import com.aliyuncs.profile.DefaultProfile;
     import com.aliyuncs.profile.IClientProfile;
     import com.aliyuncs.r_kvstore.model.v20150101.DescribeDBInstanceNetInfoRequest;
     import com.aliyuncs.r_kvstore.model.v20150101.DescribeDBInstanceNetInfoResponse;
     import org.junit.Test;
     /**
      *
      */
     public class DescribeDBInstanceNetInfoTest {
     @Test
         public  void describeDBInstanceNetInfo_success() {
             DescribeDBInstanceNetInfoRequest request=new     DescribeDBInstanceNetInfoRequest();
             request.setInstanceId("<Your instance ID>");
             IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<Your AccessKey information>",
                 "<Your AccessKey Secret>");
             IAcsClient client = new DefaultAcsClient(profile);
             try {
                 DescribeDBInstanceNetInfoResponse response
                     = client.getAcsResponse(request);
                 System.out.println(response.getRequestId());
         }catch (ServerException e) {
                 e.printStackTrace();
             }
             catch (ClientException e) {
             e.printStackTrace();
             }
         }
     }

Modify the retention period of the classic network endpoint by using the relevant API operation

  1. Download relevant SDKs. ( The SDK of ApsaraDB for Memcache is the same as that of ApsaraDB for Redis.)
  2. Call the ModifyInstanceNetExpireTime API operation to switch the network type.
    Request parameters
    Parameter Type Required Description
    Action String Yes The operation that you want to perform. Set the value to

    ModifyInstanceNetExpireTime

    .
    InstanceId String Yes The ID of the instance.
    ConnectionString String Yes The endpoint of the classic network.
    ClassicExpiredDays Integer Yes

    Specifies the number of days the classic network endpoint is retained.

    Valid values: 14, 30, 60, and 120.

    Response parameters
    Parameter Type Description
    RequestId String The ID of the instance.
    Example code
    public static void main(String[] args) {
         ModifyInstanceNetExpireTimeRequest request = new ModifyInstanceNetExpireTimeRequest();
         request.setClassicExpiredDays(3);
         request.setConnectionString("<The connection string>");
         request.setInstanceId("<The ID of the instance>");
         IClientProfile profile
                 = DefaultProfile.getProfile("cn-hangzhou", "<Your AccessKey information>",
                 "<Your AccessKey Secret>");
         IAcsClient client = new DefaultAcsClient(profile);
         try {
             ModifyInstanceNetExpireTimeResponse response
                     = client.getAcsResponse(request);
             for (NetInfoItem item:response.getNetInfoItems()) {
                 System.out.println(item.getConnectionString());
                 System.out.println(item.getPort());
                 System.out.println(item.getDBInstanceNetType());
                 System.out.println(item.getIPAddress());
                 System.out.println(item.getExpiredTime());
             }
         }catch (ServerException e) {
             e.printStackTrace();
         }
         catch (ClientException e) {
             e.printStackTrace();
         }
     }