If you have used Alibaba Cloud SDK V1.0 to integrate cloud services before, you can read this topic to learn about the differences between Alibaba Cloud SDK V1.0 and Alibaba Cloud SDK V2.0. If you use Alibaba Cloud SDK for the first time, we recommend that you directly use Alibaba Cloud SDK V2.0.
Alibaba Cloud SDK V1.0
The following figure shows the simple architecture of Alibaba Cloud SDK V1.0.
The architecture of Alibaba Cloud SDK V1.0 consists of the following three layers:
The service SDK layer. This layer processes operation-specific parameters.
The core SDK layer. This layer transitions the operation-specific parameters to HTTP request parameters.
The HTTP Client layer. This layer initiates requests.
To use an Alibaba Cloud SDK to integrate cloud services, you must import the core dependent SDK and service SDK. We recommend that you do not use Alibaba Cloud SDK V1.0 due to issues such as the inconsistent user experience and the inability to isolate authentication information.
Sample code
When you call API operations of multiple cloud services at the same time, the common client
objects cannot isolate identity and authentication information by cloud service. This may cause thread safety issues.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;
public class DescribeInstanceStatus {
public static void main(String[] args) {
// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
/** Use the Security Token Service (STS) token.
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID.
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // The AccessKey ID of the RAM user.
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // The AccessKey secret of the RAM user.
System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")); // The STS token.
**/
IAcsClient client = new DefaultAcsClient(profile);
DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
request.setRegionId("cn-hangzhou");
try {
DescribeInstanceStatusResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
Alibaba Cloud SDK V2.0
Alibaba Cloud SDK V1.0 is an earlier version that is used by a large number of existing users for development. Alibaba Cloud SDK V2.0 is developed based on Alibaba Cloud SDK V1.0. To resolve a series of issues that developers encounter when they use Alibaba Cloud SDK V1.0, Alibaba Cloud SDK V2.0 offers many new features to provide readable content, simplify the use of SDKs, and improve the robustness of SDKs.
Alibaba Cloud SDK V2.0 is generated based on Darabonba, a domain-specific language (DSL) developed by Alibaba Cloud. The flexibility of DSL allows you to express more information. You can also parse the DSL code and use the generated abstract syntax trees (ASTs) to eliminate the differences in API styles when you use different Alibaba Cloud services. This way, API operations are easily called and SDKs are easily generated. Compared with Alibaba Cloud SDK V1.0, Alibaba Cloud SDK V2.0 has the following new features:
Consistent user experience: In Alibaba Cloud SDK V1.0, the APIs of different Alibaba Cloud services vary based on the style, such as remote procedure call (RPC) or resource-oriented architecture (ROA). Therefore, the API operations of the services are called by using different methods. In Alibaba Cloud SDK V2.0, the SDKs of all Alibaba Cloud services are used in the same way. This ensures consistent user experience.
Reduced development costs: DSL-based Alibaba Cloud SDK V2.0 contains complete business logic. This resolves the issue that Alibaba Cloud SDK V1.0 is tightly coupled with the core library. This way, the development costs generated due to SDK updates are reduced.
Isolated identity and authentication information: The SDKs of each Alibaba Cloud service provide a client object. You can instantiate the client object to call all API operations of an Alibaba Cloud service. This implementation of Alibaba Cloud SDK V2.0 is better than that of Alibaba Cloud SDK V1.0. In Alibaba Cloud SDK V1.0, an SDK client processes multiple threads, and permission management is less efficient. In contrast, Alibaba Cloud SDK V2.0 ensures the security of multiple threads, isolates the identity and authentication information about each Alibaba Cloud service, and does not require multiple Alibaba Cloud services to share one piece of user profile information. You can configure different parameters to call the API operations of different Alibaba Cloud services. The parameters that you can configure include the region, timeout period, HTTP proxy, and retry mechanism.
Complex scenarios: Alibaba Cloud SDK V2.0 supports API calls in more complex scenarios. For example, to use the FaceBody feature of Visual Intelligence API, you must upload an image to Object Storage Service (OSS) and then use AI to analyze the image based on the generated image URL. In this case, Alibaba Cloud SDK V2.0 allows you to combine the authentication, image upload, and image analysis operations. This simplifies API calls.
SDK samples for specific scenarios: Alibaba Cloud SDK V2.0 provides sample code for all API requests. It also provides sample code for scenarios in which multiple API requests are sent at a time. The provided information helps you use SDKs with ease and better understand business scenarios in which you can use Alibaba Cloud services.
If you are a new user, we recommend that you directly use Alibaba Cloud SDK V2.0. If you are an existing user, we recommend that you update your SDKs to the latest version at the earliest opportunity.
Sample code
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Specify your AccessKey ID.
.setAccessKeyId(accessKeyId)
// Required. Specify your AccessKey secret.
.setAccessKeySecret(accessKeySecret);
// Specify an endpoint. For more information, visit https://api.aliyun.com/product/Ecs.
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
return new com.aliyun.ecs20140526.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
// If the project code is leaked, the AccessKey pair may be leaked and resources in your account become insecure. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. The sample code is for reference only. We recommend that you use STS, which provides higher security. For more information about authentication methods, see the "Configure credentials" topic in the "Alibaba Cloud SDK V2.0 for Java" documentation.
com.aliyun.ecs20140526.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
com.aliyun.ecs20140526.models.DescribeInstanceStatusRequest describeInstanceStatusRequest = new com.aliyun.ecs20140526.models.DescribeInstanceStatusRequest()
.setRegionId("cn-hangzhou");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// After you copy and run the sample code, obtain the return value of the operation.
client.describeInstanceStatusWithOptions(describeInstanceStatusRequest, runtime);
} catch (TeaException error) {
// Print the error based on your business requirements.
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// Print the error based on your business requirements.
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Comparison
Item | SDK V1.0 | SDK V2.0 |
Generation method | SDKs for different programming languages are generated based on different templates. The features encapsulated in the SDKs vary with the programming language. Therefore, you have different user experience with SDKs for different programming languages. | SDKs are generated based on Darabonba. Darabonba serves as an intermediary to generate SDKs for different programming languages and automatically verify syntaxes. This provides consistent user experience when you use SDKs for different programming languages. |
Supported languages | For most Alibaba Cloud services, only SDKs for up to three mainstream programming languages are provided. | SDKs for six mainstream programming languages are provided. |
Extended support for more languages | It is difficult to generate SDKs for more programming languages because SDKs are generated based on templates. | SDKs for more programming languages can be easily generated by developing ASTs in Darabonba. |
SDK samples | The sample code for different programming languages differs from each other. The correctness of the sample code cannot be guaranteed. | The sample code for different programming languages is written in an intermediate language based on the same logic. All the sample code is verified to ensure correctness. |
SDK samples for specific scenarios | SDK samples for specific scenarios are difficult to write. Only a few services provide SDK samples for specific scenarios in a limited number of programming languages. | DSL makes it easy to generate SDK samples for specific scenarios in different programming languages. This helps you efficiently learn how to use the SDKs. For more information, visit Sample Code Center. |