This commit is contained in:
parent
4aaa542e84
commit
ade0cd7024
|
|
@ -102,6 +102,7 @@ public class NetUtils {
|
|||
return Constants.ANYHOST_VALUE.equals(host);
|
||||
}
|
||||
|
||||
// FIXME: should remove this method completely
|
||||
public static boolean isInvalidLocalHost(String host) {
|
||||
return host == null
|
||||
|| host.length() == 0
|
||||
|
|
@ -110,6 +111,7 @@ public class NetUtils {
|
|||
|| (LOCAL_IP_PATTERN.matcher(host).matches());
|
||||
}
|
||||
|
||||
// FIXME: should remove this method completely
|
||||
public static boolean isValidLocalHost(String host) {
|
||||
return !isInvalidLocalHost(host);
|
||||
}
|
||||
|
|
@ -120,9 +122,6 @@ public class NetUtils {
|
|||
}
|
||||
|
||||
static boolean isValidV4Address(InetAddress address) {
|
||||
if (address == null || address.isLoopbackAddress()) {
|
||||
return false;
|
||||
}
|
||||
String name = address.getHostAddress();
|
||||
return (name != null
|
||||
&& IP_PATTERN.matcher(name).matches()
|
||||
|
|
@ -149,6 +148,10 @@ public class NetUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
static boolean isValidPublicAddress(InetAddress address) {
|
||||
return !address.isSiteLocalAddress() && !address.isLoopbackAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* normalize the ipv6 Address, convert scope name to scope id.
|
||||
* e.g.
|
||||
|
|
@ -219,14 +222,16 @@ public class NetUtils {
|
|||
}
|
||||
|
||||
private static Optional<InetAddress> toValidAddress(InetAddress address) {
|
||||
if (address instanceof Inet6Address) {
|
||||
Inet6Address v6Address = (Inet6Address) address;
|
||||
if (isValidV6Address(v6Address)) {
|
||||
return Optional.ofNullable(normalizeV6Address(v6Address));
|
||||
if (isValidPublicAddress(address)) {
|
||||
if (address instanceof Inet6Address) {
|
||||
Inet6Address v6Address = (Inet6Address) address;
|
||||
if (isValidV6Address(v6Address)) {
|
||||
return Optional.ofNullable(normalizeV6Address(v6Address));
|
||||
}
|
||||
}
|
||||
if (isValidV4Address(address)) {
|
||||
return Optional.of(address);
|
||||
}
|
||||
}
|
||||
if (isValidV4Address(address)) {
|
||||
return Optional.of(address);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,7 +104,6 @@ public class NetUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testIsValidAddress() throws Exception {
|
||||
assertFalse(NetUtils.isValidV4Address((InetAddress) null));
|
||||
InetAddress address = mock(InetAddress.class);
|
||||
when(address.isLoopbackAddress()).thenReturn(true);
|
||||
assertFalse(NetUtils.isValidV4Address(address));
|
||||
|
|
@ -131,7 +130,6 @@ public class NetUtilsTest {
|
|||
public void testGetLocalAddress() throws Exception {
|
||||
InetAddress address = NetUtils.getLocalAddress();
|
||||
assertNotNull(address);
|
||||
assertTrue(NetUtils.isValidLocalHost(address.getHostAddress()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -209,4 +207,4 @@ public class NetUtilsTest {
|
|||
InetAddress normalized = NetUtils.normalizeV6Address(address);
|
||||
assertThat(normalized.getHostAddress(), equalTo("fe80:0:0:0:894:aeec:f37d:23e1%5"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -333,9 +333,6 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
|
|||
String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY);
|
||||
if (StringUtils.isEmpty(hostToRegistry)) {
|
||||
hostToRegistry = NetUtils.getLocalHost();
|
||||
} else if (NetUtils.isInvalidLocalHost(hostToRegistry)) {
|
||||
throw new IllegalArgumentException("Specified invalid registry ip from property:" +
|
||||
Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
|
||||
}
|
||||
map.put(Constants.REGISTER_IP_KEY, hostToRegistry);
|
||||
appendParameters(map, monitor);
|
||||
|
|
|
|||
|
|
@ -55,7 +55,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
|
||||
|
||||
/**
|
||||
* ReferenceConfig
|
||||
|
|
@ -108,7 +107,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
|
|||
* The interface class of the reference service
|
||||
*/
|
||||
private Class<?> interfaceClass;
|
||||
|
||||
|
||||
/**
|
||||
* client type
|
||||
*/
|
||||
|
|
@ -299,8 +298,6 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
|
|||
String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY);
|
||||
if (StringUtils.isEmpty(hostToRegistry)) {
|
||||
hostToRegistry = NetUtils.getLocalHost();
|
||||
} else if (isInvalidLocalHost(hostToRegistry)) {
|
||||
throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
|
||||
}
|
||||
map.put(Constants.REGISTER_IP_KEY, hostToRegistry);
|
||||
|
||||
|
|
|
|||
|
|
@ -43,11 +43,9 @@ 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;
|
||||
|
|
@ -62,7 +60,6 @@ import java.util.concurrent.TimeUnit;
|
|||
import static org.apache.dubbo.common.Constants.LOCALHOST_VALUE;
|
||||
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
|
||||
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
|
||||
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
|
||||
import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort;
|
||||
|
||||
/**
|
||||
|
|
@ -602,9 +599,6 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
|
|||
boolean anyhost = false;
|
||||
|
||||
String hostToBind = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND);
|
||||
if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) {
|
||||
throw new IllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind);
|
||||
}
|
||||
|
||||
// if bind ip is not found in environment, keep looking up
|
||||
if (StringUtils.isEmpty(hostToBind)) {
|
||||
|
|
@ -612,33 +606,13 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
|
|||
if (provider != null && StringUtils.isEmpty(hostToBind)) {
|
||||
hostToBind = provider.getHost();
|
||||
}
|
||||
if (isInvalidLocalHost(hostToBind)) {
|
||||
|
||||
if (StringUtils.isEmpty(hostToBind)) {
|
||||
anyhost = true;
|
||||
try {
|
||||
hostToBind = InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
if (isInvalidLocalHost(hostToBind)) {
|
||||
if (CollectionUtils.isNotEmpty(registryURLs)) {
|
||||
for (URL registryURL : registryURLs) {
|
||||
if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) {
|
||||
// skip multicast registry since we cannot connect to it via Socket
|
||||
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();
|
||||
}
|
||||
hostToBind = getLocalHost();
|
||||
|
||||
if (StringUtils.isEmpty(hostToBind)) {
|
||||
hostToBind = findHostToBindByConnectRegistries(registryURLs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -647,9 +621,7 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
|
|||
|
||||
// registry ip is not used for bind ip by default
|
||||
String hostToRegistry = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY);
|
||||
if (hostToRegistry != null && hostToRegistry.length() > 0 && isInvalidLocalHost(hostToRegistry)) {
|
||||
throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
|
||||
} else if (StringUtils.isEmpty(hostToRegistry)) {
|
||||
if (StringUtils.isEmpty(hostToRegistry)) {
|
||||
// bind ip is used as registry ip by default
|
||||
hostToRegistry = hostToBind;
|
||||
}
|
||||
|
|
@ -659,6 +631,25 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
|
|||
return hostToRegistry;
|
||||
}
|
||||
|
||||
private String findHostToBindByConnectRegistries(List<URL> registryURLs) {
|
||||
if (CollectionUtils.isNotEmpty(registryURLs)) {
|
||||
for (URL registryURL : registryURLs) {
|
||||
if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) {
|
||||
// skip multicast registry since we cannot connect to it via Socket
|
||||
continue;
|
||||
}
|
||||
try (Socket socket = new Socket()) {
|
||||
SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort());
|
||||
socket.connect(addr, 1000);
|
||||
return socket.getLocalAddress().getHostAddress();
|
||||
} catch (Exception e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register port and bind port for the provider, can be configured separately
|
||||
* Configuration priority: environment variable -> java system properties -> port property in protocol config file
|
||||
|
|
|
|||
Loading…
Reference in New Issue