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

Har*_*sar 6 java localhost opc-ua milo

我正在使用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"));
Run Code Online (Sandbox Code Playgroud)

但是return的endpoint.getEndpointUrl()opc.tcp://127.0.0.1:4880/会导致连接失败。

我不知道为什么我的OPC URL被更改了?

Kev*_*ron 5

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

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

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

无论哪种方式,您都需要创建一组新的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()
    );
}
Run Code Online (Sandbox Code Playgroud)

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