You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/06/30 03:24:57 UTC

[GitHub] liubao68 closed pull request #784: [SCB-693] registry microservice failed if failed to get host address

liubao68 closed pull request #784: [SCB-693] registry microservice failed if failed to get host address
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/784
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/NetUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/NetUtils.java
index 8cf04d690..5c5175d14 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/NetUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/net/NetUtils.java
@@ -55,6 +55,10 @@
   private static String hostAddress;
 
   static {
+    doGetHostNameAndHostAddress();
+  }
+
+  private static void doGetHostNameAndHostAddress() {
     try {
       doGetIpv4AddressFromNetworkInterface();
       // getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming
@@ -189,10 +193,20 @@ public static String getRealListenAddress(String schema, String address) {
   }
 
   public static String getHostName() {
+    //If failed to get host name ,micro-service will registry failed
+    //So I add retry mechanism
+    if(hostName == null) {
+      doGetHostNameAndHostAddress();
+    }
     return hostName;
   }
 
   public static String getHostAddress() {
+    //If failed to get host address ,micro-service will registry failed
+    //So I add retry mechanism
+    if(hostAddress == null) {
+      doGetHostNameAndHostAddress();
+    }
     return hostAddress;
   }
 
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestNetUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestNetUtils.java
index 85eebe27a..8766b847e 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestNetUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/net/TestNetUtils.java
@@ -18,6 +18,7 @@
 package org.apache.servicecomb.foundation.common.net;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.util.HashMap;
@@ -107,4 +108,18 @@ public void testCanTcpListenYes() throws IOException {
 
     Assert.assertTrue(NetUtils.canTcpListen(address, port));
   }
+  
+  @Test
+  public void testGetHostName() {
+    Assert.assertNotEquals(null, NetUtils.getHostName());
+    Deencapsulation.setField(NetUtils.class, "hostName", null);
+    Assert.assertNotEquals(null, NetUtils.getHostName());
+  }
+
+  @Test
+  public void testGetHostAddress() {
+    Assert.assertNotEquals(null, NetUtils.getHostAddress());
+    Deencapsulation.setField(NetUtils.class, "hostAddress", null);
+    Assert.assertNotEquals(null, NetUtils.getHostAddress());
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services