Skip to content

Commit

Permalink
[ISSUE alibaba#6160] Optimization of IP address acquisition method (a…
Browse files Browse the repository at this point in the history
…libaba#6320)

* [ISSUE alibaba#6160] Optimization of IP address acquisition method

* add test case and fix bug

* perform the cleanup operation after the method
  • Loading branch information
bdqfork authored Jul 14, 2021
1 parent 12a9bfb commit 10ef182
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
35 changes: 27 additions & 8 deletions api/src/main/java/com/alibaba/nacos/api/utils/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
* @author xuanyin.zy
*/
public class NetUtils {


@Deprecated
private static final String CLIENT_NAMING_LOCAL_IP_PROPERTY = "com.alibaba.nacos.client.naming.local.ip";


private static final String CLIENT_LOCAL_IP_PROPERTY = "com.alibaba.nacos.client.local.ip";

private static final String CLIENT_LOCAL_PREFER_HOSTNAME_PROPERTY = "com.alibaba.nacos.client.local.preferHostname";

private static final String LEGAL_LOCAL_IP_PROPERTY = "java.net.preferIPv6Addresses";

private static final String DEFAULT_SOLVE_FAILED_RETURN = "resolve_failed";
Expand All @@ -48,14 +53,28 @@ public static String localIP() {
if (!StringUtils.isEmpty(localIp)) {
return localIp;
}

String ip = System.getProperty(CLIENT_NAMING_LOCAL_IP_PROPERTY, findFirstNonLoopbackAddress());

if (System.getProperties().containsKey(CLIENT_LOCAL_IP_PROPERTY)) {
return localIp = System.getProperty(CLIENT_LOCAL_IP_PROPERTY, getAddress());
}

String ip = System.getProperty(CLIENT_NAMING_LOCAL_IP_PROPERTY, getAddress());

return localIp = ip;

}

private static String getAddress() {
InetAddress inetAddress = findFirstNonLoopbackAddress();
if (inetAddress == null) {
return DEFAULT_SOLVE_FAILED_RETURN;
}

boolean preferHost = Boolean.parseBoolean(System.getProperty(CLIENT_LOCAL_PREFER_HOSTNAME_PROPERTY));
return preferHost ? inetAddress.getHostName() : inetAddress.getHostAddress();
}

private static String findFirstNonLoopbackAddress() {
private static InetAddress findFirstNonLoopbackAddress() {
InetAddress result = null;

try {
Expand Down Expand Up @@ -87,16 +106,16 @@ private static String findFirstNonLoopbackAddress() {
}

if (result != null) {
return result.getHostAddress();
return result;
}

try {
return InetAddress.getLocalHost().getHostAddress();
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
//ignore
}

return DEFAULT_SOLVE_FAILED_RETURN;
return null;

}
}
66 changes: 66 additions & 0 deletions api/src/test/java/com/alibaba/nacos/api/utils/NetUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.api.utils;

import org.junit.After;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;

import static org.junit.Assert.assertEquals;

public class NetUtilsTest {

@After
public void tearDown() throws Exception {
Class<?> clazz = Class.forName("com.alibaba.nacos.api.utils.NetUtils");
Field field = clazz.getDeclaredField("localIp");
field.setAccessible(true);
field.set(null, "");
System.clearProperty("com.alibaba.nacos.client.local.ip");
System.clearProperty("com.alibaba.nacos.client.naming.local.ip");
System.clearProperty("com.alibaba.nacos.client.local.preferHostname");
}

@Test
public void testLocalIP() {
System.setProperty("com.alibaba.nacos.client.naming.local.ip", "10.2.7.8");
System.setProperty("com.alibaba.nacos.client.local.ip", "10.2.8.8");
assertEquals("10.2.8.8", NetUtils.localIP());
}

@Test
public void testCompatibleLocalIP() {
System.setProperty("com.alibaba.nacos.client.naming.local.ip", "10.2.7.8");
assertEquals("10.2.7.8", NetUtils.localIP());
}

@Test
public void testPreferHostname() throws Exception {
Class<?> clazz = Class.forName("com.alibaba.nacos.api.utils.NetUtils");
Method method = clazz.getDeclaredMethod("findFirstNonLoopbackAddress");
method.setAccessible(true);
InetAddress inetAddress = (InetAddress) method.invoke(null);
String hostname = inetAddress.getHostName();

System.setProperty("com.alibaba.nacos.client.local.preferHostname", "true");
assertEquals(hostname, NetUtils.localIP());
}

}

0 comments on commit 10ef182

Please sign in to comment.