基于多个DNS结果的Java传出TCP连接故障切换 [英] Java outgoing TCP connection failover based on multiple DNS results

查看:189
本文介绍了基于多个DNS结果的Java传出TCP连接故障切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 new Socket(unit.domain.com,100) unit.domain.com DNS记录在A记录中有多个IP地址。如果连接失败,Java会自动连接到浏览器中列表中的其他地址之一吗?或者必须手动执行?

If I make a connection using new Socket("unit.domain.com", 100) and the unit.domain.com DNS record has multiple IP addresses in the A record.. In the event of a failed connection, Does Java automatically connect to one of the other addresses in the list like the browser does? or does that have to be implemented manually?

推荐答案

不!
通过新的Socket(String,int)创建一个套接字导致像这样的解析

No! Creating a socket via new Socket(String, int) results in a resolving like that

addr = InetAddress.getByName(hostname);

这是

return InetAddress.getAllByName(host)[0];

地址解析在Socket c-tor中执行。

The address resolution is performed in the Socket c-tor.

如果必须重新连接(故障切换),请使用InetAddress.getAllByName(host)返回的结果,随机化(或使用循环)并连接到必要的地址。

If you have to reconnect (failover) use the result returned by InetAddress.getAllByName(host), randomize (or use round-robin) and connect to the necessary addresses.

编辑:如果你需要连接一些可能的故障,你最好使用超时的Socket类的connect方法。 确保您关闭甚至失败的套接字(以及特别频道),因为它们可能会在* Nix上泄漏FD。

also if you are going to need to connect with some likely failure, you'd be better off using connect method of the Socket class with a timeout. Also make sure you close even failed sockets (and esp. channels) since they may leak a FD on *Nix.

这篇关于基于多个DNS结果的Java传出TCP连接故障切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆