Install
openclaw skills install @yndu13/alibabacloud-sdk-client-initialization-for-javaInitialize and manage Alibaba Cloud SDK clients in Java. Covers singleton pattern, thread safety, endpoint vs region configuration, VPC endpoints, sync vs async clients, and file upload APIs. Use when the user creates Java SDK clients, configures endpoints, asks about thread safety, singleton patterns, async calls, or VPC endpoint setup.
openclaw skills install @yndu13/alibabacloud-sdk-client-initialization-for-javanew Client() calls waste resources and hurt performance.public class ClientFactory {
private static volatile com.aliyun.ecs20140526.Client instance;
public static com.aliyun.ecs20140526.Client getInstance() throws Exception {
if (instance == null) {
synchronized (ClientFactory.class) {
if (instance == null) {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
instance = new com.aliyun.ecs20140526.Client(config);
}
}
}
return instance;
}
}
Priority: explicit endpoint > region-based resolution via regionId.
// Preferred: explicit endpoint
config.setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
// Alternative: SDK resolves endpoint from region
config.setRegionId("cn-hangzhou");
Use VPC endpoints when running inside Alibaba Cloud VPC (hybrid cloud, leased lines, multi-region):
config.setEndpoint("ecs-vpc.cn-hangzhou.aliyuncs.com");
For file upload APIs (e.g., Visual Intelligence), set both regionId and endpoint to the same region. Otherwise you may see timeouts due to cross-region OSS access:
config.setRegionId("cn-shanghai");
config.setEndpoint("objectdet.cn-shanghai.aliyuncs.com");
// For VPC file upload authorization:
client._openPlatformEndpoint = "openplatform-vpc.cn-shanghai.aliyuncs.com";
| Mode | SDK Artifact | When to Use |
|---|---|---|
| Synchronous | com.aliyun:{productCode}{version} | Simple flows, low concurrency, easier debugging |
| Asynchronous | com.aliyun:alibabacloud-{productCode}{version} | High concurrency/throughput, non-blocking I/O |
Async example:
AsyncClient client = AsyncClient.builder()
.region("cn-hangzhou")
.credentialsProvider(provider)
.overrideConfiguration(ClientOverrideConfiguration.create()
.setEndpointOverride("ecs.cn-chengdu.aliyuncs.com"))
.build();
CompletableFuture<DescribeRegionsResponse> response = client.describeRegions(request);
response.thenAccept(resp -> System.out.println(new Gson().toJson(resp)))
.exceptionally(throwable -> { System.out.println(throwable.getMessage()); return null; });
// Always close async client when done
client.close();