Skip to content

Commit

Permalink
[master] Simplify and unify local address getting method (#8679)
Browse files Browse the repository at this point in the history
* simplify and unify local address getting method

* simplify getIpByConfig()
  • Loading branch information
zrlw committed Sep 8, 2021
1 parent 2667985 commit 233be16
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 44 deletions.
Expand Up @@ -244,9 +244,7 @@ public static String getIpByConfig() {
return configIp;
}

InetAddress localAddress = getLocalAddress();
String hostName = localAddress == null ? LOCALHOST_VALUE : localAddress.getHostName();
return getIpByHost(hostName);
return getLocalHost();
}

/**
Expand Down Expand Up @@ -417,8 +415,7 @@ public static NetworkInterface findNetworkInterface() {
if (addressOp.isPresent()) {
try {
if (addressOp.get().isReachable(100)) {
result = networkInterface;
break;
return networkInterface;
}
} catch (IOException e) {
// ignore
Expand Down
Expand Up @@ -50,11 +50,6 @@
import org.apache.dubbo.rpc.support.ProtocolUtils;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -83,7 +78,6 @@
import static org.apache.dubbo.common.constants.CommonConstants.SERVICE_NAME_MAPPING_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_PROTOCOL;
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
Expand All @@ -92,8 +86,6 @@
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_BIND;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.MULTICAST;
import static org.apache.dubbo.config.Constants.MULTIPLE;
import static org.apache.dubbo.config.Constants.SCOPE_NONE;
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
Expand All @@ -108,6 +100,8 @@

public class ServiceConfig<T> extends ServiceConfigBase<T> {

private static final long serialVersionUID = -412700146501624375L;

public static final Logger logger = LoggerFactory.getLogger(ServiceConfig.class);

/**
Expand Down Expand Up @@ -596,37 +590,8 @@ private String findConfigedHosts(ProtocolConfig protocolConfig,
}
if (isInvalidLocalHost(hostToBind)) {
anyhost = true;
try {
logger.info("No valid ip found from environment, try to find valid host from DNS.");
hostToBind = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.warn(e.getMessage(), e);
}
if (isInvalidLocalHost(hostToBind)) {
if (CollectionUtils.isNotEmpty(registryURLs)) {
for (URL registryURL : registryURLs) {
if (MULTICAST.equalsIgnoreCase(registryURL.getParameter(REGISTRY_KEY))) {
// skip multicast registry since we cannot connect to it via Socket
continue;
}
if (MULTIPLE.equalsIgnoreCase(registryURL.getParameter("registry"))) {
// skip, multiple-registry address is a fake ip
continue;
}
try (Socket socket = new Socket()) {
SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort());
socket.connect(addr, 1000);
hostToBind = socket.getLocalAddress().getHostAddress();
break;
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
}
if (isInvalidLocalHost(hostToBind)) {
hostToBind = getLocalHost();
}
}
logger.info("No valid ip found from environment, try to get local host.");
hostToBind = getLocalHost();
}
}

Expand Down

0 comments on commit 233be16

Please sign in to comment.