Skip to content

feat:support consul config data. #1417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
- [fix:fix app starting failed when user using custom OpenAPI bean.](https://github.com/Tencent/spring-cloud-tencent/pull/1414)
- [fix: move ConditionalOnTsfEnabled to spring-cloud-tencent-commons and fix PolarisInetUtilsAutoConfiguration.](https://github.com/Tencent/spring-cloud-tencent/pull/1415)
- [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1416)
- [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1417)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

/**
Expand Down Expand Up @@ -84,6 +85,7 @@ public ConfigurationModifier configurationModifier(PolarisConfigProperties polar
}

@Bean
@Primary
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
@ConditionalOnReflectRefreshType
public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ private void afterLocatePolarisConfigExtension(CompositePropertySource composite
}

private void initInternalConfigFiles(CompositePropertySource compositePropertySource) {
if (!polarisConfigProperties.isInternalEnabled()) {
return;
}
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();

for (ConfigFileMetadata configFile : internalConfigFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

package com.tencent.cloud.polaris.config.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext;
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 com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -79,8 +82,18 @@ private void registerPolarisConfigPublishEvent() {

// register polaris config publish event
for (PolarisPropertySource polarisPropertySource : polarisPropertySources) {
registerPolarisConfigPublishChangeListener(polarisPropertySource);
customRegisterPolarisConfigPublishChangeListener(polarisPropertySource);
if (polarisPropertySource.getConfigKVFile() instanceof CompositeConfigFile) {
CompositeConfigFile configKVFile = (CompositeConfigFile) polarisPropertySource.getConfigKVFile();
for (ConfigKVFile cf : configKVFile.getConfigKVFiles()) {
PolarisPropertySource p = new PolarisPropertySource(cf.getNamespace(), cf.getFileGroup(), cf.getFileName(), cf, new HashMap<>());
registerPolarisConfigPublishChangeListener(p);
customRegisterPolarisConfigPublishChangeListener(p);
}
}
else {
registerPolarisConfigPublishChangeListener(polarisPropertySource);
customRegisterPolarisConfigPublishChangeListener(polarisPropertySource);
}
}
}

Expand All @@ -93,6 +106,7 @@ private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher
}

public void registerPolarisConfigPublishChangeListener(PolarisPropertySource polarisPropertySource) {
LOGGER.info("{} will register polaris config publish listener", polarisPropertySource.getPropertySourceName());
polarisPropertySource.getConfigKVFile()
.addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Map;

import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;

import org.springframework.core.env.MapPropertySource;
Expand All @@ -40,7 +41,7 @@ public class PolarisPropertySource extends MapPropertySource {
private final ConfigKVFile configKVFile;

public PolarisPropertySource(String namespace, String group, String fileName, ConfigKVFile configKVFile, Map<String, Object> source) {
super(namespace + "-" + group + "-" + fileName, source);
super(PolarisPropertySourceUtils.generateName(namespace, group, fileName), source);

this.namespace = namespace;
this.group = group;
Expand All @@ -64,7 +65,7 @@ public String getPropertySourceName() {
return namespace + "-" + group + "-" + fileName;
}

ConfigKVFile getConfigKVFile() {
public ConfigKVFile getConfigKVFile() {
return configKVFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class PolarisConfigProperties {
*/
private String localFileRootPath = "./polaris/backup/config";

/**
* If internal config file enabled.
*/
private boolean internalEnabled = true;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -179,6 +184,14 @@ public void setLocalFileRootPath(String localFileRootPath) {
this.localFileRootPath = localFileRootPath;
}

public boolean isInternalEnabled() {
return internalEnabled;
}

public void setInternalEnabled(boolean internalEnabled) {
this.internalEnabled = internalEnabled;
}

@Override
public String toString() {
return "PolarisConfigProperties{" +
Expand All @@ -193,6 +206,7 @@ public String toString() {
", groups=" + groups +
", dataSource='" + dataSource + '\'' +
", localFileRootPath='" + localFileRootPath + '\'' +
", internalEnabled=" + internalEnabled +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package com.tencent.cloud.polaris.config.configdata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.tencent.cloud.polaris.config.adapter.PolarisConfigCustomExtensionLayer;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller;
import com.tencent.cloud.polaris.config.adapter.PolarisServiceLoaderUtil;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
Expand All @@ -38,6 +41,7 @@
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

Expand All @@ -59,6 +63,7 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
static final AtomicBoolean CUSTOM_POLARIS_CONFIG_FILE_LOADED = new AtomicBoolean(false);
private static final String POLARIS_CONFIG_PROPERTY_SOURCE_NAME = "polaris-config";
private final Log log;
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();
private ConfigFileService configFileService;
private PolarisConfigFilePuller puller;

Expand All @@ -83,7 +88,9 @@ public ConfigData load(ConfigDataLoaderContext context, PolarisConfigDataResourc

public ConfigData load(ConfigurableBootstrapContext bootstrapContext, PolarisConfigDataResource resource) {
CompositePropertySource compositePropertySource = locate(bootstrapContext, resource);
return new ConfigData(compositePropertySource.getPropertySources(), getOptions(resource));
List<PropertySource<?>> propertySourceList = new ArrayList<>(compositePropertySource.getPropertySources());
Collections.reverse(propertySourceList);
return new ConfigData(propertySourceList, getOptions(resource));
}

private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext,
Expand All @@ -97,16 +104,21 @@ private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapCon
if (null == this.puller) {
this.puller = PolarisConfigFilePuller.get(resource.getPolarisContextProperties(), configFileService);
}
// load custom config extension files
if (polarisConfigCustomExtensionLayer != null) {
polarisConfigCustomExtensionLayer.initConfigFiles(null, compositePropertySource, configFileService);
}
// load spring boot default config files
PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties();
Profiles profiles = resource.getProfiles();
if (INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) {
if (polarisConfigProperties.isInternalEnabled() && INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) {
log.info("loading internal config files");
String[] activeProfiles = profiles.getActive().toArray(new String[] {});
String[] defaultProfiles = profiles.getDefault().toArray(new String[] {});
this.puller.initInternalConfigFiles(
compositePropertySource, activeProfiles, defaultProfiles, resource.getServiceName());
}

PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties();
// load custom config files
if (!CollectionUtils.isEmpty(polarisConfigProperties.getGroups()) &&
CUSTOM_POLARIS_CONFIG_FILE_LOADED.compareAndSet(false, true)) {
log.info("loading custom config files");
Expand All @@ -118,6 +130,10 @@ private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapCon
log.info("loading config data config file, group:" + resource.getGroupName() + " file: " + resource.getFileName());
this.puller.initCustomPolarisConfigFile(compositePropertySource, configFileGroup(resource));
}
if (polarisConfigCustomExtensionLayer != null) {
polarisConfigCustomExtensionLayer.executeAfterLocateConfigReturning(compositePropertySource);
}

return compositePropertySource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
Expand All @@ -46,8 +45,7 @@ public class PolarisAdaptorTsfConfigAutoConfiguration {

{
System.setProperty("spring.cloud.polaris.config.refresh-type", "refresh_context");
LOGGER.info(
"[SCTT Config] PolarisAdaptorTsfConfigAutoConfiguration init set spring.cloud.polaris.config.refresh-type to refresh_context");
LOGGER.info("PolarisAdaptorTsfConfigAutoConfiguration init set spring.cloud.polaris.config.refresh-type to refresh_context");
}

@Bean
Expand All @@ -65,7 +63,7 @@ public TsfConsulConfigRefreshEventListener polarisAdaptorTsfConsulRefreshEventLi
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnExpression("${spring.cloud.consul.config.enabled:true} == false and ${tsf.config.instance.released-config.lookup.enabled:true} == true")
@ConditionalOnProperty(name = "tsf.config.instance.released-config.lookup.enabled", matchIfMissing = true)
public PolarisAdaptorTsfConfigController polarisAdaptorTsfConfigController() {
return new PolarisAdaptorTsfConfigController();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* @author juanyinyang
Expand All @@ -32,7 +31,6 @@
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@ConditionalOnTsfEnabled
@ConditionalOnPolarisConfigEnabled
@Import(PolarisAdaptorTsfConfigAutoConfiguration.class)
public class PolarisAdaptorTsfConfigBootstrapConfiguration {


Expand Down
Loading
Loading