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 deleted file mode 100644 index e3b752ea..00000000 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/api/ThreadPoolInitRefresh.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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-common/src/test/java/cn/hippo4j/common/web/base/ResultsTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/web/base/ResultsTest.java index 826cf280..9da7c892 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/web/base/ResultsTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/web/base/ResultsTest.java @@ -1,3 +1,20 @@ +/* + * 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.web.base; import cn.hippo4j.common.toolkit.Assert; @@ -32,6 +49,7 @@ public class ResultsTest { String code = "500"; String msg = "message"; AbstractException abstractException = new AbstractException(msg, new Throwable(), new ErrorCode() { + @Override public String getCode() { return code; @@ -58,8 +76,8 @@ public class ResultsTest { public void testFailure2() { String code = "500"; String msg = "message"; - ErrorCode errorCode = new ErrorCode() { + @Override public String getCode() { return code; diff --git a/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java index 46a94110..77b5f0c1 100644 --- a/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java +++ b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java @@ -22,12 +22,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public final class JwtTokenUtilTest { - Long userId = 1L; - String username = "baymax"; - String role = ""; - boolean isRememberMe = true; - String token; + private Long userId = 1L; + + private String username = "baymax"; + + private String role = ""; + + private boolean isRememberMe = true; + + private String token; @BeforeEach public void setUp() { 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 f78e5873..bb01339e 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,7 +18,6 @@ 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; @@ -36,7 +35,7 @@ import java.util.concurrent.ExecutorService; * Abstract core thread-pool dynamic refresh. */ @Slf4j -public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, ThreadPoolInitRefresh, InitializingBean { +public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean { private final BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt; @@ -49,10 +48,10 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPo bootstrapConfigPropertiesBinderAdapt = ApplicationContextHolder.getBean(BootstrapConfigPropertiesBinderAdapt.class); } - @Override - public void initRefresh(String context) { - dynamicRefresh(context); - } + /** + * Init register listener. + */ + protected abstract void initRegisterListener(); @Override public void dynamicRefresh(String configContent) { @@ -67,9 +66,23 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPo Optional.ofNullable(configInfo).ifPresent(each -> each.putAll(newValueChangeMap)); } BootstrapConfigProperties binderCoreProperties = bootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties); - ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, binderCoreProperties)); + publishDynamicThreadPoolEvent(binderCoreProperties); + } catch (Exception ex) { + log.error("Hippo4j config mode dynamic refresh failed.", ex); + } + } + + private void publishDynamicThreadPoolEvent(BootstrapConfigProperties configProperties) { + ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, configProperties)); + } + + @Override + public void afterPropertiesSet() throws Exception { + try { + publishDynamicThreadPoolEvent(bootstrapConfigProperties); } catch (Exception ex) { - log.error("Hippo-4J core dynamic refresh failed.", ex); + log.error("Hippo4j failed to initialize update configuration.", ex); } + initRegisterListener(); } } 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 6233bd82..f70c956a 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 @@ -42,17 +42,7 @@ public class ApolloRefresherHandler extends AbstractConfigThreadPoolDynamicRefre 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() { + public void initRegisterListener() { String[] apolloNamespaces = this.namespace.split(","); this.namespace = apolloNamespaces[0]; Config config = ConfigService.getConfig(String.format("%s.%s", namespace, bootstrapConfigProperties.getConfigFileType().getValue())); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java index 69b60ffa..51826680 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/ConsulRefresherHandler.java @@ -72,15 +72,8 @@ public class ConsulRefresherHandler extends AbstractConfigThreadPoolDynamicRefre .findFirst().orElse(StringUtils.EMPTY); } - /** - * TODO consul已实现,暂时不重写 - * @return - */ @Override - public String getProperties() { - return null; + protected void initRegisterListener() { + // The listener has been registered by annotation. } - - @Override - public void afterPropertiesSet() {} } 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 63a245d1..1ae52abb 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 @@ -23,12 +23,14 @@ import io.etcd.jetcd.*; import io.etcd.jetcd.kv.GetResponse; import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchResponse; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Objects; +import java.util.concurrent.ExecutionException; /** * Etcd refresher handler. @@ -50,19 +52,9 @@ public class EtcdRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh private static final String KEY = "key"; + @SneakyThrows(value = {InterruptedException.class, ExecutionException.class}) @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 { + public void initRegisterListener() { Map etcd = bootstrapConfigProperties.getEtcd(); String key = etcd.get(KEY); Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET)); 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 0272aefb..445f3cd2 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 @@ -20,6 +20,8 @@ package cn.hippo4j.config.springboot.starter.refresher; import cn.hippo4j.common.config.ApplicationContextHolder; import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.nacos.api.config.listener.Listener; +import com.alibaba.nacos.api.exception.NacosException; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import java.util.Map; @@ -41,16 +43,9 @@ public class NacosCloudRefresherHandler extends AbstractConfigThreadPoolDynamicR nacosConfigManager = ApplicationContextHolder.getBean(NacosConfigManager.class); } + @SneakyThrows(NacosException.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 { + public void initRegisterListener() { Map nacosConfig = bootstrapConfigProperties.getNacos(); nacosConfigManager.getConfigService().addListener(nacosConfig.get(DATA_ID), nacosConfig.get(GROUP), new Listener() { 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 ba882bdb..d9e4e3f2 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 @@ -21,6 +21,8 @@ import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; +import com.alibaba.nacos.api.exception.NacosException; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import java.util.Map; @@ -44,16 +46,9 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres this.configService = nacosConfigProperties.configServiceInstance(); } + @SneakyThrows(NacosException.class) @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, 60 * 10000); - } - - @Override - public void afterPropertiesSet() throws Exception { + public void initRegisterListener() { Map nacosConfig = bootstrapConfigProperties.getNacos(); configService.addListener(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/PolarisRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/PolarisRefresherHandler.java index e9ea9e72..ba485536 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/PolarisRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/PolarisRefresherHandler.java @@ -54,14 +54,7 @@ public class PolarisRefresherHandler extends AbstractConfigThreadPoolDynamicRefr private String fileName; @Override - public String getProperties() { - ConfigKVFile configFile = getConfigKVFile(); - configFile.getContent(); - return configFile.getContent(); - } - - @Override - public void afterPropertiesSet() { + public void initRegisterListener() { ConfigKVFile configFile = getConfigKVFile(); configFile.addChangeListener((ConfigKVFileChangeListener) event -> { String content = configFile.getContent(); 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 00f6e6e5..19326c7d 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 @@ -52,15 +52,7 @@ public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRe 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() { + public void initRegisterListener() { Map zkConfigs = bootstrapConfigProperties.getZookeeper(); curatorFramework = CuratorFrameworkFactory.newClient(zkConfigs.get(ZK_CONNECT_STR), new ExponentialBackoffRetry(1000, 3));