Loading...
Loading...
Compare original and translation side by side
new Client()new Client()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;
}
}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;
}
}endpointregionId// Preferred: explicit endpoint
config.setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
// Alternative: SDK resolves endpoint from region
config.setRegionId("cn-hangzhou");endpointregionId// Preferred: explicit endpoint
config.setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
// Alternative: SDK resolves endpoint from region
config.setRegionId("cn-hangzhou");config.setEndpoint("ecs-vpc.cn-hangzhou.aliyuncs.com");config.setEndpoint("ecs-vpc.cn-hangzhou.aliyuncs.com");regionIdendpointconfig.setRegionId("cn-shanghai");
config.setEndpoint("objectdet.cn-shanghai.aliyuncs.com");
// For VPC file upload authorization:
client._openPlatformEndpoint = "openplatform-vpc.cn-shanghai.aliyuncs.com";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 | | Simple flows, low concurrency, easier debugging |
| Asynchronous | | High concurrency/throughput, non-blocking I/O |
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();| 模式 | SDK制品 | 使用场景 |
|---|---|---|
| 同步 | | 流程简单、低并发场景,调试更便捷 |
| 异步 | | 高并发/高吞吐量场景,非阻塞I/O |
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();