This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to create, edit, and delete a custom microservice namespace by calling API operations.

Prerequisites

  • The ID of the region in which the microservice namespace to be managed resides. Example: cn-hangzhou.

Create or edit a microservice namespace

The following code provides an example on how to create or edit a custom microservice namespace:

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

public class InsertOrUpdateRegion {

    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 account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the microservice namespace to be created or edited 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. 
        InsertOrUpdateRegionRequest request = new InsertOrUpdateRegionRequest();
        // The ID of the microservice namespace, in the format of "Region ID:Identifier of microservice namespace". Example: cn-hangzhou:doc. 
        request.setRegionTag("cn-hangzhou:doc");
        // The name of the microservice namespace. Example: APIdoc. 
        request.setRegionName("APIdoc");
        // The description of the microservice namespace. 
        request.setDescription("Create a microservice namespace by calling an API operation");
        // Create or edit a microservice namespace. If you want to create a microservice namespace, leave this parameter empty or set this parameter to 0. If you want to edit a microservice, set this parameter to a value other than 0. 
        request.setId((long) 0);
        // Specify whether to enable remote debugging. If you want to enable remote debugging, set this parameter to true. Otherwise, set this parameter to false. 
        request.setDebugEnable(true);

        try {
            InsertOrUpdateRegionResponse response = client.getAcsResponse(request);
            System.out.println("RegionName=" + response.getUserDefineRegionEntity().getRegionName() + "\nRegionId=" + response.getUserDefineRegionEntity().getRegionId()+ "\nId=" + response.getUserDefineRegionEntity().getId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
RegionName=APIdoc
RegionId=cn-hangzhou:doc
Id=17926
Note In the sample response, 17926 is the unique ID of the microservice namespace. If you want to delete the microservice namespace, you must reference the unique ID.

Delete a microservice namespace.

If have recorded the unique ID (such as 17926) of a microservice namespace when you create or edit the microservice namespace. You can reference the unique ID to delete it.

If you did not record the unique ID of the microservice namespace, you must call the ListUserDefineRegion operation to obtain the unique ID of the microservice namespace.

The following code provides an example on how to delete a microservice namespace.

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

public class DeleteUserDefineRegion {

    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 account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the microservice namespace to be deleted 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. 
        DeleteUserDefineRegionRequest request = new DeleteUserDefineRegionRequest();
        // The unique ID of the microservice namespace to be deleted. 
        request.setId((long) 17926);


        try {
            DeleteUserDefineRegionResponse response = client.getAcsResponse(request);
            System.out.println("RegionName=" + response.getRegionDefine().getRegionName() + "\nRegionId=" + response.getRegionDefine().getRegionId()+ "\nMessage=" + response.getMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
RegionName=APIdoc
RegionId=cn-hangzhou:doc
Message=success

Verify the result

After you create, edit, or delete a microservice namespace, you can call the ListUserDefineRegion operation to query microservice namespace. Then, you can check whether your call is successful by viewing the values of the RegionName, RegionId, and Description parameters in the returned result.

Sample response that is returned after you call the ListUserDefineRegion operation:
{
  "Message": "success",
  "RequestId": "849E908D-E2DE-4798-937B-7210E00FFDD8",
  "UserDefineRegionList": {
    "UserDefineRegionEntity": [
      {
        "Description": "Microservice namespace that is created by calling an API operation",
        "RegionName": "APIdoc2",
        "UserId": "native_****",
        "DebugEnable": true,
        "Id": 17925,
        "RegionId": "cn-hangzhou:doc2",
        "BelongRegion": "cn-hangzhou"
      }
    ]
  },
  "Code": 200
}