<> preface

Because of security vulnerabilities, we will dubbo Upgrade to 2.7.15 edition , When the environment exists docker When deploying .dubbo Get native ip Will become docker0 Address of .

<> practice

Let's see dubbo
obtain ip Address source code org.apache.dubbo.common.utils.NetUtils#getLocalAddress0 To get the local address .
You can see that the key is findNetworkInterface() Get the address of the network card

We will findNetworkInterface This method is posted , Get to see networkInterface There are three steps ,
public static NetworkInterface findNetworkInterface() { List<NetworkInterface>
validNetworkInterfaces = emptyList(); try { validNetworkInterfaces =
getValidNetworkInterfaces(); } catch (Throwable e) { logger.warn(e); }
NetworkInterface result = null; // Try to find the preferred one
// one , Check whether all network cards isPreferredNetworkInterface(networkInterface) Priority network card
// Judge whether there is a priority network card by name //String preferredNetworkInterface =
System.getProperty(DUBBO_PREFERRED_NETWORK_INTERFACE); // return
Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
for (NetworkInterface networkInterface : validNetworkInterfaces) { if
(isPreferredNetworkInterface(networkInterface)) { result = networkInterface;
break; } } if (result == null) { // If not found, try to get the first one for
(NetworkInterface networkInterface : validNetworkInterfaces) {
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while
(addresses.hasMoreElements()) { Optional<InetAddress> addressOp =
toValidAddress(addresses.nextElement()); if (addressOp.isPresent()) { try { if
(addressOp.get().isReachable(100)) { return networkInterface; } } catch
(IOException e) { // ignore } } } } } if (result == null) { result =
first(validNetworkInterfaces); } return result; } public static String
getHostName(String address) { try { int i = address.indexOf(':'); if (i > -1) {
address = address.substring(0, i); } String hostname =
HOST_NAME_CACHE.get(address); if (hostname != null && hostname.length() > 0) {
return hostname; } InetAddress inetAddress = InetAddress.getByName(address); if
(inetAddress != null) { hostname = inetAddress.getHostName();
HOST_NAME_CACHE.put(address, hostname); return hostname; } } catch (Throwable
e) { // ignore } return address; }
one , Check whether all network cards isPreferredNetworkInterface(networkInterface) Priority network card
Judge whether there is a priority network card by name

two , If the priority network card is not configured or does not exist
Get when 0.1s Network accessible network card

three , If none of the previous conditions succeed
Direct acquisition i Existing network card

According to the above three judgment conditions, if the network card is not what you want , You can configure the priority network card to get the network card you want .

String preferredNetworkInterface =
System.getProperty(DUBBO_PREFERRED_NETWORK_INTERFACE);
return Objects.equals(networkInterface.getDisplayName(),
preferredNetworkInterface);

DUBBO_PREFERRED_NETWORK_INTERFACE The value of is dubbo.network.interface.preferred
Then it can be in the startup command or yml Configure network card name

-Ddubbo.network.interface.preferred=eth0 ( Add to startup command )

yml Configure priority network card name :

dubbo:
network:
interface:
preferred: eth0

At the same time, you can also eliminate the wrong network card by ignoring the network card
-Ddubbo.network.interface.ignored=docker0

yml Configure priority network card name :

dubbo:
network:
interface:
ignored: docker0

Technology