agent代理

更新时间:

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

agent代理架构图

image.jpgimage.jpg

Backend-Agent端

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

代码模式

Maven坐标

  <dependency>
       <groupId>com.aliyun.iotx</groupId>
       <artifactId>iotx-edge-tunnel-core</artifactId>
       <version>1.0.0-SNAPSHOT</version>
  </dependency>

启动demo

package yourpackage;

import com.aliyun.iotx.edge.tunnel.core.backend.BackendBootstrap;
import com.aliyun.iotx.edge.tunnel.core.common.model.AuthType;
import io.netty.channel.ChannelFuture;

@SuppressWarnings("all")
public class ApplicationBackendDemo {

    private static final String appKey = "<appKey>";
    private static final String appSecret = "<appSecret>";

    public static void main(String[] args) throws Exception {
        BackendBootstrap bootstrap = new BackendBootstrap()
                .authType(AuthType.APPLICATION.getType())
                .appKey(appKey)
                .appSecret(appSecret);

        // 阻塞直到连接断开
        ChannelFuture closeFuture = bootstrap.start();
    }
}

JAR模式

JAR包下载地址

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

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

Frontend-Agent端

Maven坐标

<dependency>
    <groupId>com.aliyun.iotx</groupId>
    <artifactId>iotx-api-gateway-client</artifactId>
    <version>1.0.6-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.aliyun.iotx</groupId>
    <artifactId>iotx-edge-tunnel-core</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

启动代码

package yourpackage;

import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.fastjson.JSON;
import com.aliyun.iotx.api.client.IoTApiClientBuilderParams;
import com.aliyun.iotx.api.client.IoTApiRequest;
import com.aliyun.iotx.api.client.SyncApiClient;
import com.aliyun.iotx.edge.tunnel.core.common.model.AuthType;
import com.aliyun.iotx.edge.tunnel.core.frontend.FrontendBootstrap;
import io.netty.channel.ChannelFuture;
import org.apache.commons.collections4.MapUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@SuppressWarnings("all")
public class ApplicationFrontendHsfDemo {

    private static final String appKey = "<appKey>";
    private static final String appSecret = "<appSecret>";
    private static final int frontendServerPort = 9999; // frontend-agent在本机暴露哪个端口进行代理
    private static final String backendServiceIp = "将数据流转到 backend-agent 所在网络的哪个ip"; // 不填,默认为localhost
    private static final int backendServicePort = 3306; // 将数据流转到 backend-agent 所在网络中 ip为 backendServiceIp 的机器的哪个端口,默认22端口
    private static String host = "api.link.aliyun.com";
    private static String path = "/app/tunnel/token/get";
    private  static SyncApiClient syncApiClient;

    public static void main(String[] args) throws Exception {
        initApiClient();
        FrontendBootstrap frontendBootstrap = new FrontendBootstrap()
                .authType(AuthType.APPLICATION.getType())
                .appKey(appKey)
                .frontendServerPort(frontendServerPort)
                .backendServiceIp(backendServiceIp)
                .backendServicePort(backendServicePort)// 将数据流路由到backend测的哪个端口
                .tenantTokenLoader(()->getTenantToken());

        ChannelFuture closeFuture = frontendBootstrap.start();

    }

    public static void initApiClient() {
        IoTApiClientBuilderParams ioTApiClientBuilderParams =
                new IoTApiClientBuilderParams();
        ioTApiClientBuilderParams.setAppKey(appKey);
        ioTApiClientBuilderParams.setAppSecret(appSecret);
        syncApiClient = new SyncApiClient(ioTApiClientBuilderParams);
    }

    private static String getTenantToken() {
        IoTApiRequest request = new IoTApiRequest();
        String uuid = UUID.randomUUID().toString();// 设置请求ID
        String id = uuid.replace("-", "");
        request.setId(id);
        request.setApiVer("1.0.0");// 设置API版本号
        Map<String, String> headers = new HashMap<String, String>(8);
        ApiResponse response = null;
        try {
            response = syncApiClient.postBody(host, path, request, true, headers);// 设置请求参数域名、path、request , isHttps, headers
            String result = new String(response.getBody(), "UTF-8");
            if (result != null) {
                Map map = JSON.parseObject(result);
                Map dataMap = (Map) map.get("data");
                String tenantToken = MapUtils.getString(dataMap, "tenantToken");
                return tenantToken;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}