Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve NetUtils #3953

Merged
merged 4 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,23 @@ static boolean isValidV4Address(InetAddress address) {
return false;
}
String name = address.getHostAddress();
return (name != null
boolean result = (name != null
&& IP_PATTERN.matcher(name).matches()
&& !Constants.ANYHOST_VALUE.equals(name)
&& !Constants.LOCALHOST_VALUE.equals(name));
return result;
}

/**
* Check if an ipv6 address is reachable.
* Check if an ipv6 address
*
* @param address the given address
* @return true if it is reachable
*/
static boolean isValidV6Address(Inet6Address address) {
static boolean isPreferIPV6Address() {
boolean preferIpv6 = Boolean.getBoolean("java.net.preferIPv6Addresses");
if (!preferIpv6) {
return false;
}
try {
return address.isReachable(100);
} catch (IOException e) {
// ignore
}
return false;
}

Expand Down Expand Up @@ -234,7 +229,7 @@ public static InetAddress getLocalAddress() {
private static Optional<InetAddress> toValidAddress(InetAddress address) {
if (address instanceof Inet6Address) {
Inet6Address v6Address = (Inet6Address) address;
if (isValidV6Address(v6Address)) {
if (isPreferIPV6Address()) {
return Optional.ofNullable(normalizeV6Address(v6Address));
}
}
Expand Down Expand Up @@ -264,12 +259,21 @@ private static InetAddress getLocalAddress0() {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
if (network.isLoopback() || network.isVirtual() || !network.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = network.getInetAddresses();
while (addresses.hasMoreElements()) {
try {
Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
if (addressOp.isPresent()) {
return addressOp.get();
try {
if(addressOp.get().isReachable(100)){
return addressOp.get();
}
} catch (IOException e) {
// ignore
}
}
} catch (Throwable e) {
logger.warn(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void testIsValidV6Address() {
System.setProperty("java.net.preferIPv6Addresses", "true");
InetAddress address = NetUtils.getLocalAddress();
if (address instanceof Inet6Address) {
assertThat(NetUtils.isValidV6Address((Inet6Address) address), equalTo(true));
assertThat(NetUtils.isPreferIPV6Address(), equalTo(true));
}
System.setProperty("java.net.preferIPv6Addresses", saved);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ public class ApplicationConfig extends AbstractConfig {
*/
private String shutwait;


private Boolean preferPublicIp;


public ApplicationConfig() {
}

Expand Down Expand Up @@ -331,11 +327,4 @@ public boolean isValid() {
return !StringUtils.isEmpty(name);
}

public Boolean getPreferPublicIp() {
return preferPublicIp;
}

public void setPreferPublicIp(Boolean preferPublicIp) {
this.preferPublicIp = preferPublicIp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public class Application {
* launch the application
*/
public static void main(String[] args) throws Exception {
System.setProperty("DUBBO_IP_TO_REGISTRY", "4.3.2.1");
ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
service.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.export();
Expand Down