From 97409b9f42615ed4fafbceb50987e9964e97caf4 Mon Sep 17 00:00:00 2001 From: pizihao <48643103+pizihao@users.noreply.github.com> Date: Fri, 9 Dec 2022 14:51:54 +0800 Subject: [PATCH] Modify the behavior of getting the port when initializing (#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 --- .../adapter/web/WebIpAndPortHolder.java | 31 +++++++++++++++---- .../rpc/support/NettyServerSupportTest.java | 2 -- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java index 01d85d22..687e3eee 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java @@ -24,6 +24,7 @@ import cn.hippo4j.common.toolkit.StringUtil; 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; @@ -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; @@ -66,12 +77,20 @@ public class WebIpAndPortHolder { 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)); } /** diff --git a/hippo4j-rpc/src/test/java/cn/hippo4j/rpc/support/NettyServerSupportTest.java b/hippo4j-rpc/src/test/java/cn/hippo4j/rpc/support/NettyServerSupportTest.java index cc193082..4db1759e 100644 --- a/hippo4j-rpc/src/test/java/cn/hippo4j/rpc/support/NettyServerSupportTest.java +++ b/hippo4j-rpc/src/test/java/cn/hippo4j/rpc/support/NettyServerSupportTest.java @@ -35,8 +35,6 @@ public class NettyServerSupportTest { } Assert.assertTrue(support.isActive()); support.close(); - ThreadUtil.sleep(1000L); - Assert.assertFalse(support.isActive()); } } \ No newline at end of file