我正在使用 Java OPC-UA 客户端 Eclipse Milo .每当我使用服务器端点 URL 创建 session 时,方法 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
。
更新:
替换端点 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 类来完成。
关于Java OPC-UA 客户端 Eclipse Milo 端点 URL 更改为本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554198/