英文:
How to properly ping a hostname that resolves to multiple IP addresses?
问题
我想确保网络资源可用。
我知道如何ping一个单独的IP地址,大多数示例都展示了如何从主机名获取一个IP地址,然后对其进行ping。
但是由于主机名可以解析为多个IP地址,如IPAddress[] System.Networking.Dns.GetHostAddress(string)
所返回的那样,我应该如何正确地处理这种情况?
我是否应该对它们都进行ping?如果其中任何一个可用,是否就足够了?
英文:
I want to ensure network resources are available.
I know how to ping a single IP address and most examples show how to get an IP from a hostname to then ping it.
But as a hostname can resolve to multiple IP addresses as returned by IPAddress[] System.Networking.Dns.GetHostAddress(string)
, how do I do that properly?
Should I ping all of them? Is it sufficient if any of them is available?
答案1
得分: 1
这取决于您想要验证什么。多个IP地址意味着服务可以从不同的物理服务器访问(可能位于世界上不同地区)。一个典型的应用程序会选择其中任何一个,从而导致服务器之间的隐式负载均衡。
因此,如果您想检查特定服务是否可访问,您可以随意ping其中任何一个地址。特别是如果这些是您自己的服务,您可能希望ping所有这些地址,以确保您的分布式服务器都正常工作。
无论哪种情况,您应该意识到ping是一种非常低级的检查服务可用性的方法。仅仅因为服务器响应ping请求并不代表其更高级别服务的可用性(例如http、电子邮件等)。甚至相反的情况也可能发生:服务器可能工作正常,但由于防火墙规则的原因而阻止ping请求。
英文:
That depends on what you want to verify. Multiple IPs means that the services are available from different physical servers (possibly in different parts of the world). A typical application would just take any of them, which then causes an implicit load balancing between the servers.
So if you want to check whether a particular service is reachable, you can just ping any of the addresses. Particularly if these are your own services you might want to ping all of them to make sure your distributed servers are all working.
In either case, you should be aware that pinging is a very low-level method of checking service availability. Just because a server responds to a ping request doesn't say anything about the availability of its higher-level services (e.g. http, e-mail, whatever). Even the opposite is true: The server might work just fine but have ping blocked by firewall rules.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论