通道
更新时间:
本文介绍如何使用SDK调用通道相关的方法。
创建通道
说明 
仅当使用专线或者VPN的情况下才需要创建通道,公网场景一般无须创建通道。
以下示例代码用于创建通道。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.CreateTunnelInfo;
import com.aliyun.hcs_mgw20240626.models.CreateTunnelRequest;
import com.aliyun.hcs_mgw20240626.models.TunnelQos;
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);
			Client client = new Client(config);
			CreateTunnelRequest request = new CreateTunnelRequest();
			CreateTunnelInfo info = new CreateTunnelInfo();
			TunnelQos qos = new TunnelQos();
			// MaxBandwidth, MaxQps 默认为0, 表示没有限制, MaxBandWidth的单位是bit,请按照实际需求填写。
			qos.setMaxBandwidth(1073741824L);
			qos.setMaxQps(1000);
			info.setTunnelQos(qos);
			request.setImportTunnel(info);
			CreateTunnelResponse resp = client.createTunnel(userId, request);
			System.out.println("通道ID:" + resp.headers.get("x-oss-import-tunnel-id"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}获取通道详情
以下示例代码用于获取指定通道详情。
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";
      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);
         Client client = new Client(config);
         GetTunnelResponse resp = client.getTunnel(userId, tunnelId);
         System.out.println(new Gson().toJson(resp.getBody().getImportTunnel()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}正常返回示例
{
  "ImportTunnel": {
    "Owner": "test_owner",
    "TunnelId": "test_tunnel_id",
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "Tags": "K1:V1,K2:V2",
    "TunnelQos": {
      "MaxQps": 100,
      "MaxBandwidth": 1073741824
    }
  }
}列举通道
以下示例代码用于列举当前账号下的所有通道信息。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.GetTunnelResponse;
import com.aliyun.hcs_mgw20240626.models.ListTunnelRequest;
import com.aliyun.hcs_mgw20240626.models.ListTunnelResponse;
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);
			Client client = new Client(config);
			ListTunnelRequest request = new ListTunnelRequest();
			// 根据实际填写marker,count。
			String marker = "";
			int count = 1;
			request.setMarker(marker);
			request.setCount(count);
			ListTunnelResponse resp = client.listTunnel(userId, request);
			System.out.println(new Gson().toJson(resp.getBody().getImportTunnelList()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
正常返回示例
{
  "ImportTunnelList": {
    "Truncated": true,
    "NextMarker": "test_marker",
    "ImportTunnel": [
      {
        "Owner": "test_owner",
        "TunnelId": "test_tunnel_id",
        "CreateTime": "2024-05-01T12:00:00.000Z",
        "ModifyTime": "2024-05-01T12:00:00.000Z",
        "Tags": "K1:V1,K2:V2",
        "TunnelQos": {
          "MaxQps": 100,
          "MaxBandwidth": 1073741824
        }
      }
    ]
  }
}更新通道
以下示例代码用于更新通道Qos等信息。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
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";
		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);
			Client client = new Client(config);
			UpdateTunnelInfo info = new UpdateTunnelInfo();
			TunnelQos qos = new TunnelQos();
			// MaxBandwidth, MaxQps 默认为0, 表示没有限制, MaxBandWidth的单位是bit,请按照实际需求填写。
			qos.setMaxBandwidth(1273741824L);
			qos.setMaxQps(1000);
			info.setTunnelQos(qos);
			UpdateTunnelRequest request = new UpdateTunnelRequest();
			request.setImportTunnel(info);
			client.updateTunnel(userId, tunnelId, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}删除通道
以下示例代码用于删除指定通道。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
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";
		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);
			Client client = new Client(config);
			client.deleteTunnel(userId, tunnelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}后续步骤
通道创建完成后,您可以选择继续进行代理的创建操作,具体信息请参见代理。
该文章对您有帮助吗?