From 20d12371a9599ac90742d4d9c943c5a581327c08 Mon Sep 17 00:00:00 2001 From: pizihao <2335715300@qq.com> Date: Fri, 2 Sep 2022 18:07:27 +0800 Subject: [PATCH] fix: The IP and port of the runtime Web (#614) --- .../config/AdapterExecutorProperties.java | 2 +- .../starter/config/ExecutorProperties.java | 2 +- .../config/WebThreadPoolProperties.java | 2 +- .../event/AbstractRefreshListener.java | 100 +++++++++++++----- .../AdapterExecutorsRefreshListener.java | 2 +- .../DynamicThreadPoolRefreshListener.java | 2 +- 6 files changed, 79 insertions(+), 31 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/AdapterExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/AdapterExecutorProperties.java index 1ddf5f48..c70d7328 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/AdapterExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/AdapterExecutorProperties.java @@ -46,7 +46,7 @@ public class AdapterExecutorProperties { private Integer maximumPoolSize; /** - * nodes + * nodes,application startup is not affect,change properties is effect */ private String nodes; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java index c5d78b4b..ccf8aa0d 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ExecutorProperties.java @@ -104,7 +104,7 @@ public class ExecutorProperties { private DynamicThreadPoolNotifyProperties notify; /** - * nodes + * nodes,application startup is not affect,change properties is effect */ private String nodes; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java index 68ce9f3f..941f876b 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/WebThreadPoolProperties.java @@ -41,7 +41,7 @@ public class WebThreadPoolProperties { private Integer keepAliveTime; /** - * nodes + * nodes,application startup is not affect,change properties is effect */ private String nodes; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java index e5025b4d..b73bac01 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java @@ -23,7 +23,8 @@ import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; import lombok.Data; import lombok.extern.log4j.Log4j2; -import org.springframework.core.env.Environment; +import org.springframework.boot.web.context.WebServerInitializedEvent; +import org.springframework.context.event.EventListener; import java.util.Arrays; import java.util.Objects; @@ -34,39 +35,40 @@ import java.util.Objects; @Log4j2 public abstract class AbstractRefreshListener implements RefreshListener { - protected static final String PORT_PROPERTY = "local.server.port"; - protected static final String ALL = "*"; - /** - * separator - */ + protected static final String SPOT = "\\."; + protected static final String SEPARATOR = ","; + protected static final String COLON = ":"; + /** * application ip */ - protected final String ipAddress; + protected final String[] ipSegment; /** * application post */ - protected final String port; + protected String port; AbstractRefreshListener() { InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); InetUtils.HostInfo loopbackHostInfo = inetUtils.findFirstNonLoopbackHostInfo(); - Assert.isNull(loopbackHostInfo, "Unable to get the application IP address"); - ipAddress = loopbackHostInfo.getIpAddress(); - Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); - Assert.isTrue(environment.containsProperty(PORT_PROPERTY), "Unable to get the application port"); - port = environment.getProperty(PORT_PROPERTY); + Assert.notNull(loopbackHostInfo, "Unable to get the application IP address"); + ipSegment = loopbackHostInfo.getIpAddress().split(SPOT); + } + + @EventListener(WebServerInitializedEvent.class) + public void webServerInitializedListener(WebServerInitializedEvent event) { + port = String.valueOf(event.getWebServer().getPort()); } /** * Matching nodes
* nodes is ip + port.Get 'nodes' in the new Properties,Compare this with the ip + port of Application.
- * Support prefix pattern matching.e.g:
+ * support prefix pattern matching. e.g:
*