From a7f2bb7521d91c7ea0d7a23b430220ba31249237 Mon Sep 17 00:00:00 2001 From: pizihao <48643103+pizihao@users.noreply.github.com> Date: Fri, 23 Sep 2022 21:49:18 +0800 Subject: [PATCH] =?UTF-8?q?Adapter=20=E5=88=9D=E5=A7=8B=E5=8C=96=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E6=A0=B8=E5=BF=83=E5=8F=82=E6=95=B0=20(#716)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : Unify get the IP and port of the current instance(#609) * feat : add enable prop for this(#609) * feat : add ThreadPoolInitRefresh for init(#609) * feat : adapter thread pool init, including nacos, zookeeper, etcd and apollo(#609) --- .../adapter/web/WebIpAndPortHolder.java | 97 +++++++++++++++++++ .../common/api/ThreadPoolInitRefresh.java | 51 ++++++++++ .../config/AdapterExecutorProperties.java | 5 + .../config/WebThreadPoolProperties.java | 5 + ...bstractConfigThreadPoolDynamicRefresh.java | 12 ++- .../refresher/ApolloRefresherHandler.java | 10 ++ .../refresher/EtcdRefresherHandler.java | 46 ++++++--- .../refresher/NacosCloudRefresherHandler.java | 17 +++- .../refresher/NacosRefresherHandler.java | 15 ++- .../refresher/ZookeeperRefresherHandler.java | 40 +++++++- .../event/AbstractRefreshListener.java | 57 +---------- .../AdapterExecutorsRefreshListener.java | 2 +- .../event/WebExecutorRefreshListener.java | 2 +- 13 files changed, 281 insertions(+), 78 deletions(-) create mode 100644 hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/api/ThreadPoolInitRefresh.java 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 new file mode 100644 index 00000000..c35f90af --- /dev/null +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.adapter.web; + +import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.model.WebIpAndPortInfo; +import cn.hippo4j.common.toolkit.Assert; +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.core.toolkit.inet.InetUtils; +import org.springframework.boot.web.server.WebServer; + +import java.util.Arrays; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Ip and port Holder + */ +public class WebIpAndPortHolder { + + /** + * Application ip and application post + */ + protected static AtomicReference webIpAndPort = new AtomicReference<>(); + + public static final String ALL = "*"; + + protected static final String SEPARATOR = ","; + + private WebIpAndPortHolder() { + + } + + protected static void initIpAndPort() { + webIpAndPort.compareAndSet(null, getWebIpAndPortInfo()); + } + + private static WebIpAndPortInfo getWebIpAndPortInfo() { + InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); + 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); + } + + /** + * get WebIpAndPortInfo, If it is null, initialize it + * + * @return WebIpAndPortInfo + */ + public static WebIpAndPortInfo getWebIpAndPort() { + if (webIpAndPort.get() == null) { + initIpAndPort(); + } + return WebIpAndPortHolder.webIpAndPort.get(); + } + + /** + * Check the new properties and instance IP and port + * + * @param nodes nodes in properties + * @return Whether it meets the conditions + */ + public static boolean check(String nodes) { + WebIpAndPortInfo webIpAndPort = WebIpAndPortHolder.getWebIpAndPort(); + if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) { + return true; + } + String[] splitNodes = nodes.split(SEPARATOR); + return Arrays.stream(splitNodes) + .distinct() + .map(WebIpAndPortInfo::build) + .filter(Objects::nonNull) + .anyMatch(each -> each.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort())); + } + +} \ No newline at end of file diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/ThreadPoolInitRefresh.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ThreadPoolInitRefresh.java new file mode 100644 index 00000000..e3b752ea --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/ThreadPoolInitRefresh.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.api; + +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; + +/** + * when init thread-pool dynamic refresh. + */ +public interface ThreadPoolInitRefresh extends ApplicationRunner { + + /** + * Initializes the thread pool after system startup + * + * @param context new properties + */ + void initRefresh(String context); + + /** + * get from the Configuration center + * + * @return new properties + * @throws Exception exception + */ + String getProperties() throws Exception; + + @Override + default void run(ApplicationArguments args) throws Exception { + String properties = getProperties(); + if (properties == null) { + return; + } + initRefresh(properties); + } +} 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 6ff8c9e0..c46d8934 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 @@ -49,4 +49,9 @@ public class AdapterExecutorProperties { * Nodes, application startup is not affect, change properties is effect */ private String nodes; + + /** + * these propertied is enabled? + */ + private Boolean enable = true; } 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 b7cb737c..f2d1ff02 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 @@ -44,4 +44,9 @@ public class WebThreadPoolProperties { * Nodes, application startup is not affect, change properties is effect */ private String nodes; + + /** + * these propertied is enabled? + */ + private Boolean enable = true; } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java index bf545487..ba5933fc 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java @@ -18,6 +18,7 @@ package cn.hippo4j.config.springboot.starter.refresher; import cn.hippo4j.common.api.ThreadPoolDynamicRefresh; +import cn.hippo4j.common.api.ThreadPoolInitRefresh; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; @@ -37,7 +38,11 @@ import java.util.concurrent.ExecutorService; */ @Slf4j @RequiredArgsConstructor -public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean { +public abstract class AbstractConfigThreadPoolDynamicRefresh + implements + ThreadPoolDynamicRefresh, + ThreadPoolInitRefresh, + InitializingBean { protected final BootstrapConfigProperties bootstrapConfigProperties; @@ -47,6 +52,11 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPo bootstrapConfigProperties = ApplicationContextHolder.getBean(BootstrapConfigProperties.class); } + @Override + public void initRefresh(String context) { + dynamicRefresh(context); + } + @Override public void dynamicRefresh(String configContent) { dynamicRefresh(configContent, null); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ApolloRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ApolloRefresherHandler.java index bc4ce2e2..90425870 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ApolloRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ApolloRefresherHandler.java @@ -41,6 +41,16 @@ public class ApolloRefresherHandler extends AbstractConfigThreadPoolDynamicRefre @Value(APOLLO_PROPERTY) private String namespace; + @Override + public String getProperties() { + String[] apolloNamespaces = this.namespace.split(","); + this.namespace = apolloNamespaces[0]; + String copyNamespace = this.namespace.replaceAll("." + bootstrapConfigProperties.getConfigFileType().getValue(), ""); + ConfigFileFormat configFileFormat = ConfigFileFormat.fromString(bootstrapConfigProperties.getConfigFileType().getValue()); + ConfigFile configFile = ConfigService.getConfigFile(copyNamespace, configFileFormat); + return configFile.getContent(); + } + @Override public void afterPropertiesSet() { String[] apolloNamespaces = this.namespace.split(","); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java index 30df2d14..5f0cbe25 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/EtcdRefresherHandler.java @@ -52,22 +52,25 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh private static final String KEY = "key"; + @Override + public String getProperties() throws Exception { + Map etcd = bootstrapConfigProperties.getEtcd(); + Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); + initClient(etcd, charset); + + String key = etcd.get(KEY); + GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); + KeyValue keyValue = getResponse.getKvs().get(0); + return Objects.isNull(keyValue) ? null : keyValue.getValue().toString(charset); + } + @Override public void afterPropertiesSet() throws Exception { Map etcd = bootstrapConfigProperties.getEtcd(); - String user = etcd.get(USER); - String password = etcd.get(PASSWORD); - String endpoints = etcd.get(ENDPOINTS); - String authority = etcd.get(AUTHORITY); String key = etcd.get(KEY); Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); - ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); - // todo - if (Objects.isNull(client)) { - client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) - .password(ByteSequence.from(password, charset)).authority(authority) - .build() : clientBuilder.build(); - } + initClient(etcd, charset); + // todo Currently only supports json GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); KeyValue keyValue = getResponse.getKvs().get(0); @@ -100,4 +103,25 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh } }); } + + /** + * if client is null, init it + * + * @param etcd etcd configuration item + * @param charset charset + */ + private void initClient(Map etcd, Charset charset) { + // todo + if (Objects.isNull(client)) { + String user = etcd.get(USER); + String password = etcd.get(PASSWORD); + String authority = etcd.get(AUTHORITY); + String endpoints = etcd.get(ENDPOINTS); + ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); + client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) + .password(ByteSequence.from(password, charset)).authority(authority) + .build() : clientBuilder.build(); + } + } + } \ No newline at end of file diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosCloudRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosCloudRefresherHandler.java index 4433cfa4..6f328b1f 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosCloudRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosCloudRefresherHandler.java @@ -31,17 +31,28 @@ import java.util.concurrent.Executor; @Slf4j public class NacosCloudRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { + static final String DATA_ID = "data-id"; + static final String GROUP = "group"; + private final NacosConfigManager nacosConfigManager; public NacosCloudRefresherHandler() { nacosConfigManager = ApplicationContextHolder.getBean(NacosConfigManager.class); } + @Override + public String getProperties() throws Exception { + Map nacosConfig = bootstrapConfigProperties.getNacos(); + String dataId = nacosConfig.get(DATA_ID); + String group = nacosConfig.get(GROUP); + return nacosConfigManager.getConfigService().getConfig(dataId, group, 5000L); + } + @Override public void afterPropertiesSet() throws Exception { Map nacosConfig = bootstrapConfigProperties.getNacos(); - nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), - nacosConfig.get("group"), new Listener() { + nacosConfigManager.getConfigService().addListener(nacosConfig.get(DATA_ID), + nacosConfig.get(GROUP), new Listener() { @Override public Executor getExecutor() { @@ -53,6 +64,6 @@ public class NacosCloudRefresherHandler extends AbstractConfigThreadPoolDynamicR dynamicRefresh(configInfo); } }); - log.info("Dynamic thread pool refresher, add nacos cloud listener success. data-id: {}, group: {}", nacosConfig.get("data-id"), nacosConfig.get("group")); + log.info("Dynamic thread pool refresher, add nacos cloud listener success. data-id: {}, group: {}", nacosConfig.get(DATA_ID), nacosConfig.get(GROUP)); } } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java index fa5f7e91..ad052018 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java @@ -32,6 +32,9 @@ import java.util.concurrent.Executor; @Slf4j public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { + static final String DATA_ID = "data-id"; + static final String GROUP = "group"; + @NacosInjected private ConfigService configService; @@ -39,11 +42,19 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres super(bootstrapConfigProperties); } + @Override + public String getProperties() throws Exception { + Map nacosConfig = bootstrapConfigProperties.getNacos(); + String dataId = nacosConfig.get(DATA_ID); + String group = nacosConfig.get(GROUP); + return configService.getConfig(dataId, group, Long.MAX_VALUE); + } + @Override public void afterPropertiesSet() throws Exception { Map nacosConfig = bootstrapConfigProperties.getNacos(); - configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), + configService.addListener(nacosConfig.get(DATA_ID), nacosConfig.get(GROUP), new Listener() { @Override @@ -56,6 +67,6 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres dynamicRefresh(configInfo); } }); - log.info("Dynamic thread pool refresher, add nacos listener success. data-id: {}, group: {}", nacosConfig.get("data-id"), nacosConfig.get("group")); + log.info("Dynamic thread pool refresher, add nacos listener success. data-id: {}, group: {}", nacosConfig.get(DATA_ID), nacosConfig.get(GROUP)); } } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java index 479bba79..fe886001 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ZookeeperRefresherHandler.java @@ -41,15 +41,31 @@ import java.util.Map; @Slf4j public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { + static final String ZK_CONNECT_STR = "zk-connect-str"; + + static final String ROOT_NODE = "root-node"; + + static final String CONFIG_VERSION = "config-version"; + + static final String NODE = "node"; + private CuratorFramework curatorFramework; + @Override + public String getProperties() { + Map zkConfigs = bootstrapConfigProperties.getZookeeper(); + String nodePath = ZKPaths.makePath(ZKPaths.makePath(zkConfigs.get(ROOT_NODE), + zkConfigs.get(CONFIG_VERSION)), zkConfigs.get(NODE)); + return nodePathResolver(nodePath); + } + @Override public void afterPropertiesSet() { Map zkConfigs = bootstrapConfigProperties.getZookeeper(); - curatorFramework = CuratorFrameworkFactory.newClient(zkConfigs.get("zk-connect-str"), + curatorFramework = CuratorFrameworkFactory.newClient(zkConfigs.get(ZK_CONNECT_STR), new ExponentialBackoffRetry(1000, 3)); - String nodePath = ZKPaths.makePath(ZKPaths.makePath(zkConfigs.get("root-node"), - zkConfigs.get("config-version")), zkConfigs.get("node")); + String nodePath = ZKPaths.makePath(ZKPaths.makePath(zkConfigs.get(ROOT_NODE), + zkConfigs.get(CONFIG_VERSION)), zkConfigs.get(NODE)); final ConnectionStateListener connectionStateListener = (client, newState) -> { if (newState == ConnectionState.CONNECTED) { loadNode(nodePath); @@ -81,6 +97,20 @@ public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRe * @param nodePath zk config node path. */ public void loadNode(String nodePath) { + String content = nodePathResolver(nodePath); + if (content != null) { + dynamicRefresh(content); + registerNotifyAlarmManage(); + } + } + + /** + * resolver for zk config + * + * @param nodePath zk config node path + * @return resolver result + */ + private String nodePathResolver(String nodePath) { try { final GetChildrenBuilder childrenBuilder = curatorFramework.getChildren(); final List children = childrenBuilder.watched().forPath(nodePath); @@ -97,10 +127,10 @@ public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRe } content.append(nodeName).append("=").append(value).append("\n"); }); - dynamicRefresh(content.toString()); - registerNotifyAlarmManage(); + return content.toString(); } catch (Exception ex) { log.error("Load zookeeper node error, nodePath is: {}", nodePath, ex); + return null; } } 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 a630db60..61d6fe08 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 @@ -17,55 +17,15 @@ package cn.hippo4j.config.springboot.starter.refresher.event; -import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; -import cn.hippo4j.adapter.web.WebThreadPoolService; -import cn.hippo4j.common.config.ApplicationContextHolder; -import cn.hippo4j.common.model.WebIpAndPortInfo; -import cn.hippo4j.common.toolkit.Assert; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.core.toolkit.inet.InetUtils; +import cn.hippo4j.adapter.web.WebIpAndPortHolder; import lombok.extern.slf4j.Slf4j; -import java.util.Arrays; -import java.util.Objects; - /** * Refresh listener abstract base class. */ @Slf4j public abstract class AbstractRefreshListener implements RefreshListener { - protected static final String ALL = "*"; - - protected static final String SEPARATOR = ","; - - /** - * Application ip and application post - */ - protected static volatile WebIpAndPortInfo webIpAndPort; - - protected void initIpAndPort() { - if (webIpAndPort == null) { - synchronized (AbstractRefreshListener.class) { - if (webIpAndPort == null) { - webIpAndPort = getWebIpAndPortInfo(); - } - } - } - } - - private WebIpAndPortInfo getWebIpAndPortInfo() { - InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); - 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 - String port = String.valueOf(webThreadPoolService.getWebServer().getPort()); - return new WebIpAndPortInfo(ip, port); - } - /** * Matching nodes
* nodes is ip + port.Get 'nodes' in the new Properties,Compare this with the ip + port of Application.
@@ -82,19 +42,8 @@ public abstract class AbstractRefreshListener implements RefreshListener each.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort())); + return WebIpAndPortHolder.check(nodes); } /** @@ -104,6 +53,6 @@ public abstract class AbstractRefreshListener implements RefreshListener