From d4f5c70408d9badadefa5bd42913bf4621a0720c Mon Sep 17 00:00:00 2001 From: ayi <2084276225@qq.com> Date: Fri, 1 Sep 2023 21:09:18 +0800 Subject: [PATCH] feature: hippo4j config model in nacos config before check --- .../api/BootstrapPropertiesInterface.java | 9 +++ .../core/enable/BeforeCheckConfiguration.java | 76 +++++++++++++------ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/kernel/dynamic/api/src/main/java/cn/hippo4j/threadpool/dynamic/api/BootstrapPropertiesInterface.java b/kernel/dynamic/api/src/main/java/cn/hippo4j/threadpool/dynamic/api/BootstrapPropertiesInterface.java index 324cffe69a..5ad4578b89 100644 --- a/kernel/dynamic/api/src/main/java/cn/hippo4j/threadpool/dynamic/api/BootstrapPropertiesInterface.java +++ b/kernel/dynamic/api/src/main/java/cn/hippo4j/threadpool/dynamic/api/BootstrapPropertiesInterface.java @@ -17,6 +17,8 @@ package cn.hippo4j.threadpool.dynamic.api; +import java.util.Map; + /** * Bootstrap properties interface. */ @@ -70,4 +72,11 @@ default String getServerAddr() { default Boolean getBanner() { return null; } + + /** + * Get nacos. + */ + default Map getNacos() { + return null; + } } diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java index c6640d93ac..286655232b 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java @@ -17,6 +17,7 @@ package cn.hippo4j.core.enable; +import cn.hippo4j.common.toolkit.MapUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.config.ConfigEmptyException; import cn.hippo4j.threadpool.dynamic.api.BootstrapPropertiesInterface; @@ -26,6 +27,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import java.util.Map; import java.util.Objects; /** @@ -36,36 +38,62 @@ public class BeforeCheckConfiguration { private final String bootstrapPropertiesClassName = "cn.hippo4j.springboot.starter.config.BootstrapProperties"; + private final String bootstrapConfigPropertiesClassName = "cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties"; @Bean public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties, ConfigurableEnvironment environment) { // TODO test - boolean checkFlag = properties != null && Objects.equals(bootstrapPropertiesClassName, properties.getClass().getName()) && properties.getEnable(); + boolean checkFlag = properties != null && properties.getEnable(); if (checkFlag) { - String namespace = properties.getNamespace(); - if (StringUtil.isBlank(namespace)) { - throw new ConfigEmptyException( - "Web server failed to start. The dynamic thread pool namespace is empty.", - "Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string."); - } - String itemId = properties.getItemId(); - if (StringUtil.isBlank(itemId)) { - throw new ConfigEmptyException( - "Web server failed to start. The dynamic thread pool item id is empty.", - "Please check whether the [spring.dynamic.thread-pool.item-id] configuration is empty or an empty string."); - } - String serverAddr = properties.getServerAddr(); - if (StringUtil.isBlank(serverAddr)) { - throw new ConfigEmptyException( - "Web server failed to start. The dynamic thread pool server addr is empty.", - "Please check whether the [spring.dynamic.thread-pool.server-addr] configuration is empty or an empty string."); - } - String applicationName = environment.getProperty("spring.application.name"); - if (StringUtil.isBlank(applicationName)) { - throw new ConfigEmptyException( - "Web server failed to start. The dynamic thread pool application name is empty.", - "Please check whether the [spring.application.name] configuration is empty or an empty string."); + String propertiesClassName = properties.getClass().getName(); + switch (propertiesClassName) { + case bootstrapPropertiesClassName: { + String namespace = properties.getNamespace(); + if (StringUtil.isBlank(namespace)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool namespace is empty.", + "Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string."); + } + String itemId = properties.getItemId(); + if (StringUtil.isBlank(itemId)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool item id is empty.", + "Please check whether the [spring.dynamic.thread-pool.item-id] configuration is empty or an empty string."); + } + String serverAddr = properties.getServerAddr(); + if (StringUtil.isBlank(serverAddr)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool server addr is empty.", + "Please check whether the [spring.dynamic.thread-pool.server-addr] configuration is empty or an empty string."); + } + String applicationName = environment.getProperty("spring.application.name"); + if (StringUtil.isBlank(applicationName)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool application name is empty.", + "Please check whether the [spring.application.name] configuration is empty or an empty string."); + } + break; + } + case bootstrapConfigPropertiesClassName: { + Map nacos = properties.getNacos(); + if (MapUtil.isNotEmpty(nacos)) { + String group = nacos.get("group"); + if (StringUtil.isBlank(group)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool nacos group is empty.", + "Please check whether the [spring.dynamic.thread-pool.nacos.group] configuration is empty or an empty string."); + } + String dataId = nacos.get("data-id"); + if (StringUtil.isBlank(dataId)) { + throw new ConfigEmptyException( + "Web server failed to start. The dynamic thread pool nacos data-id is empty.", + "Please check whether the [spring.dynamic.thread-pool.nacos.data-id] configuration is empty or an empty string."); + } + } + break; + } + default: } } return new BeforeCheckConfiguration.BeforeCheck();