【问题标题】:Java OPC-UA Client Eclipse Milo endpoint URL changes to localhostJava OPC-UA 客户端 Eclipse Milo 端点 URL 更改为 localhost
【发布时间】:2016-11-11 18:38:56
【问题描述】:

我正在使用 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 会更改?

【问题讨论】:

    标签: java localhost opc-ua milo


    【解决方案1】:

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

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

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

    无论哪种方式,您都需要创建一组新的 EndpointDescriptions,在其中更正 URL,然后再继续创建 OpcUaClient。

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

    更新 2:

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

    更新:

    替换端点 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 类来完成。

    【讨论】:

    • 感谢 Kevin 的回复,我明白你想说什么.. 我怎么能创建新的 EndpointDescriptions 集,但我没有看到任何方法或设置方法
    猜你喜欢
    • 2017-02-18
    • 2020-07-24
    • 2020-08-28
    • 2016-09-20
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2022-07-08
    相关资源
    最近更新 更多