diff --git a/README.md b/README.md index e0247957..6bd693b8 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Hippo-4J 通过对 JDK 线程池增强,以及扩展三方框架底层线程池 - 通知报警 - 内置四种报警通知策略,线程池活跃度、容量水位、拒绝策略以及任务执行时间超长; - 运行监控 - 实时查看线程池运行时数据,最近半小时线程池运行数据图表展示; - 功能扩展 - 支持线程池任务传递上下文;项目关闭时,支持等待线程池在指定时间内完成任务; -- 多种模式 - 内置两种使用模式:[依赖配置中心](https://hippo4j.cn/docs/user_docs/getting-started/config/hippo4j-core-start) 和 [无中间件依赖](https://hippo4j.cn/docs/user_docs/getting-started/server/hippo4j-server-start); +- 多种模式 - 内置两种使用模式:[依赖配置中心](https://hippo4j.cn/docs/user_docs/getting_started/config/hippo4j-config-start) 和 [无中间件依赖](https://hippo4j.cn/docs/user_docs/getting_started/server/hippo4j-server-start); - 容器管理 - Tomcat、Jetty、Undertow 容器线程池运行时查看和线程数变更; - 中间件适配 - Apache RocketMQ、Dubbo、RabbitMQ、Hystrix 消费线程池运行时数据查看和线程数变更。 @@ -40,7 +40,7 @@ Hippo-4J 通过对 JDK 线程池增强,以及扩展三方框架底层线程池 ## 快速开始 -对于本地演示目的,请参阅 [Quick start](https://hippo4j.cn/docs/user_docs/getting-started/hippo4j-server-start) +对于本地演示目的,请参阅 [Quick start](https://hippo4j.cn/docs/user_docs/user_guide/quick-start) 演示环境: - http://console.hippo4j.cn/index.html diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java index 69600265..771d6699 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/AbstractWebThreadPoolService.java @@ -51,15 +51,20 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi if (executor == null) { synchronized (AbstractWebThreadPoolService.class) { if (executor == null) { - ApplicationContext applicationContext = ApplicationContextHolder.getInstance(); - WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); - executor = getWebThreadPoolByServer(webServer); + executor = getWebThreadPoolByServer(getWebServer()); } } } return executor; } + @Override + public WebServer getWebServer() { + ApplicationContext applicationContext = ApplicationContextHolder.getInstance(); + WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); + return webServer; + } + @Override public void run(ApplicationArguments args) { try { diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java index 7bc7e2ad..ba88ed12 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebThreadPoolService.java @@ -21,6 +21,7 @@ import cn.hippo4j.common.model.ThreadPoolBaseInfo; import cn.hippo4j.common.model.ThreadPoolParameter; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import org.springframework.boot.web.server.WebServer; import java.util.concurrent.Executor; @@ -63,4 +64,13 @@ public interface WebThreadPoolService { * @param threadPoolParameterInfo */ void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo); + + /** + * Get web server. + * + * @return + */ + default WebServer getWebServer() { + return null; + } } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/function/Matcher.java b/hippo4j-common/src/main/java/cn/hippo4j/common/function/Matcher.java index 918ec3ee..a6377d2d 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/function/Matcher.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/function/Matcher.java @@ -23,5 +23,8 @@ package cn.hippo4j.common.function; @FunctionalInterface public interface Matcher { + /** + * Returns {@code true} if this matches {@code t}, {@code false} otherwise. + */ boolean match(T t); } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/function/NoArgsConsumer.java b/hippo4j-common/src/main/java/cn/hippo4j/common/function/NoArgsConsumer.java index 878691ec..cc4adc7c 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/function/NoArgsConsumer.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/function/NoArgsConsumer.java @@ -23,5 +23,8 @@ package cn.hippo4j.common.function; @FunctionalInterface public interface NoArgsConsumer { + /** + * Execute operation without parameters. + */ void accept(); } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java index 9fe6d1bb..a8cd74c9 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java @@ -17,22 +17,35 @@ package cn.hippo4j.common.toolkit; +import cn.hippo4j.common.model.ThreadPoolParameterInfo; import org.junit.Test; public class ContentUtilTest { @Test public void assertGetPoolContent() { - + String testText = "{\"tenantId\":\"prescription\",\"itemId\":\"dynamic-threadpool-example\",\"tpId\":" + + "\"message-consume\",\"queueType\":1,\"capacity\":4,\"keepAliveTime\":513,\"rejectedType\":4,\"isAlarm\"" + + ":1,\"capacityAlarm\":80,\"livenessAlarm\":80,\"allowCoreThreadTimeOut\":1}"; + ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription") + .itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1) + .maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4) + .isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build(); + Assert.isTrue(testText.equals(ContentUtil.getPoolContent(threadPoolParameterInfo))); } @Test public void assertGetGroupKey() { - + String testText = "message-consume+dynamic-threadpool-example+prescription"; + ThreadPoolParameterInfo parameter = ThreadPoolParameterInfo.builder() + .tenantId("prescription").itemId("dynamic-threadpool-example").tpId("message-consume").build(); + Assert.isTrue(testText.equals(ContentUtil.getGroupKey(parameter))); } @Test public void assertGetGroupKeys() { - + String testText = "message-consume+dynamic-threadpool-example+prescription"; + String groupKey = ContentUtil.getGroupKey("message-consume", "dynamic-threadpool-example", "prescription"); + Assert.isTrue(testText.equals(groupKey)); } -} +} \ No newline at end of file diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java index 45c2e8f9..7c0067ff 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java @@ -17,5 +17,77 @@ package cn.hippo4j.common.toolkit; +import org.junit.Test; + +import java.util.Objects; + public class StringUtilTest { + + @Test + public void assertIsEmpty() { + String string = ""; + Assert.isTrue(StringUtil.isEmpty(string)); + } + + @Test + public void assertIsNotEmpty() { + String string = "string"; + Assert.isTrue(StringUtil.isNotEmpty(string)); + } + + @Test + public void emptyToNull() { + String string = ""; + Assert.isNull(StringUtil.emptyToNull(string)); + } + + @Test + public void nullToEmpty() { + String string = "null"; + Assert.notEmpty(StringUtil.nullToEmpty(string)); + } + + @Test + public void isNullOrEmpty() { + String string = "null"; + Assert.isTrue(!StringUtil.isNullOrEmpty(string)); + } + + @Test + public void isBlank() { + String string = ""; + Assert.isTrue(StringUtil.isBlank(string)); + } + + @Test + public void isNotBlank() { + String string = "null"; + Assert.isTrue(StringUtil.isNotBlank(string)); + } + + @Test + public void isAllNotEmpty() { + String strings = "str"; + Assert.isTrue(StringUtil.isAllNotEmpty(strings)); + } + + @Test + public void hasEmpty() { + String strings = ""; + Assert.isTrue(StringUtil.hasEmpty(strings)); + } + + @Test + public void toUnderlineCase() { + String string = "str"; + String s = StringUtil.toUnderlineCase(string); + Assert.isTrue(Objects.equals(s, "str")); + } + + @Test + public void toSymbolCase() { + String string = "str"; + String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE); + Assert.isTrue(Objects.equals(s, "str")); + } } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java index 521143fb..94265d22 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/state/ThreadPoolRunStateHandler.java @@ -57,7 +57,7 @@ public class ThreadPoolRunStateHandler extends AbstractThreadPoolRuntime { ByteConvertUtil.getPrintSize(runtimeInfo.getMaxMemory())).toString(); poolRunStateInfo.setCurrentLoad(poolRunStateInfo.getCurrentLoad() + "%"); poolRunStateInfo.setPeakLoad(poolRunStateInfo.getPeakLoad() + "%"); - String ipAddress = hippo4JInetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + String ipAddress = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); poolRunStateInfo.setHost(ipAddress); poolRunStateInfo.setMemoryProportion(memoryProportion); poolRunStateInfo.setFreeMemory(ByteConvertUtil.getPrintSize(runtimeInfo.getFreeMemory())); diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java index fec00c8d..a01d4924 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/IdentifyUtil.java @@ -51,7 +51,7 @@ public class IdentifyUtil { if (StrUtil.isNotBlank(IDENTIFY)) { return IDENTIFY; } - String ip = hippo4JInetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + String ip = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); String port = environment.getProperty("server.port", "8080"); String identification = StrUtil.builder(ip, ":", diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java index cc7e5c68..46cb7e32 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/toolkit/inet/InetUtils.java @@ -58,7 +58,7 @@ public class InetUtils implements Closeable { this.executorService.shutdown(); } - public HostInfo findFirstNonLoopbackHostInfo() { + public HostInfo findFirstNonLoopBackHostInfo() { InetAddress address = findFirstNonLoopbackAddress(); if (address != null) { return convertAddress(address); diff --git a/hippo4j-server/pom.xml b/hippo4j-server/pom.xml index 35fda466..4b8ee0f2 100644 --- a/hippo4j-server/pom.xml +++ b/hippo4j-server/pom.xml @@ -14,11 +14,6 @@ - - cn.hippo4j - hippo4j-config - ${revision} - cn.hippo4j 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 f78a2539..6ff8c9e0 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 @@ -44,4 +44,9 @@ public class AdapterExecutorProperties { * Maximum pool size */ private Integer maximumPoolSize; + + /** + * 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/BootstrapConfigProperties.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java index d939e371..8a534f69 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/BootstrapConfigProperties.java @@ -20,9 +20,6 @@ package cn.hippo4j.config.springboot.starter.config; import java.util.List; import java.util.Map; -import java.util.List; -import java.util.Map; - import cn.hippo4j.core.config.BootstrapPropertiesInterface; import cn.hippo4j.config.springboot.starter.parser.ConfigFileTypeEnum; import lombok.Getter; diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java index bff13549..c77536a8 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java @@ -17,11 +17,7 @@ package cn.hippo4j.config.springboot.starter.config; -import cn.hippo4j.config.springboot.starter.refresher.ApolloRefresherHandler; -import cn.hippo4j.config.springboot.starter.refresher.NacosCloudRefresherHandler; -import cn.hippo4j.config.springboot.starter.refresher.NacosRefresherHandler; -import cn.hippo4j.config.springboot.starter.refresher.ZookeeperRefresherHandler; -import cn.hippo4j.config.springboot.starter.refresher.EtcdRefresherHandler; +import cn.hippo4j.config.springboot.starter.refresher.*; import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.nacos.api.config.ConfigService; import io.etcd.jetcd.Client; diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index b7f4d7c7..aa24eaeb 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -36,8 +36,8 @@ import cn.hippo4j.message.config.MessageConfiguration; import cn.hippo4j.message.service.AlarmControlHandler; import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; import cn.hippo4j.message.service.Hippo4jSendMessageService; +import cn.hippo4j.springboot.starter.adapter.web.EnableWebAdapter; import lombok.AllArgsConstructor; - import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -53,6 +53,7 @@ import org.springframework.core.annotation.Order; * Dynamic thread-pool core auto configuration. */ @Configuration +@EnableWebAdapter @AllArgsConstructor @ConditionalOnBean(MarkerConfiguration.Marker.class) @EnableConfigurationProperties(BootstrapConfigProperties.class) 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 71cc5f69..58754275 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 @@ -102,4 +102,9 @@ public class ExecutorProperties { * Notify */ private DynamicThreadPoolNotifyProperties notify; + + /** + * 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 437f8d8e..b7cb737c 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 @@ -39,4 +39,9 @@ public class WebThreadPoolProperties { * Keep alive time */ private Integer keepAliveTime; + + /** + * 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/AbstractCoreThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java similarity index 91% rename from hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java rename to hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java index 91defbc4..bf545487 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java @@ -37,13 +37,13 @@ import java.util.concurrent.ExecutorService; */ @Slf4j @RequiredArgsConstructor -public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean { +public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean { protected final BootstrapConfigProperties bootstrapConfigProperties; protected final ExecutorService dynamicRefreshExecutorService = ThreadPoolBuilder.builder().singlePool("client.dynamic.refresh").build(); - public AbstractCoreThreadPoolDynamicRefresh() { + public AbstractConfigThreadPoolDynamicRefresh() { bootstrapConfigProperties = ApplicationContextHolder.getBean(BootstrapConfigProperties.class); } @@ -59,7 +59,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool if (CollectionUtil.isNotEmpty(newValueChangeMap)) { Optional.ofNullable(configInfo).ifPresent(each -> each.putAll(newValueChangeMap)); } - BootstrapConfigProperties bindableCoreProperties = BootstrapCorePropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties); + BootstrapConfigProperties bindableCoreProperties = BootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties); ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, bindableCoreProperties)); } catch (Exception ex) { log.error("Hippo-4J core dynamic refresh failed.", ex); 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 4c372684..bc4ce2e2 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 @@ -34,7 +34,7 @@ import java.util.Map; * Apollo refresher handler. */ @Slf4j -public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { +public class ApolloRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { private static final String APOLLO_PROPERTY = "${spring.dynamic.thread-pool.apollo.namespace:application}"; diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapCorePropertiesBinderAdapt.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java similarity index 99% rename from hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapCorePropertiesBinderAdapt.java rename to hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java index e8ab3d83..433c6535 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapCorePropertiesBinderAdapt.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java @@ -38,7 +38,7 @@ import java.util.Map; /** * Bootstrap core properties binder adapt. */ -public class BootstrapCorePropertiesBinderAdapt { +public class BootstrapConfigPropertiesBinderAdapt { /** * Bootstrap core properties binder. 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 a8ef886f..30df2d14 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 @@ -36,7 +36,7 @@ import java.util.Objects; * @description: */ @Slf4j -public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { +public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { private Client client; 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 4a473988..4433cfa4 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 @@ -29,7 +29,7 @@ import java.util.concurrent.Executor; * Nacos cloud refresher handler. */ @Slf4j -public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { +public class NacosCloudRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { private final NacosConfigManager nacosConfigManager; 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 baf0616b..fa5f7e91 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 @@ -30,7 +30,7 @@ import java.util.concurrent.Executor; * Nacos refresher handler. */ @Slf4j -public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { +public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { @NacosInjected private ConfigService configService; 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 2cbe8100..479bba79 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 @@ -39,7 +39,7 @@ import java.util.Map; * Zookeeper refresher handler. */ @Slf4j -public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { +public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh { private CuratorFramework curatorFramework; 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 new file mode 100644 index 00000000..a42793a5 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AbstractRefreshListener.java @@ -0,0 +1,181 @@ +/* + * 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.config.springboot.starter.refresher.event; + +import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.toolkit.Assert; +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.core.toolkit.inet.InetUtils; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.web.context.WebServerInitializedEvent; +import org.springframework.context.event.EventListener; + +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 SPOT = "\\."; + + protected static final String SEPARATOR = ","; + + protected static final String COLON = ":"; + + /** + * application ip + */ + protected final String[] ipSegment; + + /** + * application post + */ + protected String port; + + AbstractRefreshListener() { + InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); + InetUtils.HostInfo loopBackHostInfo = inetUtils.findFirstNonLoopBackHostInfo(); + 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:
+ *
    + *
  • 192.168.1.5:* -- Matches all ports of 192.168.1.5
  • + *
  • 192.168.1.*:2009 -- Matches 2009 port of 192.168.1.*
  • + *
  • * -- all
  • + *
  • empty -- all
  • + *
+ * The format of ip + port is ip : port. + * + * @param properties new Properties + */ + @Override + public boolean match(M properties) { + String nodes = getNodes(properties); + if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) { + return true; + } + String[] splitNodes = nodes.split(SEPARATOR); + return Arrays.stream(splitNodes) + .distinct() + .map(IpAndPort::build) + .filter(Objects::nonNull) + .anyMatch(i -> i.check(ipSegment, port)); + } + + /** + * get nodes in new properties + * + * @param properties new properties + * @return nodes in properties + */ + protected String getNodes(M properties) { + return ALL; + } + + /** + * ip + port + */ + @Data + protected static class IpAndPort { + + private String ip; + private String port; + private String[] propIpSegment; + + private IpAndPort(String ip, String port) { + this.ip = ip; + this.port = port; + this.propIpSegment = ip.split(SPOT); + } + + public static IpAndPort build(String node) { + if (ALL.equals(node)) { + return new IpAndPort(ALL, ALL); + } + String[] ipPort = node.split(COLON); + if (ipPort.length != 2) { + log.error("The IP address format is error : {}", node); + return null; + } + return new IpAndPort(ipPort[0], ipPort[1]); + } + + /** + * check + * + * @param appIpSegment application ip segment + * @param port application port + */ + public boolean check(String[] appIpSegment, String port) { + return checkPort(port) && checkIp(appIpSegment); + } + + /** + * check ip + * + * @param appIpSegment application ip segment + */ + protected boolean checkIp(String[] appIpSegment) { + if (ALL.equals(this.ip)) { + return true; + } + boolean flag = true; + for (int i = 0; i < propIpSegment.length && flag; i++) { + String propIp = propIpSegment[i]; + String appIp = appIpSegment[i]; + flag = contrastSegment(appIp, propIp); + } + return flag; + } + + /** + * check port + * + * @param port application port + */ + protected boolean checkPort(String port) { + return contrastSegment(port, this.port); + } + + /** + * Check whether the strings are the same + * + * @param appIp appIp + * @param propIp propIp + */ + protected boolean contrastSegment(String appIp, String propIp) { + return ALL.equals(propIp) || appIp.equals(propIp); + } + } +} \ 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/event/AdapterExecutorsRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java index 70fd664e..63eaf876 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/AdapterExecutorsRefreshListener.java @@ -25,7 +25,6 @@ import cn.hippo4j.config.springboot.starter.config.AdapterExecutorProperties; import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolAdapterRegister; import cn.hutool.core.bean.BeanUtil; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; import java.util.List; @@ -40,7 +39,12 @@ import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfig */ @Slf4j @Order(ADAPTER_EXECUTORS_LISTENER) -public class AdapterExecutorsRefreshListener implements ApplicationListener { +public class AdapterExecutorsRefreshListener extends AbstractRefreshListener { + + @Override + public String getNodes(AdapterExecutorProperties properties) { + return properties.getNodes(); + } @Override public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent event) { @@ -52,11 +56,11 @@ public class AdapterExecutorsRefreshListener implements ApplicationListener { if (Objects.equals(val.mark(), each.getMark())) { val.updateThreadPool(BeanUtil.toBean(each, ThreadPoolAdapterParameter.class)); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java index 8dbc02b3..1bf61464 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/DynamicThreadPoolRefreshListener.java @@ -21,6 +21,7 @@ import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; import cn.hippo4j.common.toolkit.CollectionUtil; +import cn.hippo4j.config.springboot.starter.config.AdapterExecutorProperties; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.ExecutorProperties; import cn.hippo4j.config.springboot.starter.notify.CoreNotifyConfigBuilder; @@ -38,7 +39,6 @@ import cn.hippo4j.message.service.ThreadPoolNotifyAlarm; import com.google.common.collect.Lists; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; import java.util.List; @@ -60,7 +60,7 @@ import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfig @Slf4j @RequiredArgsConstructor @Order(EXECUTORS_LISTENER) -public class DynamicThreadPoolRefreshListener implements ApplicationListener { +public class DynamicThreadPoolRefreshListener extends AbstractRefreshListener { private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler; @@ -68,13 +68,21 @@ public class DynamicThreadPoolRefreshListener implements ApplicationListener executors = bindableConfigProperties.getExecutors(); for (ExecutorProperties properties : executors) { String threadPoolId = properties.getThreadPoolId(); - /** + if (!match(properties)) { + continue; + } + /* * Check whether the notification configuration is consistent, this operation will not trigger the notification. */ checkNotifyConsistencyAndReplace(properties); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java index ec380bad..f6601b75 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/PlatformsRefreshListener.java @@ -25,7 +25,6 @@ import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; import cn.hippo4j.message.dto.NotifyConfigDTO; import cn.hippo4j.message.service.Hippo4jBaseSendMessageService; -import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; import java.util.List; @@ -37,7 +36,7 @@ import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfig * Platforms refresh listener. */ @Order(PLATFORMS_LISTENER) -public class PlatformsRefreshListener implements ApplicationListener { +public class PlatformsRefreshListener extends AbstractRefreshListener { @Override public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynamicRefreshEvent) { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/RefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/RefreshListener.java new file mode 100644 index 00000000..bd072c04 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/RefreshListener.java @@ -0,0 +1,31 @@ +/* + * 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.config.springboot.starter.refresher.event; + +import cn.hippo4j.common.function.Matcher; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; + +/** + * Refresh listener interface. + * T:event. + * M:properties. + */ +public interface RefreshListener extends ApplicationListener, Matcher { + +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java index 5e01320c..f529bee7 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/event/WebExecutorRefreshListener.java @@ -25,7 +25,6 @@ import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; import cn.hippo4j.config.springboot.starter.config.WebThreadPoolProperties; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; import java.util.Objects; @@ -37,7 +36,12 @@ import static cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfig */ @Slf4j @Order(WEB_EXECUTOR_LISTENER) -public class WebExecutorRefreshListener implements ApplicationListener { +public class WebExecutorRefreshListener extends AbstractRefreshListener { + + @Override + public String getNodes(WebThreadPoolProperties properties) { + return properties.getNodes(); + } @Override public void onApplicationEvent(Hippo4jConfigDynamicRefreshEvent threadPoolDynamicRefreshEvent) { @@ -75,7 +79,7 @@ public class WebExecutorRefreshListener implements ApplicationListener