diff --git a/hippo4j-example/hippo4j-core-spring-boot-starter-example/pom.xml b/hippo4j-example/hippo4j-core-spring-boot-starter-example/pom.xml index 24ecaf3c..ecb92efe 100644 --- a/hippo4j-example/hippo4j-core-spring-boot-starter-example/pom.xml +++ b/hippo4j-example/hippo4j-core-spring-boot-starter-example/pom.xml @@ -43,6 +43,12 @@ hippo4j-core-spring-boot-starter ${revision} + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + 2.2.5.RELEASE + diff --git a/hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/application.yaml b/hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/bootstrap.yaml similarity index 85% rename from hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/application.yaml rename to hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/bootstrap.yaml index b1fc8d4a..dfac43e9 100644 --- a/hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/application.yaml +++ b/hippo4j-example/hippo4j-core-spring-boot-starter-example/src/main/resources/bootstrap.yaml @@ -6,9 +6,14 @@ server: spring: profiles: active: dev - - application: - name: dynamic-threadpool-example + cloud: + nacos: + config: + # group: DEFAULT_GROUP + password: nacos + server-addr: 192.168.1.5:8848 + username: nacos + # namespace: public dynamic: thread-pool: diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml index 8144b094..54ebf34e 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml @@ -28,6 +28,32 @@ cn.hippo4j hippo4j-core + + + com.aliyun + alibaba-dingtalk-service-sdk + + + + log4j + log4j + + + + + + com.alibaba.nacos + nacos-client + 2.0.4 + true + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + 2.2.5.RELEASE + true + diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index 9156c1b2..b7513991 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -1,17 +1,27 @@ package cn.hippo4j.core.starter.config; +import cn.hippo4j.common.api.NotifyConfigBuilder; +import cn.hippo4j.common.api.ThreadPoolDynamicRefresh; import cn.hippo4j.common.config.ApplicationContextHolder; -import cn.hippo4j.common.notify.*; +import cn.hippo4j.common.notify.AlarmControlHandler; +import cn.hippo4j.common.notify.BaseSendMessageServiceImpl; +import cn.hippo4j.common.notify.HippoSendMessageService; +import cn.hippo4j.common.notify.SendMessageHandler; import cn.hippo4j.common.notify.platform.DingSendMessageHandler; import cn.hippo4j.common.notify.platform.LarkSendMessageHandler; import cn.hippo4j.common.notify.platform.WeChatSendMessageHandler; import cn.hippo4j.core.config.UtilAutoConfiguration; import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; -import cn.hippo4j.core.refresh.ThreadPoolDynamicRefresh; import cn.hippo4j.core.starter.notify.CoreNotifyConfigBuilder; +import cn.hippo4j.core.starter.refresher.CoreThreadPoolDynamicRefresh; +import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler; +import cn.hippo4j.core.starter.refresher.NacosRefresherHandler; import cn.hippo4j.core.starter.support.DynamicThreadPoolPostProcessor; +import com.alibaba.cloud.nacos.NacosConfigManager; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -32,6 +42,8 @@ public class DynamicThreadPoolCoreAutoConfiguration { private final BootstrapCoreProperties bootstrapCoreProperties; + private static final String NACOS_KEY = "com.alibaba.cloud.nacos.NacosConfigManager"; + @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public ApplicationContextHolder hippo4JApplicationContextHolder() { @@ -75,8 +87,8 @@ public class DynamicThreadPoolCoreAutoConfiguration { } @Bean - public ThreadPoolDynamicRefresh threadPoolDynamicRefresh(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) { - return new ThreadPoolDynamicRefresh(threadPoolNotifyAlarmHandler); + public ThreadPoolDynamicRefresh coreThreadPoolDynamicRefresh(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) { + return new CoreThreadPoolDynamicRefresh(threadPoolNotifyAlarmHandler); } @Bean @@ -84,4 +96,16 @@ public class DynamicThreadPoolCoreAutoConfiguration { return new DynamicThreadPoolPostProcessor(bootstrapCoreProperties); } + @Bean + @ConditionalOnMissingClass(NACOS_KEY) + public NacosRefresherHandler nacosRefresherHandler() { + return new NacosRefresherHandler(); + } + + @Bean + @ConditionalOnClass(name = NACOS_KEY) + public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager) { + return new NacosCloudRefresherHandler(nacosConfigManager); + } + } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java index 0c7bf603..9b6f2562 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java @@ -1,7 +1,7 @@ package cn.hippo4j.core.starter.notify; import cn.hippo4j.common.notify.AlarmControlHandler; -import cn.hippo4j.common.notify.NotifyConfigBuilder; +import cn.hippo4j.common.api.NotifyConfigBuilder; import cn.hippo4j.common.notify.NotifyConfigDTO; import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.ExecutorProperties; diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/CoreThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/CoreThreadPoolDynamicRefresh.java new file mode 100644 index 00000000..381c7213 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/CoreThreadPoolDynamicRefresh.java @@ -0,0 +1,23 @@ +package cn.hippo4j.core.starter.refresher; + +import cn.hippo4j.common.api.ThreadPoolDynamicRefresh; +import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; +import lombok.AllArgsConstructor; + +/** + * Core thread pool dynamic refresh. + * + * @author chen.ma + * @date 2022/2/26 12:32 + */ +@AllArgsConstructor +public class CoreThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh { + + private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler; + + @Override + public void dynamicRefresh(String content) { + + } + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java new file mode 100644 index 00000000..390802ec --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java @@ -0,0 +1,39 @@ +package cn.hippo4j.core.starter.refresher; + +import com.alibaba.cloud.nacos.NacosConfigManager; +import com.alibaba.nacos.api.config.listener.Listener; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * Nacos cloud refresher handler. + * + * @author chen.ma + * @date 2022/2/26 11:21 + */ +@Slf4j +@AllArgsConstructor +public class NacosCloudRefresherHandler implements InitializingBean, Listener { + + private final NacosConfigManager nacosConfigManager; + + @Override + public void afterPropertiesSet() throws Exception { + nacosConfigManager.getConfigService().addListener("hippo4j-nacos.yaml", "DEFAULT_GROUP", this); + } + + @Override + public Executor getExecutor() { + return Executors.newSingleThreadExecutor(); + } + + @Override + public void receiveConfigInfo(String configInfo) { + log.info("Config :: {}", configInfo); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java new file mode 100644 index 00000000..e057dc2b --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java @@ -0,0 +1,38 @@ +package cn.hippo4j.core.starter.refresher; + +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.listener.Listener; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * Nacos refresher handler. + * + * @author chen.ma + * @date 2022/2/26 00:10 + */ +@Slf4j +public class NacosRefresherHandler implements InitializingBean, Listener { + + @Autowired(required = false) + private ConfigService configService; + + @Override + public void afterPropertiesSet() throws Exception { + configService.addListener("hippo4j-nacos.yaml", "DEFAULT_GROUP", this); + } + + @Override + public Executor getExecutor() { + return Executors.newSingleThreadExecutor(); + } + + @Override + public void receiveConfigInfo(String configInfo) { + log.info("Config :: {}", configInfo); + } +}