All Products
Search
Document Center

Enterprise Distributed Application Service:Call API operations to manage microservice namespaces

Last Updated:Jun 15, 2023

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call API operations to create, modify, and delete microservice namespaces.

Prerequisites

  • The ID of the region where the microservice namespace that you want to manage resides. Sample region ID: cn-hangzhou.

Create or modify a microservice namespace

The following sample code provides an example on how to create or modify 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.InsertOrUpdateRegionRequest;
import com.aliyuncs.edas.model.v20170801.InsertOrUpdateRegionResponse;

public class InsertOrUpdateRegion {

    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 sample code, the AccessKey ID and AccessKey secret are included in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file. 
        // To prevent key leaks, we recommend that you do not include 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 the microservice namespace you want to create or modify resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Initiate an API request and configure the related parameters. 
        InsertOrUpdateRegionRequest request = new InsertOrUpdateRegionRequest();
        // The ID of the microservice namespace, in the format of "Region ID:Identifier of the 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("Call an API operation to create a microservice namespace");
        // Create or modify a microservice namespace. If you want to create a microservice namespace, leave this parameter empty or set the value to 0. If you want to modify a microservice namespace, 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, Id=17926 specifies that the unique identifier that is generated by the system for the microservice namespace is 17926. If you want to delete the microservice namespace, you must reference the unique identifier.

Delete a microservice namespace

Before you delete a microservice namespace, make sure that the microservice namespace meets the following prerequisites:

  • No cluster is deployed in the microservice namespace.

  • No Elastic Compute Service (ECS) instance is deployed in the microservice namespace.

If you recorded the unique identifier of a microservice namespace, such as Id=17926, when you created or modified the microservice namespace, you can use the recorded unique identifier to delete the microservice namespace.

If you did not record the unique identifier of the microservice namespace that you want to delete, you can call the ListUserDefineRegion operation to query the unique identifier of the microservice namespace.

The following sample 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 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 RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        // In this sample code, the AccessKey ID and AccessKey secret are included in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file. 
        // To prevent key leaks, we recommend that you do not include 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 the microservice namespace that you want to delete resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Initiate an API request and configure the related parameters. 
        DeleteUserDefineRegionRequest request = new DeleteUserDefineRegionRequest();
        // The unique identifier of the microservice namespace that you want to delete. 
        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

Check the result

After you create, modify, or delete a microservice namespace, you can call the ListUserDefineRegion operation to query the values of the RegionName, RegionId, and Description parameters of the microservice namespace to check whether your operation is successful.

Sample returned response after you call the ListUserDefineRegion operation:

{
  "Message": "success",
  "RequestId": "849E908D-E2DE-4798-937B-7210E00FFDD8",
  "UserDefineRegionList": {
    "UserDefineRegionEntity": [
      {
        "Description": "The microservice namespace that is created by using calling an API operation",
        "RegionName": "APIdoc2",
        "UserId": "native_****",
        "DebugEnable": true,
        "Id": 17925,
        "RegionId": "cn-hangzhou:doc2",
        "BelongRegion": "cn-hangzhou"
      }
    ]
  },
  "Code": 200
}