An endpoint is a domain name of an Alibaba Cloud service. For example, the endpoint of Elastic Compute Service (ECS) in the China (Hangzhou) region is ecs.cn-hangzhou.aliyuncs.com. Each Alibaba Cloud service has its unique endpoints. The endpoints of a service may vary based on regions. This topic describes how to configure an endpoint in Alibaba Cloud SDK V2.0.
Endpoint type
Endpoints are divided into public endpoints and Virtual Private Cloud (VPC) endpoints. For more information, see Endpoints.
Configure an endpoint
Alibaba Cloud SDK V2.0 provides two methods for configuring an endpoint. The following section describes the methods based on their priority in descending order.
We recommend that you configure an endpoint by specifying a custom endpoint.
Specify a custom endpoint. You can specify an endpoint when you initialize a client. You can query an endpoint in OpenAPI Explorer. For more information, see the "Appendix: Query an endpoint" section of this topic.
import com.aliyun.ecs20140526.Client; import com.aliyun.teaopenapi.models.Config; public class Sample { public static void main(String[] args) throws Exception { com.aliyun.teaopenapi.models.Config config = new Config(); config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")); config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // Configure an endpoint. config.setEndpoint("<endpoint>"); Client client = new Client(config); } }
Specify a custom region ID.
If the SDK for an Alibaba Cloud service contains the relationship object of region IDs and endpoints and the specified region ID is included in the relationship object, you can obtain an endpoint from the relationship object.
If the SDK for an Alibaba Cloud service does not contain the relationship object of region IDs and endpoints or the specified region ID is not included in the relationship object, an endpoint is automatically concatenated based on concatenation rules.
import com.aliyun.ecs20140526.Client; import com.aliyun.teaopenapi.models.Config; public class Sample { public static void main(String[] args) throws Exception { Config config = new Config(); config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")); config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // Specify an endpoint based on the region ID. config.setRegionId("<regionId>"); Client client = new Client(config); } }
Scenarios in which VPC endpoints are used
We recommend that you configure VPC endpoints if you use Alibaba Cloud SDKs to call API operations and your business has the following requirements:
Business systems are deployed both in an on-premises data center and a cloud data center. Different business modules are built based on VPC networks to build an isolated cloud environment. The data centers communicate with each other over the Internet.
A cloud data center is deployed based on VPC networks. It communicates with the on-premises data center over an Express Connect circuit to implement hybrid cloud connections. This ensures the security of core user data and helps respond to workload fluctuations and fast data synchronization.
Multiple applications deployed based on VPC networks need to provide external services, and the workload of the applications fluctuates at different points in time. In this case, multiple IP addresses need to share bandwidth and traffic fluctuations need to be reduced, thus reducing costs.
Cloud services are constructed based on VPC networks. To reduce network latency for users in different regions, business systems are deployed on different nodes. High bandwidth connections between these nodes are required to optimize user experience.
Obtain an endpoint to request authentication information
Some API operations can be called to configure an endpoint of an open platform. If you do not configure an endpoint, the default public endpoint is used. You can use the endpoint to call the file upload authentication service and then obtain authentication information and the information about the default Object Storage Service (OSS) bucket.
The following example shows how to configure a VPC endpoint by calling the people count detection operation of Visual Intelligence API (VIAPI):
import com.aliyun.facebody20191230.models.DetectBodyCountAdvanceRequest;
import com.aliyun.facebody20191230.models.DetectBodyCountResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Sample {
public static void main(String[] args) throws Exception {
Config config = new com.aliyun.teaopenapi.models.Config();
config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.setRegionId("<regionId>");
// Configure the OSS endpoint that you want to use to upload the file. To upload the file by using an internal network, uncomment the following line:
// config.setEndpointType("internal");
com.aliyun.facebody20191230.Client client = new com.aliyun.facebody20191230.Client(config);
// Configure a VPC endpoint of the authentication service based on the region ID such as openplatform-vpc.cn-hangzhou.aliyuncs.com.
client._openPlatformEndpoint="openplatform-vpc.<regionId>.aliyuncs.com";
RuntimeOptions runtimeOptions = new RuntimeOptions();
// Read the file and instantiate the file stream.
File f = new File("<your-file-path>");
InputStream in =new FileInputStream(f);
DetectBodyCountAdvanceRequest request = new DetectBodyCountAdvanceRequest();
request.setImageURLObject(in);
DetectBodyCountResponse resp = client.detectBodyCountAdvance(request, runtimeOptions);
// The response, which contains the body and headers that are returned by the server.
System.out.println(new Gson().toJson(resp.body));
System.out.println(new Gson().toJson(resp.headers));
}
}
Appendix: Query an endpoint
You can query an endpoint in OpenAPI Explorer.
Select an Alibaba Cloud service on the homepage of OpenAPI Explorer. In this example, ECS is selected.
2. On the homepage of ECS, click the Regions tab.
3. Find the region that you want to specify and copy the endpoint of ECS in the region.
You can also query the endpoints of ECS on the Debugging page. On this page, move the pointer over Regions in the left-side navigation pane to view the endpoints of ECS.