You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/08/15 03:19:10 UTC
[GitHub] chickenlj closed pull request #2278: Fix #1343 A port conflict
problem of dubbo provider when multiple services start concurrently. team6
chickenlj closed pull request #2278: Fix #1343 A port conflict problem of dubbo provider when multiple services start concurrently. team6
URL: https://github.com/apache/incubator-dubbo/pull/2278
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/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 70d6006cbe..3650c29a11 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
@@ -21,11 +21,12 @@
import org.apache.dubbo.common.logger.LoggerFactory;
import java.io.IOException;
-import java.net.Inet6Address;
import java.net.InetAddress;
+import java.net.Inet6Address;
import java.net.InetSocketAddress;
-import java.net.NetworkInterface;
import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Map;
@@ -75,21 +76,26 @@ public static int getAvailablePort() {
}
}
- public static int getAvailablePort(int port) {
+ public static int getAvailablePort(String host, int port) {
if (port <= 0) {
return getAvailablePort();
}
+ InetAddress inetAddress = null;
+ Socket socket = null;
+ try {
+ inetAddress = InetAddress.getByName(host);
+ } catch (UnknownHostException e) {
+ return port;
+ }
for (int i = port; i < MAX_PORT; i++) {
- ServerSocket ss = null;
try {
- ss = new ServerSocket(i);
- return i;
+ socket = new Socket(inetAddress, i);
} catch (IOException e) {
- // continue
+ return i;
} finally {
- if (ss != null) {
+ if (socket != null) {
try {
- ss.close();
+ socket.close();
} catch (IOException e) {
}
}
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 caa216f930..ff240bfb08 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
@@ -47,8 +47,8 @@ public void testGetRandomPort() throws Exception {
@Test
public void testGetAvailablePort() throws Exception {
assertThat(NetUtils.getAvailablePort(), greaterThan(0));
- assertThat(NetUtils.getAvailablePort(12345), greaterThanOrEqualTo(12345));
- assertThat(NetUtils.getAvailablePort(-1), greaterThanOrEqualTo(30000));
+ assertThat(NetUtils.getAvailablePort("localhost",12345), greaterThanOrEqualTo(12345));
+ assertThat(NetUtils.getAvailablePort("localhost",-1), greaterThanOrEqualTo(30000));
}
@Test
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index e9d5e6b567..b2c6902af2 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -355,7 +355,21 @@ public synchronized void unexport() {
private void doExportUrls() {
List<URL> registryURLs = loadRegistries(true);
for (ProtocolConfig protocolConfig : protocols) {
- doExportUrlsFor1Protocol(protocolConfig, registryURLs);
+ while (true) {
+ try {
+ doExportUrlsFor1Protocol(protocolConfig, registryURLs);
+ break;
+ } catch (Exception e) {
+ if (e.getMessage().contains("Address already in use")) {
+ RANDOM_PORT_MAP.remove(protocolConfig.getName() != null ? protocolConfig.getName().toLowerCase() : "dubbo");
+ try {
+ Thread.sleep(500);
+ } catch (Exception e1) {}
+ } else {
+ throw e;
+ }
+ }
+ }
}
}
@@ -468,7 +482,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
}
String host = this.findConfigedHosts(protocolConfig, registryURLs, map);
- Integer port = this.findConfigedPorts(protocolConfig, name, map);
+ Integer port = this.findConfigedPorts(protocolConfig, name, host, map);
URL url = new URL(name, host, port, (contextPath == null || contextPath.length() == 0 ? "" : contextPath + "/") + path, map);
if (ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
@@ -632,7 +646,7 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> regist
* @param name
* @return
*/
- private Integer findConfigedPorts(ProtocolConfig protocolConfig, String name, Map<String, String> map) {
+ private Integer findConfigedPorts(ProtocolConfig protocolConfig, String name, String host, Map<String, String> map) {
Integer portToBind = null;
// parse bind port from environment
@@ -652,7 +666,7 @@ private Integer findConfigedPorts(ProtocolConfig protocolConfig, String name, Ma
if (portToBind == null || portToBind <= 0) {
portToBind = getRandomPort(name);
if (portToBind == null || portToBind < 0) {
- portToBind = getAvailablePort(defaultPort);
+ portToBind = getAvailablePort(host, defaultPort);
putRandomPort(name, portToBind);
}
logger.warn("Use random available port(" + portToBind + ") for protocol " + name);
----------------------------------------------------------------
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
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org