全部产品
云市场

agent代理

更新时间:2019-11-18 13:06:25

agent代理是阿里云iot研发的一款高安全等级的网络代理工具,有效将本地服务通过反向代理通道与云端服务处于一个调用平面,方便开发者在云测和本地服务进行联动。

agent代理架构图

image.jpg

image.jpg

Backend-Agent端

远程Agent启动目前支持两种方式

代码模式
maven坐标
  1. <dependency>
  2. <groupId>com.aliyun.iotx</groupId>
  3. <artifactId>iotx-edge-tunnel-core</artifactId>
  4. <version>1.0.0-SNAPSHOT</version>
  5. </dependency>
启动demo
  1. package yourpackage;
  2. import com.aliyun.iotx.edge.tunnel.core.backend.BackendBootstrap;
  3. import com.aliyun.iotx.edge.tunnel.core.common.model.AuthType;
  4. import io.netty.channel.ChannelFuture;
  5. @SuppressWarnings("all")
  6. public class ApplicationBackendDemo {
  7. private static final String appKey = "<appKey>";
  8. private static final String appSecret = "<appSecret>";
  9. public static void main(String[] args) throws Exception {
  10. BackendBootstrap bootstrap = new BackendBootstrap()
  11. .authType(AuthType.APPLICATION.getType())
  12. .appKey(appKey)
  13. .appSecret(appSecret);
  14. // 阻塞直到连接断开
  15. ChannelFuture closeFuture = bootstrap.start();
  16. }
  17. }
JAR模式
JAR包下载地址

https://edge-service-agent.oss-cn-shanghai.aliyuncs.com/iotx-remote-debug.zip

  1. 配置修改:
  2. {
  3. "serverUrl":"wss://backend-iotx-remote-debug.aliyun.com:443",
  4. "authType":"application",
  5. "appKey": "请输入appkey",
  6. "appSecret":"请输入appSecret"
  7. }
  1. 运行启动脚本
  2. linux:start_up.sh
  3. windows:start_up.bat

Frontend-Agent端

maven坐标
  1. <dependency>
  2. <groupId>com.aliyun.iotx</groupId>
  3. <artifactId>iotx-api-gateway-client</artifactId>
  4. <version>1.0.6-SNAPSHOT</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun.iotx</groupId>
  8. <artifactId>iotx-edge-tunnel-core</artifactId>
  9. <version>1.0.0-SNAPSHOT</version>
  10. </dependency>

启动代码

  1. package yourpackage;
  2. import com.alibaba.cloudapi.sdk.model.ApiResponse;
  3. import com.alibaba.fastjson.JSON;
  4. import com.aliyun.iotx.api.client.IoTApiClientBuilderParams;
  5. import com.aliyun.iotx.api.client.IoTApiRequest;
  6. import com.aliyun.iotx.api.client.SyncApiClient;
  7. import com.aliyun.iotx.edge.tunnel.core.common.model.AuthType;
  8. import com.aliyun.iotx.edge.tunnel.core.frontend.FrontendBootstrap;
  9. import io.netty.channel.ChannelFuture;
  10. import org.apache.commons.collections4.MapUtils;
  11. import java.io.UnsupportedEncodingException;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.UUID;
  15. @SuppressWarnings("all")
  16. public class ApplicationFrontendHsfDemo {
  17. private static final String appKey = "<appKey>";
  18. private static final String appSecret = "<appSecret>";
  19. private static final int frontendServerPort = 9999; // frontend-agent在本机暴露哪个端口进行代理
  20. private static final String backendServiceIp = "将数据流转到 backend-agent 所在网络的哪个ip"; // 不填,默认为localhost
  21. private static final int backendServicePort = 3306; // 将数据流转到 backend-agent 所在网络中 ip为 backendServiceIp 的机器的哪个端口,默认22端口
  22. private static String host = "api.link.aliyun.com";
  23. private static String path = "/app/tunnel/token/get";
  24. private static SyncApiClient syncApiClient;
  25. public static void main(String[] args) throws Exception {
  26. initApiClient();
  27. FrontendBootstrap frontendBootstrap = new FrontendBootstrap()
  28. .authType(AuthType.APPLICATION.getType())
  29. .appKey(appKey)
  30. .frontendServerPort(frontendServerPort)
  31. .backendServiceIp(backendServiceIp)
  32. .backendServicePort(backendServicePort)// 将数据流路由到backend测的哪个端口
  33. .tenantTokenLoader(()->getTenantToken());
  34. ChannelFuture closeFuture = frontendBootstrap.start();
  35. }
  36. public static void initApiClient() {
  37. IoTApiClientBuilderParams ioTApiClientBuilderParams =
  38. new IoTApiClientBuilderParams();
  39. ioTApiClientBuilderParams.setAppKey(appKey);
  40. ioTApiClientBuilderParams.setAppSecret(appSecret);
  41. syncApiClient = new SyncApiClient(ioTApiClientBuilderParams);
  42. }
  43. private static String getTenantToken() {
  44. IoTApiRequest request = new IoTApiRequest();
  45. String uuid = UUID.randomUUID().toString();// 设置请求ID
  46. String id = uuid.replace("-", "");
  47. request.setId(id);
  48. request.setApiVer("1.0.0");// 设置API版本号
  49. Map<String, String> headers = new HashMap<String, String>(8);
  50. ApiResponse response = null;
  51. try {
  52. response = syncApiClient.postBody(host, path, request, true, headers);// 设置请求参数域名、path、request , isHttps, headers
  53. String result = new String(response.getBody(), "UTF-8");
  54. if (result != null) {
  55. Map map = JSON.parseObject(result);
  56. Map dataMap = (Map) map.get("data");
  57. String tenantToken = MapUtils.getString(dataMap, "tenantToken");
  58. return tenantToken;
  59. }
  60. } catch (UnsupportedEncodingException e) {
  61. e.printStackTrace();
  62. }
  63. return null;
  64. }
  65. }