Kirito,
Pls. note NetUtils.getLocalHost intends to return the valid public address
which can be able to be discoverable from the registry center.
The current behavior is: if there's no public address found, then 127.0.0.1
will be used as fallback value:
```
public static String getLocalHost() {
InetAddress address = getLocalAddress();
return address == null ? Constants.LOCALHOST_VALUE :
address.getHostAddress();
}
```
When there are multiple network interfaces, the first resolved public
address will be returned. This behavior will lead to dubbo sometimes return
the address from the virtual network interface, usually configured by
virtual machine.
So my suggestions are:
1. Should we throw IllegalStateException when there's no public address
found instead of falling back to localhost?
2. How can we distinguish virtual network interfaces accurately from others?
Thanks,
-Ian.
On Thu, Apr 4, 2019 at 5:21 PM 徐靖峰 <[email protected]> wrote:
> Hi,
> I notice that recently there are some issue about NetUtils.getLocalHost,
> someone complains about that logic changes bring by
> issue: https://github.com/apache/incubator-dubbo/issues/538 <
> https://github.com/apache/incubator-dubbo/issues/538>
> pr: https://github.com/apache/incubator-dubbo/pull/3520 <
> https://github.com/apache/incubator-dubbo/pull/3520>
> The change places in 2.7.1 is NetUtils.getLocalHost add a new check
> method:
> > static boolean isValidPublicAddress(InetAddress address) {
> > return !address.isSiteLocalAddress() && !address.isLoopbackAddress();
> > }
> This will exclude local site address like:
> 10/8 prefix
> 172.16/12 prefix
> 192.168/16 prefix
> so a internal ip 172.161.3.4 will successfully register into registry
> before, but will register localhost(mostly 127.0.0.1) into registry if no
> public ip exists.
>
> Action:
> 1. Add a param switch let user choose to prefer to register local site
> address or public address.
> 2. The default priority to find useful address in multi network cards can
> be : public > internal > localhost.