Skip to content

Commit

Permalink
Modify the behavior of getting the port when initializing (opengoofy#…
Browse files Browse the repository at this point in the history
…1029)

* fix : The port is obtained from the configuration file first. If the value is null, the default value is 8080. If the value is 0, it means that the random value is defined and the port is obtained using webServer

* fix : supplementary modification

* fix : unified the project Environment information and changed the environment to ConfigurableEnvironment
  • Loading branch information
pizihao authored Dec 9, 2022
1 parent 21f5a12 commit 97409b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import cn.hippo4j.core.toolkit.inet.InetUtils;
import lombok.NoArgsConstructor;
import org.springframework.boot.web.server.WebServer;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Arrays;
import java.util.Objects;
Expand Down Expand Up @@ -54,6 +55,16 @@ public class WebIpAndPortHolder {

protected static final String SEPARATOR = ",";

/**
* get port for Environment
*/
protected static final String PORT_KEY = "server.port";

/**
* if port is null, use this
*/
protected static final int PORT = 8080;

protected static void initIpAndPort() {
if (!supportVersion) {
return;
Expand All @@ -66,12 +77,20 @@ private static WebIpAndPortInfo getWebIpAndPortInfo() {
InetUtils.HostInfo loopBackHostInfo = inetUtils.findFirstNonLoopBackHostInfo();
Assert.notNull(loopBackHostInfo, "Unable to get the application IP address");
String ip = loopBackHostInfo.getIpAddress();
WebThreadPoolHandlerChoose webThreadPoolHandlerChoose = ApplicationContextHolder.getBean(WebThreadPoolHandlerChoose.class);
WebThreadPoolService webThreadPoolService = webThreadPoolHandlerChoose.choose();
// When get the port at startup, can get the message: "port xxx was already in use" or use two ports
WebServer webServer = webThreadPoolService.getWebServer();
String port = String.valueOf(webServer.getPort());
return new WebIpAndPortInfo(ip, port);

ConfigurableEnvironment environment = ApplicationContextHolder.getBean(ConfigurableEnvironment.class);
Integer port = environment.getProperty(PORT_KEY, Integer.TYPE);
port = Objects.isNull(port) ? PORT : port;

if (port == 0) {
WebThreadPoolHandlerChoose webThreadPoolHandlerChoose = ApplicationContextHolder.getBean(WebThreadPoolHandlerChoose.class);
WebThreadPoolService webThreadPoolService = webThreadPoolHandlerChoose.choose();
// When get the port at startup, can get the message: "port xxx was already in use" or use two ports
WebServer webServer = webThreadPoolService.getWebServer();
port = webServer.getPort();
}

return new WebIpAndPortInfo(ip, String.valueOf(port));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public synchronized void bind() throws IOException {
}
Assert.assertTrue(support.isActive());
support.close();
ThreadUtil.sleep(1000L);
Assert.assertFalse(support.isActive());
}

}

0 comments on commit 97409b9

Please sign in to comment.