This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call API operations to create, modify, and delete microservices namespaces.
Prerequisites
EDAS SDK for Java is installed. For more information, see Use EDAS SDK for Java to call
The region ID of the microservices namespace that you want to manage is obtained. Sample region ID: cn-hangzhou.
Create or modify a microservices namespace
The following sample code provides an example on how to create or modify a microservices 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. To avoid security risks, we recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. You can log on to the RAM console to create a RAM user.
// In this example, the AccessKey ID and AccessKey secret are stored 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 in which the microservices 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);
// Create an API request and configure the related parameters.
InsertOrUpdateRegionRequest request = new InsertOrUpdateRegionRequest();
// The ID of the microservices namespace, in the format of "Region ID:Identifier of the microservices namespace". Example: cn-hangzhou:doc.
request.setRegionTag("cn-hangzhou:doc");
// The name of the microservices namespace. Example: APIdoc.
request.setRegionName("APIdoc");
// The description of the microservices namespace.
request.setDescription("Call an API operation to create a microservices namespace");
// Create or modify a microservices namespace. If you want to create a microservices namespace, leave this parameter empty or set the value to 0. If you want to modify a microservices 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();
}
}
}The following result is returned after you run the sample code:
RegionName=APIdoc
RegionId=cn-hangzhou:doc
Id=17926In the sample response, Id=17926 specifies that the unique identifier that is generated by the system for the microservices namespace is 17926. If you want to delete the microservices namespace, you must reference the unique identifier.
After you create a microservices namespace, you cannot modify the AccessKey ID and AccessKey secret that are automatically generated for the microservices namespace.
Delete a microservices namespace
Before you delete a microservices namespace, make sure that the following requirements are met:
The microservices namespace contains no cluster.
The microservices namespace contains no microservices application.
The microservices namespace contains no Elastic Compute Service (ECS) instance.
If you recorded the unique identifier of a microservices namespace, such as Id=17926, when you created or modified the microservices namespace, you can use the recorded unique identifier to delete the microservices namespace.
If you did not record the unique identifier of the microservices namespace that you want to delete, you can call the ListUserDefineRegion operation to query the unique identifier of the microservices namespace.
The following sample code provides an example on how to delete a microservices 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. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. You can log on to the RAM console to create a RAM user.
// In this example, the AccessKey ID and AccessKey secret are stored 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 the microservices 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);
// Create an API request and configure the related parameters.
DeleteUserDefineRegionRequest request = new DeleteUserDefineRegionRequest();
// The unique identifier of the microservices 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();
}
}
}The following result is returned after you run the sample code:
RegionName=APIdoc
RegionId=cn-hangzhou:doc
Message=successCheck the result
After you create, modify, or delete a microservices namespace, you can call the ListUserDefineRegion operation to query the values of the RegionName, RegionId, and Description parameters of the microservices 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 microservices 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
}