InetAddress getLocalHost function,

Source: Internet
Author: User

InetAddress getLocalHost function,

Today, we encountered a strange problem when running the DemoService 2.5.4-SNAPSHOT version of dubbo. Consumer cannot connect to provider services. It was finally discovered that dubbo's own local IP address detection code was not strong enough. Your provider is actually running on address A, but dubbo detects that the local IP address is B, and then it registers its own service address on zookeeper, this IP address B is used. When the consumer is connected to zookeeper, it is found that DemoService is on B. Obviously, this IP address does not provide dubbo services, then the consumer will certainly fail.

The result of dubbo's local IP address detection is incorrect, because dubbo's policy of detecting local IP addresses is to call InetAddress first. getLocalHost: if this method returns a valid address, it is regarded as a local IP address. Otherwise, all the local NICs are enumerated and the first valid IP address is returned as the local address.

InetAddress. getLocalHost returns an incorrect IP address. Why does this function return an incorrect address, because the principle of this function is to obtain the hostname of the local machine, and then parse the hostname to obtain the IP address. If the host name is directed to an incorrect IP address in the/etc/hosts file of the Local Machine, InetAddress. getLocalHost will return the wrong IP address. Of course, if your hostname is resolved by DNS, it may happen that the information on DNS is also wrong, and it is also a tragic ending.

If you encounter the same problem, you can first check where the address points in the settings of the hosts file, and then check the DNS resolution address. Of course, if the dubbo code checks whether the returned address is actually the IP address of the local machine, it will not cause this problem. In the final analysis, the dubbo card issuer believes in InetAddress too much. if getLocalHost is reached, this product should ensure that such an event will not appear. You return an IP address that is not the local machine as the local IP address. This is a semantic error. No matter how stupid an algorithm you use, the result is that a local IP address instead of a local IP Address should not be returned, isn't it? Otherwise, get local host !!!

Why do I say InetAddress. the getLocalHost algorithm is stupid because although it may be misled by the hosts file and DNS, it is clear that neither of these two locations is the authority of the local IP address, the Authority obtains the configuration information of the NIC. Even if you want to use unreliable source information, you should at least verify it with the local NIC address! If you really don't know how to get it, throwing an exception indicates that you are powerless and everyone is acceptable! I couldn't give a wrong message, so it would be a waste of time for so many people to fall into the trap, right?

Finally, dubbo detects that the code of the local address is located:
The getLocalAddress function in dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/NetUtils. java can be viewed.


In linux, what is the result of outputting InetAddressgetLocalHost (); in java?

InetAddress. getLocalHost () is not very powerful in Linux ...... You can try it. Although this method works normally in Windows, it will only return 127.0.0.1…… in Linux ......
Because the specific implementation of this method in Linux is only to read the configuration content in/etc/hosts, do not use this method in Linux to obtain the local IP address. If you really want to obtain it, you can refer to this webpage:
Www.javaeye.com/topic/169889
Or this:
Wjason.javaeye.com/blog/206320

Java obtains the IP address in the InetAddress host = InetAddressgetLocalHost () getAddress (); error, solution!

InetAddress host = InetAddress. getLocalHost ();
That's all.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.