Endpoint 配置
Endpoint又叫服务接入点,是请求接口服务的网络域名,如产品 ECS 在杭州地域的Endpoint:ecs.cn-hangzhou.aliyuncs.com。每个产品都有其独立的 Endpoint,并且 Endpoint 与服务区域 RegionId 有关,不同地域可能是不同的 Endpoint。本节主要介绍升级版 SDK 对于 Endpoint 的配置。
Endpoint类型
服务接入点主要有两类:公网服务地址、VPC服务地址,更多信息请参见服务接入点。
Endpoint 设置
升级版SDK在Endpoint寻址上简化了许多,只提供了最容易理解的两种方式,下面按优先级排列:
用户自定义:用户可以通过在初始化时指定云产品 Client 实例的请求地址,产品的 Endpoint 可以通过OpenAPI 开发者门户的产品主页中查找,具体参考文末。
public static void main(String[] args) {
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"));
// 直接设置服务域名 endpoint
config.setEndpoint("<endpoint>");
com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
}
Endpoint 拼接规则:
若请求的产品 SDK 具有 Endpoint 数据文件时,当前通过 RegionId 寻址的逻辑才会生效,否则会在实例化 Client 对象时抛出异常 TeaException ,其 message 为
config.endpoint can not be empty
。若请求的产品 SDK 缺少 Endpoint 数据文件时,则只能使用用户自定义的方式指定 Endpoint。 Endpoint 数据文件示例(Ecs Endpoint Data),它是一个以RegionId作为
key
,Endpoint作为value
的Map
。若指定未在endpoint数据文件枚举的 RegionId则会按照拼接规则:${产品code}.${RegionId}.aliyuncs.com
生成Endpoint。
public static void main(String[] args) {
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"));
// 通过 regionId 映射到 endpoint
config.setRegionId("<regionId>");
com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
}
内网 Endpoint 设置
使用阿里云SDK方式调用OpenAPI时,若业务场景有以下需求,建议设置阿里云VPC内Endpoint:
业务系统同时存在于本地机房和云上机房,基于阿里云VPC搭建不同的业务模块,构建完全隔离的云上环境,云上云下通过公网进行业务交互。
基于阿里云VPC搭建云上数据中心,通过专线与云下内部数据中心打通,进行用户核心数据安全保障,完美应对业务激增及数据快速同步,实现混合云方案。
基于阿里云VPC搭建多个应用,各应用都需要对外提供服务,且其波峰时间点不一致,希望多IP共享带宽,尽量减小波峰波谷效应从而降低成本。
云上业务完全基于VPC构建,用户遍布各个区域,为提升用户访问速度,业务系统同样位于不同节点,需实现各节点之间的网络高速互联。
下面以视觉智能开放平台的检测人体计数接口为例,讲解上传文件时设置内网Endpoint:
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>");
// 设置通过 OSS 内网地址上传文件,如需完全内网环境请打开注释
// config.setEndpointType("internal");
com.aliyun.facebody20191230.Client client = new com.aliyun.facebody20191230.Client(config);
// 根据 regionId 设置鉴权服务的 VPC请求地址,例如杭州 openplatform-vpc.cn-hangzhou.aliyuncs.com
client._openPlatformEndpoint="openplatform-vpc.<regionId>.aliyuncs.com";
RuntimeOptions runtimeOptions = new RuntimeOptions();
// 读取文件并实例化文件流
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);
// response 包含服务端响应的 body 和 headers
System.out.println(new Gson().toJson(resp.body));
System.out.println(new Gson().toJson(resp.headers));
}
}
附:Endpoint 查找方式
产品的 Endpoint 可以通过在平台 开发者门户 上寻找:
打开云产品主页:在首页选择相应云产品,例如 ECS:
2. 找到服务区域列表:
3. 选择相应 RegionId 所对应的 Endpoint,进行复制粘贴即可。
还可以这样查找:
在 API 调试界面,找到对应接口,选择服务地址 Region,平台会自动生成 SDK 代码,代码中可以查看需要的 Endpoint。
- 本页导读 (1)