You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/08 04:40:14 UTC
[dubbo] branch master updated: [master] Simplify and unify local
address getting method (#8679)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 233be16 [master] Simplify and unify local address getting method (#8679)
233be16 is described below
commit 233be16448175a6519ae4356ba8f23588d51e30b
Author: zrlw <zr...@sina.com>
AuthorDate: Wed Sep 8 12:40:03 2021 +0800
[master] Simplify and unify local address getting method (#8679)
* simplify and unify local address getting method
* simplify getIpByConfig()
---
.../org/apache/dubbo/common/utils/NetUtils.java | 7 +---
.../org/apache/dubbo/config/ServiceConfig.java | 43 ++--------------------
2 files changed, 6 insertions(+), 44 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index a7f941f..07f84e8 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -244,9 +244,7 @@ public class NetUtils {
return configIp;
}
- InetAddress localAddress = getLocalAddress();
- String hostName = localAddress == null ? LOCALHOST_VALUE : localAddress.getHostName();
- return getIpByHost(hostName);
+ return getLocalHost();
}
/**
@@ -417,8 +415,7 @@ public class NetUtils {
if (addressOp.isPresent()) {
try {
if (addressOp.get().isReachable(100)) {
- result = networkInterface;
- break;
+ return networkInterface;
}
} catch (IOException e) {
// ignore
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index b59aba6..c133114 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -50,11 +50,6 @@ import org.apache.dubbo.rpc.service.GenericService;
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;
@@ -83,7 +78,6 @@ import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;
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;
@@ -92,8 +86,6 @@ import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort;
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;
@@ -108,6 +100,8 @@ import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY;
public class ServiceConfig<T> extends ServiceConfigBase<T> {
+ private static final long serialVersionUID = -412700146501624375L;
+
public static final Logger logger = LoggerFactory.getLogger(ServiceConfig.class);
/**
@@ -596,37 +590,8 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
}
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();
}
}