[英]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.