Skip to content

Commit

Permalink
improve NetUtils (#3953)
Browse files Browse the repository at this point in the history
* 1. filter network interface in NetUtils 2. remove the useless attribute in ApplicationConfig

* add reachable check for ipv4

* move the reachable check to outside

* rename isValidV6Address to isPreferIPV6Address
  • Loading branch information
lexburner authored and beiwei30 committed May 5, 2019
1 parent ff50a29 commit 74a491b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
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

0 comments on commit 74a491b

Please sign in to comment.