You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/12/25 07:23:13 UTC
[dubbo] branch master updated: Refactor: remove
NetUtils.getHostAddress() and relace it with NetUtils.getLocalHost()
(#5526)
This is an automated email from the ASF dual-hosted git repository.
liujun 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 fa157de Refactor: remove NetUtils.getHostAddress() and relace it with NetUtils.getLocalHost() (#5526)
fa157de is described below
commit fa157deadf65d00c9884d611430ec78e585840ff
Author: LinShunKang <li...@gmail.com>
AuthorDate: Wed Dec 25 15:22:57 2019 +0800
Refactor: remove NetUtils.getHostAddress() and relace it with NetUtils.getLocalHost() (#5526)
---
.../ConsumerContextClusterInterceptor.java | 2 +-
.../org/apache/dubbo/common/utils/NetUtils.java | 22 ++++++++++------------
.../apache/dubbo/common/utils/NetUtilsTest.java | 7 +++++++
3 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
index 1216ed6..ec6323e 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
@@ -31,7 +31,7 @@ public class ConsumerContextClusterInterceptor implements ClusterInterceptor, Cl
public void before(AbstractClusterInvoker<?> invoker, Invocation invocation) {
RpcContext.getContext()
.setInvocation(invocation)
- .setLocalAddress(NetUtils.getHostAddress(), 0);
+ .setLocalAddress(NetUtils.getLocalHost(), 0);
if (invocation instanceof RpcInvocation) {
((RpcInvocation) invocation).setInvoker(invoker);
}
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 5c3f9a5..ee2aa61 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
@@ -139,12 +139,12 @@ public class NetUtils {
if (address == null || address.isLoopbackAddress()) {
return false;
}
+
String name = address.getHostAddress();
- boolean result = (name != null
+ return (name != null
&& IP_PATTERN.matcher(name).matches()
&& !ANYHOST_VALUE.equals(name)
&& !LOCALHOST_VALUE.equals(name));
- return result;
}
/**
@@ -186,18 +186,16 @@ public class NetUtils {
private static volatile String HOST_ADDRESS;
- public static String getHostAddress () {
+ public static String getLocalHost() {
if (HOST_ADDRESS != null) {
return HOST_ADDRESS;
}
- HOST_ADDRESS = getLocalHost();
- return HOST_ADDRESS;
- }
-
- public static String getLocalHost() {
InetAddress address = getLocalAddress();
- return address == null ? LOCALHOST_VALUE : address.getHostAddress();
+ if (address != null) {
+ return HOST_ADDRESS = address.getHostAddress();
+ }
+ return LOCALHOST_VALUE;
}
public static String filterLocalHost(String host) {
@@ -287,7 +285,7 @@ public class NetUtils {
Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
if (addressOp.isPresent()) {
try {
- if(addressOp.get().isReachable(100)){
+ if (addressOp.get().isReachable(100)) {
return addressOp.get();
}
} catch (IOException e) {
@@ -387,7 +385,7 @@ public class NetUtils {
InetAddress address = (InetAddress) addresses.nextElement();
if (preferIpv6 && address instanceof Inet6Address) {
try {
- if(address.isReachable(100)){
+ if (address.isReachable(100)) {
multicastSocket.setInterface(address);
interfaceSet = true;
break;
@@ -397,7 +395,7 @@ public class NetUtils {
}
} else if (!preferIpv6 && address instanceof Inet4Address) {
try {
- if(address.isReachable(100)){
+ if (address.isReachable(100)) {
multicastSocket.setInterface(address);
interfaceSet = true;
break;
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
index 5def6c7..f690ef7 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
@@ -300,4 +300,11 @@ public class NetUtilsTest {
assertFalse(NetUtils.matchIpRange("192.168.1.1-61:90", "192.168.1.62", 90));
assertFalse(NetUtils.matchIpRange("192.168.1.62:90", "192.168.1.63", 90));
}
+
+ @Test
+ public void testLocalHost() {
+ assertEquals(NetUtils.getLocalHost(), NetUtils.getLocalAddress().getHostAddress());
+ assertTrue(NetUtils.isValidLocalHost(NetUtils.getLocalHost()));
+ assertFalse(NetUtils.isInvalidLocalHost(NetUtils.getLocalHost()));
+ }
}