Refactor configuration mode initialization logic (#1011)

* Refactor configuration mode initialization logic

* Refactoring configuration mode initialization execution timing

* remove superfluous super
pull/1012/head
马称 2 years ago committed by GitHub
parent 3bc4cf4de0
commit c8cb4d8ecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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);
}
}

@ -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;

@ -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() {

@ -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;
@ -27,6 +26,8 @@ import cn.hippo4j.config.springboot.starter.refresher.event.Hippo4jConfigDynamic
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import java.util.Map;
import java.util.Optional;
@ -36,7 +37,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, ApplicationRunner {
private final BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt;
@ -49,10 +50,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 +68,31 @@ 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() {
try {
initRegisterListener();
} catch (Exception ex) {
log.error("Hippo4j failed to initialize register listener.", ex);
}
}
@Override
public void run(ApplicationArguments args) {
try {
publishDynamicThreadPoolEvent(bootstrapConfigProperties);
} catch (Exception ex) {
log.error("Hippo-4J core dynamic refresh failed.", ex);
log.error("Hippo4j failed to initialize update configuration.", ex);
}
}
}

@ -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()));

@ -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() {}
}

@ -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<String, String> 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<String, String> etcd = bootstrapConfigProperties.getEtcd();
String key = etcd.get(KEY);
Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET));

@ -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<String, String> 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<String, String> nacosConfig = bootstrapConfigProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get(DATA_ID),
nacosConfig.get(GROUP), new Listener() {

@ -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;
@ -40,20 +42,12 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres
private ConfigService configService;
public NacosRefresherHandler(NacosConfigProperties nacosConfigProperties) {
super();
this.configService = nacosConfigProperties.configServiceInstance();
}
@SneakyThrows(NacosException.class)
@Override
public String getProperties() throws Exception {
Map<String, String> 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<String, String> nacosConfig = bootstrapConfigProperties.getNacos();
configService.addListener(nacosConfig.get(DATA_ID), nacosConfig.get(GROUP),

@ -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();

@ -52,15 +52,7 @@ public class ZookeeperRefresherHandler extends AbstractConfigThreadPoolDynamicRe
private CuratorFramework curatorFramework;
@Override
public String getProperties() {
Map<String, String> 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<String, String> zkConfigs = bootstrapConfigProperties.getZookeeper();
curatorFramework = CuratorFrameworkFactory.newClient(zkConfigs.get(ZK_CONNECT_STR),
new ExponentialBackoffRetry(1000, 3));

Loading…
Cancel
Save