Skip to content

Commit

Permalink
DUBBO-501 随机端口改为在缺省端口的基础上增长
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfei0201 committed Jul 18, 2012
1 parent 17243e7 commit bf0eee9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ public static int getAvailablePort() {
}
}
}
}

public static int getAvailablePort(int port) {
if (port <= 0) {
return getAvailablePort();
}
for(int i = port; i < MAX_PORT; i ++) {
ServerSocket ss = null;
try {
ss = new ServerSocket(i);
return ss.getLocalPort();
} catch (IOException e) {
// continue
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
}
return port;
}

private static final int MIN_PORT = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,14 @@ private void doExportUrls() {
if (provider != null && (port == null || port == 0)) {
port = provider.getPort();
}
int defaultPort = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name).getDefaultPort();
if (port == null || port == 0) {
port = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name).getDefaultPort();
port = defaultPort;
}
if (port == null || port <= 0) {
port = getRandomPort(name);
if (port == null || port < 0) {
port = NetUtils.getAvailablePort();
port = NetUtils.getAvailablePort(defaultPort);
putRandomPort(name, port);
}
logger.warn("Use random available port(" + port + ") for protocol " + name);
Expand Down

0 comments on commit bf0eee9

Please sign in to comment.