When you run multiple applications across environments such as development, staging, and production, microservices namespaces in Enterprise Distributed Application Service (EDAS) help you organize and manage services per environment. Use the EDAS SDK for Java to create, modify, and delete these namespaces programmatically.
Quick start: Create a namespace
The following minimal example creates a namespace in the cn-hangzhou region. For the full parameter reference, see Create or modify a microservices namespace.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.InsertOrUpdateRegionRequest;
import com.aliyuncs.edas.model.v20170801.InsertOrUpdateRegionResponse;
public class CreateNamespaceQuickStart {
public static void main(String[] args) throws Exception {
DefaultAcsClient client = new DefaultAcsClient(
DefaultProfile.getProfile("cn-hangzhou",
System.getenv("ACCESS_KEY_ID"),
System.getenv("ACCESS_KEY_SECRET")));
InsertOrUpdateRegionRequest request = new InsertOrUpdateRegionRequest();
request.setRegionTag("cn-hangzhou:production");
request.setRegionName("Production");
request.setId((long) 0);
InsertOrUpdateRegionResponse response = client.getAcsResponse(request);
System.out.println("Created namespace ID: " + response.getUserDefineRegionEntity().getId());
}
}Expected output:
Created namespace ID: 17926Save the returned Id value. You need it to modify or delete the namespace later.
Prerequisites
Before you begin, make sure that you have:
The EDAS SDK for Java installed. For more information, see Use EDAS SDK for Java to call EDAS API operations
The region ID where the namespace resides (for example,
cn-hangzhou)An AccessKey pair stored in environment variables (
ACCESS_KEY_IDandACCESS_KEY_SECRET). To reduce security risks, use a Resource Access Management (RAM) user instead of your Alibaba Cloud account
Create or modify a microservices namespace
The InsertOrUpdateRegionRequest API handles both creation and modification. Set Id to 0 (or leave it blank) to create a namespace, or set it to the system-generated unique identifier to modify an existing one.
Parameters
Parameter | Description | Example |
| Namespace ID in the format |
|
| Display name of the namespace |
|
| Short description of the namespace |
|
|
|
|
| Enable remote debugging ( |
|
Complete example
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) {
// Read credentials from environment variables.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// Specify the region where the namespace resides.
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
InsertOrUpdateRegionRequest request = new InsertOrUpdateRegionRequest();
// Format: <region-id>:<identifier>
request.setRegionTag("cn-hangzhou:production");
request.setRegionName("Production");
request.setDescription("Production environment for order processing services");
// Set to 0 to create a namespace. Set to a non-zero value to modify an existing one.
request.setId((long) 0);
// Enable or disable remote debugging.
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();
}
}
}Expected output:
RegionName=Production
RegionId=cn-hangzhou:production
Id=17926Save the Id value (for example, 17926). This system-generated unique identifier is required to modify or delete the namespace later.
After a namespace is created, the AccessKey ID and AccessKey secret that are automatically generated for the namespace cannot be modified.
Delete a microservices namespace
Before you delete a namespace, make sure that the following conditions are met:
The namespace contains no microservices application
The namespace contains no cluster
The namespace contains no Elastic Compute Service (ECS) instance
Remove all applications, clusters, and ECS instances from the namespace before you proceed.
Find the namespace ID
If you saved the Id value from the create response, use it directly. Otherwise, call the ListUserDefineRegion operation to retrieve the namespace ID.
Complete example
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) {
// Read credentials from environment variables.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// Specify the region where the namespace resides.
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
DeleteUserDefineRegionRequest request = new DeleteUserDefineRegionRequest();
// Replace with the unique identifier of the namespace 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();
}
}
}Expected output:
RegionName=Production
RegionId=cn-hangzhou:production
Message=successVerify the result
After you create, modify, or delete a namespace, call the ListUserDefineRegion operation to confirm the change. Check the RegionName, RegionId, and Description values in the response.
Example response:
{
"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
}