繁体   English   中英

Java OPC-UA 客户端 Eclipse Milo 端点 URL 更改为 localhost

[英]Java OPC-UA Client Eclipse Milo endpoint URL changes to localhost

我正在使用 Java OPC-UA 客户端Eclipse Milo 每当我使用服务器的端点 URL 创建会话时,方法UaTcpStackClient.getEndpoints()将 URL 更改为localhost

String endpointUrl = "opc.tcp://10.8.0.104:48809";

EndpointDescription[] endpoints = UaTcpStackClient.getEndpoints(endpointUrl).get();

EndpointDescription endpoint = Arrays.stream(endpoints)
            .filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getSecurityPolicyUri()))
            .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));

然而endpoint.getEndpointUrl()值返回opc.tcp://127.0.0.1:4880/导致连接失败。

我不知道为什么我的 OPC URL 会改变?

在实现 UA 客户端时,这是一个非常常见的问题。

服务器最终负责您返回的端点的内容,而您要连接的端点显然被(错误地)配置为在端点 URL 中返回 127.0.0.1。

您需要检查从服务器获取的端点,然后根据应用程序的性质,立即将它们替换为包含您已修改的 URL 的新复制EndpointDescription ,或者让用户知道并首先请求他们的许可。

无论哪种方式,您都需要创建一组新的EndpointDescription ,在继续创建OpcUaClient之前更正了其中的 URL。

或者……您可以弄清楚如何正确配置您的服务器,以便它返回包含可公开访问的主机名或 IP 地址的端点。

更新 2:

从 v0.2.2 开始,有EndpointUtil.updateUrl可以用来修改EndpointDescription s。

更新:

替换端点 URL 的代码可能是以下代码的一些变体:

private static EndpointDescription updateEndpointUrl(
    EndpointDescription original, String hostname) throws URISyntaxException {

    URI uri = new URI(original.getEndpointUrl()).parseServerAuthority();

    String endpointUrl = String.format(
        "%s://%s:%s%s",
        uri.getScheme(),
        hostname,
        uri.getPort(),
        uri.getPath()
    );

    return new EndpointDescription(
        endpointUrl,
        original.getServer(),
        original.getServerCertificate(),
        original.getSecurityMode(),
        original.getSecurityPolicyUri(),
        original.getUserIdentityTokens(),
        original.getTransportProfileUri(),
        original.getSecurityLevel()
    );
}

警告:这在大多数情况下都有效,但一个值得注意的情况是当远程端点 URL 包含 URL 主机名中不允许的字符时(根据 RFC),例如下划线('_'),不幸的是,这在例如 Windows 机器的主机名中是允许的。 因此,您可能需要使用其他一些方法来解析端点 URL,而不是依赖 URI 类来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM