add PolarisRefresherHandler (#754)

* add PolarisRefresherHandler

* format code
pull/757/head
weihubeats 2 years ago committed by GitHub
parent 2a3f5973cb
commit ad9a8f76ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,7 @@ import java.math.BigDecimal;
import org.junit.Test;
public final class MatcherFunctionTest {
public static <T> boolean matchTest(Matcher<T> matcher, T value) {
return matcher.match(value);
}

@ -64,6 +64,47 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-configuration-factory</artifactId>
<version>${polaris.version}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-rule</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-canary</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-set</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-isolated</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId>
</exclusion>
<exclusion>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

@ -20,9 +20,11 @@ package cn.hippo4j.config.springboot.starter.config;
import cn.hippo4j.config.springboot.starter.refresher.*;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import io.etcd.jetcd.Client;
import lombok.RequiredArgsConstructor;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -45,6 +47,8 @@ public class ConfigHandlerConfiguration {
private static final String ETCD = "etcd.endpoints";
private static final String POLARIS = "config.serverConnector";
@RequiredArgsConstructor
@ConditionalOnClass(ConfigService.class)
@ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY)
@ -99,4 +103,14 @@ public class ConfigHandlerConfiguration {
}
}
@ConditionalOnClass(ConfigFileService.class)
@ConditionalOnProperty(prefix = BootstrapConfigProperties.PREFIX, name = POLARIS)
static class Polaris {
@Bean
public PolarisRefresherHandler polarisRefresher(ConfigFileService configFileService) {
return new PolarisRefresherHandler(configFileService);
}
}
}

@ -0,0 +1,85 @@
/*
* 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.config.springboot.starter.refresher;
import java.util.Map;
import java.util.Objects;
import com.google.common.collect.Maps;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
/**
*@author : wh
*@date : 2022/10/1 15:24
*@description:
*/
@RequiredArgsConstructor
public class PolarisRefresherHandler extends AbstractConfigThreadPoolDynamicRefresh {
private final ConfigFileService configFileService;
private static final String POLARIS_NAMESPACE = "${spring.dynamic.thread-pool.polaris.namespace:dev}";
private static final String POLARIS_FILE_GROUP = "${spring.dynamic.thread-pool.polaris.file.group:dynamic}";
private static final String POLARIS_FILE_NAME = "${spring.dynamic.thread-pool.polaris.file.name:root/bootstrap.yaml}";
private static final String POLARIS_FILE_TYPE = "${spring.dynamic.thread-pool.polaris.file.type:properties}";
@Value(POLARIS_NAMESPACE)
private String namespace;
@Value(POLARIS_FILE_GROUP)
private String fileGroup;
@Value(POLARIS_FILE_NAME)
private String fileName;
@Override
public String getProperties() {
ConfigKVFile configFile = getConfigKVFile();
configFile.getContent();
return configFile.getContent();
}
@Override
public void afterPropertiesSet() {
ConfigKVFile configFile = getConfigKVFile();
configFile.addChangeListener((ConfigKVFileChangeListener) event -> {
String content = configFile.getContent();
Map<String, Object> newChangeValueMap = Maps.newHashMap();
for (String key : event.changedKeys()) {
ConfigPropertyChangeInfo changeInfo = event.getChangeInfo(key);
newChangeValueMap.put(key, changeInfo.getNewValue());
}
dynamicRefresh(content, newChangeValueMap);
});
}
private ConfigKVFile getConfigKVFile() {
return Objects.equals(POLARIS_FILE_TYPE, "yaml") ? configFileService.getConfigYamlFile(namespace, fileGroup, fileName)
: configFileService.getConfigPropertiesFile(namespace, fileGroup, fileName);
}
}

@ -47,6 +47,7 @@
<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
<apollo.version>1.9.1</apollo.version>
<jetcd.version>0.7.3</jetcd.version>
<polaris.version>1.7.2</polaris.version>
<rocketmq.version>2.2.2</rocketmq.version>
<netty.version>4.1.56.Final</netty.version>
<tomcat-embed-core.version>9.0.55</tomcat-embed-core.version>

Loading…
Cancel
Save