From 3d9a8bf8c3a135d05e284d7cc5c5b2303d44b220 Mon Sep 17 00:00:00 2001 From: weihu Date: Fri, 2 Sep 2022 20:14:27 +0800 Subject: [PATCH] add etcd --- .../pom.xml | 49 +++++++++++++++++++ .../etcd/ConfigEtcdExampleApplication.java | 20 ++++++++ .../config/etcd/config/ThreadPoolConfig.java | 35 +++++++++++++ .../etcd/controller/TestController.java | 29 +++++++++++ .../src/main/resources/application.properties | 23 +++++++++ .../src/test/java/Test.java | 21 ++++++++ hippo4j-example/pom.xml | 3 +- .../config/ConfigHandlerConfiguration.java | 2 +- ...ynamicThreadPoolCoreAutoConfiguration.java | 12 +++-- .../refresher/EtcdRefresherHandler.java | 16 ++---- 10 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties create mode 100644 hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/test/java/Test.java diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml new file mode 100644 index 00000000..8ac150a8 --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/pom.xml @@ -0,0 +1,49 @@ + + + + hippo4j-example + cn.hippo4j + ${revision} + + 4.0.0 + + hippo4j-config-etcd-spring-boot-starter-example + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-logging + + + + cn.hippo4j + hippo4j-config-spring-boot-starter + ${revision} + + + + io.etcd + jetcd-core + ${jetcd.version} + + + + cn.hippo4j + hippo4j-example-core + ${revision} + + + + \ No newline at end of file diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java new file mode 100644 index 00000000..2445a9cf --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/ConfigEtcdExampleApplication.java @@ -0,0 +1,20 @@ +package cn.hippo4j.example.config.etcd; + +import cn.hippo4j.core.enable.EnableDynamicThreadPool; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + *@author : wh + *@date : 2022/9/2 19:06 + *@description: + */ +@EnableDynamicThreadPool +@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.config") +public class ConfigEtcdExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigEtcdExampleApplication.class, args); + } +} diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java new file mode 100644 index 00000000..de3bb1de --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java @@ -0,0 +1,35 @@ +package cn.hippo4j.example.config.etcd.config; + +import java.util.concurrent.ThreadPoolExecutor; + +import cn.hippo4j.core.executor.DynamicThreadPool; +import cn.hippo4j.core.executor.support.ThreadPoolBuilder; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *@author : wh + *@date : 2022/9/2 19:26 + *@description: + */ +@Configuration +public class ThreadPoolConfig { + + + @Bean + @DynamicThreadPool + public ThreadPoolExecutor messageConsumeDynamicExecutor() { + String threadPoolId = "message-consume"; + ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder() + .threadFactory(threadPoolId) + .threadPoolId(threadPoolId) + .dynamicPool() + .build(); + return messageConsumeDynamicExecutor; + } + + + + +} diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java new file mode 100644 index 00000000..f323a218 --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/controller/TestController.java @@ -0,0 +1,29 @@ +package cn.hippo4j.example.config.etcd.controller; + +import java.util.concurrent.ThreadPoolExecutor; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + *@author : wh + *@date : 2022/9/2 19:18 + *@description: + */ +@RestController +@RequestMapping +public class TestController { + + + @Autowired + private ThreadPoolExecutor messageConsumeDynamicExecutor; + + + @GetMapping("test") + public void test() { + System.out.println(messageConsumeDynamicExecutor.getMaximumPoolSize()); + } + +} diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties new file mode 100644 index 00000000..eb07b401 --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/resources/application.properties @@ -0,0 +1,23 @@ +server.port=8888 + +spring.application.name=etcd +spring.dynamic.thread-pool.etcd.endpoints = http://192.168.0.154:2379 +spring.dynamic.thread-pool.etcd.key = /thread +spring.dynamic.thread-pool.executors[0].thread-pool-id=message-consume +spring.dynamic.thread-pool.executors[0].core-pool-size=3 +spring.dynamic.thread-pool.executors[0].maximum-pool-size=4 +spring.dynamic.thread-pool.executors[0].queue-capacity=4 +spring.dynamic.thread-pool.executors[0].execute-time-out=1000 +spring.dynamic.thread-pool.executors[0].blocking-queue=LinkedBlockingQueue +spring.dynamic.thread-pool.executors[0].rejected-handler=AbortPolicy +spring.dynamic.thread-pool.executors[0].keep-alive-time=1000 +spring.dynamic.thread-pool.executors[0].allow-core-thread-time-out=true +spring.dynamic.thread-pool.executors[0].thread-name-prefix=message-consume +spring.dynamic.thread-pool.executors[0].alarm=true +spring.dynamic.thread-pool.executors[0].active-alarm=80 +spring.dynamic.thread-pool.executors[0].capacity-alarm=80 +spring.dynamic.thread-pool.executors[0].notify.interval=8 +spring.dynamic.thread-pool.executors[0].notify.receives=111 + +spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT +spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff \ No newline at end of file diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/test/java/Test.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/test/java/Test.java new file mode 100644 index 00000000..6dacd346 --- /dev/null +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/test/java/Test.java @@ -0,0 +1,21 @@ +import java.nio.charset.StandardCharsets; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.kv.GetResponse; + +/** + *@author : wh + *@date : 2022/9/2 20:08 + *@description: + */ +public class Test { + + + public static void main(String[] args) throws Exception{ + + Client client = Client.builder().endpoints("http://192.168.0.154:2379").build(); + GetResponse getResponse = client.getKVClient().get(ByteSequence.from("/thread", StandardCharsets.UTF_8)).get(); + System.out.println("hahah"); + } +} diff --git a/hippo4j-example/pom.xml b/hippo4j-example/pom.xml index a8159637..55448c1c 100644 --- a/hippo4j-example/pom.xml +++ b/hippo4j-example/pom.xml @@ -22,7 +22,8 @@ hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rabbitmq-example hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example hippo4j-spring-boot-starter-adapter-rocketmq-example - + hippo4j-config-etcd-spring-boot-starter-example + true 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 0685a0a3..bff13549 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 @@ -47,7 +47,7 @@ public class ConfigHandlerConfiguration { private static final String ZOOKEEPER_CONNECT_STR_KEY = "zookeeper.zk-connect-str"; - private static final String ETCD = "endpoints"; + private static final String ETCD = "etcd.endpoints"; @RequiredArgsConstructor @ConditionalOnClass(ConfigService.class) 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 fd8ce8f5..8818ab3e 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 @@ -18,10 +18,6 @@ package cn.hippo4j.config.springboot.starter.config; import cn.hippo4j.common.config.ApplicationContextHolder; -import cn.hippo4j.core.config.UtilAutoConfiguration; -import cn.hippo4j.core.enable.MarkerConfiguration; -import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; -import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.config.springboot.starter.monitor.DynamicThreadPoolMonitorExecutor; import cn.hippo4j.config.springboot.starter.notify.CoreNotifyConfigBuilder; import cn.hippo4j.config.springboot.starter.refresher.event.AdapterExecutorsRefreshListener; @@ -31,12 +27,17 @@ import cn.hippo4j.config.springboot.starter.refresher.event.WebExecutorRefreshLi import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolAdapterRegister; import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolConfigService; import cn.hippo4j.config.springboot.starter.support.DynamicThreadPoolPostProcessor; +import cn.hippo4j.core.config.UtilAutoConfiguration; +import cn.hippo4j.core.enable.MarkerConfiguration; +import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; +import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.message.api.NotifyConfigBuilder; 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 lombok.AllArgsConstructor; + import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -59,7 +60,8 @@ import org.springframework.core.annotation.Order; @ConditionalOnProperty(prefix = BootstrapConfigProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true") @Import({ ConfigHandlerConfiguration.EmbeddedNacos.class, ConfigHandlerConfiguration.EmbeddedNacosCloud.class, - ConfigHandlerConfiguration.EmbeddedApollo.class, ConfigHandlerConfiguration.EmbeddedZookeeper.class + ConfigHandlerConfiguration.EmbeddedApollo.class, ConfigHandlerConfiguration.EmbeddedZookeeper.class, + ConfigHandlerConfiguration.EmbeddedEtcd.class }) public class DynamicThreadPoolCoreAutoConfiguration { 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 8af6d026..e99eb463 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 @@ -29,13 +29,12 @@ import io.etcd.jetcd.Client; import io.etcd.jetcd.ClientBuilder; import io.etcd.jetcd.KeyValue; import io.etcd.jetcd.Watch; +import io.etcd.jetcd.kv.GetResponse; import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchResponse; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; /** *@author : wh @@ -43,7 +42,7 @@ import org.springframework.context.ApplicationContextAware; *@description: */ @Slf4j -public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements ApplicationContextAware { +public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh { private ApplicationContext applicationContext; @@ -72,8 +71,7 @@ public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh i Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(",")); - - client = applicationContext.getBean(Client.class); + //todo if (Objects.isNull(client)) { client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset)) .password(ByteSequence.from(password, charset)).authority(authority) @@ -81,11 +79,11 @@ public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh i } // todo Currently only supports json - KeyValue keyValue = client.getKVClient().get(ByteSequence.from(key, charset)).get().getKvs().get(0); + GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get(); + KeyValue keyValue = getResponse.getKvs().get(0); if (Objects.isNull(keyValue)) { return; } - client.getWatchClient().watch(ByteSequence.from(key, charset), new Watch.Listener() { @Override @@ -115,8 +113,4 @@ public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh i } - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } } \ No newline at end of file