全部產品
Search
文件中心

Alibaba Cloud SDK:Endpoint 配置

更新時間:Jul 01, 2024

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作為valueMap。若指定未在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 可以通過在平台 開發人員門戶 上尋找:

  1. 開啟雲產品首頁:在首頁選擇相應雲產品,例如 ECS:

image.png

2. 找到服務地區列表:

image.png

3. 選擇相應 RegionId 所對應的 Endpoint,進行複製粘貼即可。

還可以這樣尋找

在 API 調試介面,找到對應介面,選擇服務地址 Region,平台會自動產生 SDK 代碼,代碼中可以查看需要的 Endpoint。

image.png