全部產品
Search
文件中心

Data Online Migration:代理

更新時間:Dec 11, 2025

本文介紹如何使用SDK調用代理相關的方法。

建立代理

說明

使用代理前務必先部署代理程式,SDK無法自動部署代理,請參考文檔部署代理程式 按照文檔提示操作。僅當使用專線或者VPN的情況下才需要建立代理,公網情境一般無須建立代理。

以下範例程式碼用於建立代理。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.CreateAgentInfo;
import com.aliyun.hcs_mgw20240626.models.CreateAgentRequest;

public class Demo {
	/** 
	  強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
	*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填寫主帳號ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填寫通道ID。
		String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
		// 填寫代理名稱。
		String agentName = "exampleagent";
		// 使用網路,公網請填寫public, 專線或者VPN請填寫vpc
		String agentEndpoint = "public";
		// 部署方式,目前僅支援填寫default。
		String deployMethod = "default";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 這裡以北京地區為例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
                        config.setProtocol("http");
			Client client = new Client(config);
			CreateAgentRequest request = new CreateAgentRequest();
			CreateAgentInfo info = new CreateAgentInfo();
			info.setName(agentName);
			info.setTunnelId(tunnelId);
			info.setAgentEndpoint(agentEndpoint);
			info.setDeployMethod(deployMethod);
			request.setImportAgent(info);
			client.createAgent(userId, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

擷取代理程式狀態

以下範例程式碼用於擷取指定代理程式狀態。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.GetAgentStatusResponse;
import com.google.gson.Gson;

public class Demo {
   /** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填寫主帳號ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填寫代理名稱。
      String agentName = "exampleagent";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 這裡以北京地區為例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         Client client = new Client(config);
         GetAgentStatusResponse resp = client.getAgentStatus(userId, agentName);
         // 如果status欄位顯示的是OK,說明代理程式狀態正常,其它任何值都表示代理異常。
         System.out.println(new Gson().toJson(resp.getBody().getImportAgentStatus()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回樣本

{
  "ImportAgentStatus": {
    "Status": "OK",
    "AgentIP": "192.168.0.2",
    "AgentVersion": "1.6.0",
  }
}

擷取代理詳情

以下範例程式碼用於查詢指定代理詳情資訊。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.GetAgentResponse;
import com.google.gson.Gson;

public class Demo {
   /** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填寫主帳號ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填寫代理名稱。
      String agentName = "exampleagent";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 這裡以北京地區為例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         Client client = new Client(config);
         GetAgentResponse resp = client.getAgent(userId, agentName);
         System.out.println(new Gson().toJson(resp.getBody().getImportAgent()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回樣本

{
  "ImportAgent": {
    "Owner": "test_owner",
    "Name": "test_name",
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "DeployMethod": "default",
    "AgentEndpoint": "vpc",
    "ActivationKey": "6af62558-970d-4f44-8663-4e297170fd6a",
    "Tags": "K1:V1,K2:V2",
    "Version": "test_agent_id",
    "TunnelId": "test_tunnel_id"
  }
}

列舉代理

以下範例程式碼用於列舉主帳號下所有代理資訊。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.ListAgentRequest;
import com.aliyun.hcs_mgw20240626.models.ListAgentResponse;
import com.google.gson.Gson;

public class Demo {
   /** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填寫主帳號ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 這裡以北京地區為例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         Client client = new Client(config);
         ListAgentRequest request = new ListAgentRequest();
         // 根據實際填寫marker,count。
	 String marker = "";
	 int count = 1;
	 request.setMarker(marker);
         request.setCount(count);
         ListAgentResponse resp = client.listAgent(userId, request);
         System.out.println(new Gson().toJson(resp.getBody().getImportAgentList()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回樣本

{
  "ImportAgentList": {
    "Truncated": true,
    "NextMarker": "test_next_marker",
    "ImportAgent": [
      {
        "Owner": "test_owner",
        "Name": "test_name",
        "CreateTime": "2024-05-01T12:00:00.000Z",
        "ModifyTime": "2024-05-01T12:00:00.000Z",
        "DeployMethod": "default",
        "AgentEndpoint": "vpc",
        "ActivationKey": "6af62558-970d-4f44-8663-4e297170fd6a",
        "Tags": "K1:V1,K2:V2",
        "Version": "test_agent_id",
        "TunnelId": "test_tunnel_id"
      }
    ]
  }
}

刪除代理

以下範例程式碼用於刪除指定代理。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.DeleteAgentResponse;

public class Demo {
   /** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填寫主帳號ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填寫代理名稱。
      String agentName = "exampleagent";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 這裡以北京地區為例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         Client client = new Client(config);
         DeleteAgentResponse resp = client.deleteAgent(userId, agentName);
         System.out.println(resp.statusCode);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

後續步驟

代理建立後,您可以選擇繼續執行建立資料地址操作,詳情請參見資料地址