From ae2974fc5a34522aee4052e398399f599cce2a0b Mon Sep 17 00:00:00 2001 From: wulingxiao <1251605638@qqcom> Date: Fri, 8 Jul 2022 21:13:04 +0800 Subject: [PATCH] configdata --- .../configdata/PolarisConfigDataLoader.java | 224 +++++++++++++++ .../PolarisConfigDataLocationResolver.java | 254 ++++++++++++++++++ .../configdata/PolarisConfigDataResource.java | 98 +++++++ .../main/resources/META-INF/spring.factories | 10 +- .../polaris-config-data-example/pom.xml | 62 +++++ .../PolarisConfigDataExampleApplication.java | 16 ++ .../src/main/resources/application.yml | 23 ++ .../polaris-config-example/pom.xml | 5 + spring-cloud-tencent-examples/pom.xml | 1 + spring-cloud-tencent-polaris-context/pom.xml | 5 - .../cloud/polaris/context/ModifyAddress.java | 3 + .../config/PolarisContextProperties.java | 4 +- 12 files changed, 697 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataResource.java create mode 100644 spring-cloud-tencent-examples/polaris-config-data-example/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-config-data-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigDataExampleApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java new file mode 100644 index 000000000..0f289ed6b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java @@ -0,0 +1,224 @@ +package com.tencent.cloud.polaris.config.configdata; + +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.config.ConfigFileGroup; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; +import com.tencent.polaris.configuration.api.core.ConfigFileService; +import com.tencent.polaris.configuration.api.core.ConfigKVFile; +import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata; +import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; +import org.apache.commons.logging.Log; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.context.config.ConfigData; +import org.springframework.boot.context.config.ConfigDataLoader; +import org.springframework.boot.context.config.ConfigDataLoaderContext; +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_IMPORTS; +import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_PROFILES; +import static org.springframework.boot.context.config.ConfigData.Option.PROFILE_SPECIFIC; + +/** + * Implementation of {@link ConfigDataLoader}.can be used to load {@link ConfigData} for a given + * {@link PolarisConfigDataResource} + *

+ * Load {@link ConfigData} via {@link PolarisConfigDataLoader} + * + * @author wlx + * @date 2022/7/5 11:14 下午 + */ +public class PolarisConfigDataLoader implements ConfigDataLoader { + + private static final String POLARIS_CONFIG_PROPERTY_SOURCE_NAME = "polaris-config"; + + private final Log log; + + public PolarisConfigDataLoader(DeferredLogFactory logFactory) { + this.log = logFactory.getLog(getClass()); + } + + @Override + public ConfigData load(ConfigDataLoaderContext context, PolarisConfigDataResource resource) + throws IOException, ConfigDataResourceNotFoundException { + ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext(); + CompositePropertySource compositePropertySource = locate(bootstrapContext, resource); + return new ConfigData(compositePropertySource.getPropertySources(), + getOptions(context, resource)); + } + + private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext, + PolarisConfigDataResource resource) { + CompositePropertySource compositePropertySource = new CompositePropertySource( + POLARIS_CONFIG_PROPERTY_SOURCE_NAME); + + // load spring boot default config files + initInternalConfigFiles(compositePropertySource, bootstrapContext,resource); + + PolarisConfigProperties polarisConfigProperties = bootstrapContext.get(PolarisConfigProperties.class); + + // load custom config files + List configFileGroups = polarisConfigProperties.getGroups(); + if (CollectionUtils.isEmpty(configFileGroups)) { + return compositePropertySource; + } + initCustomPolarisConfigFiles(compositePropertySource, configFileGroups, bootstrapContext); + + return compositePropertySource; + } + + private ConfigData.Option[] getOptions(ConfigDataLoaderContext context, + PolarisConfigDataResource resource) { + List options = new ArrayList<>(); + options.add(IGNORE_IMPORTS); + options.add(IGNORE_PROFILES); + // mark it as 'PROFILE_SPECIFIC' config, it has higher priority, + // will override the none profile specific config. +// options.add(PROFILE_SPECIFIC); + return options.toArray(new ConfigData.Option[0]); + } + + private void initInternalConfigFiles(CompositePropertySource compositePropertySource, + ConfigurableBootstrapContext bootstrapContext, + PolarisConfigDataResource resource) { + PolarisPropertySourceManager polarisPropertySourceManager = + bootstrapContext.get(PolarisPropertySourceManager.class); + List internalConfigFiles = getInternalConfigFiles(bootstrapContext,resource); + + for (ConfigFileMetadata configFile : internalConfigFiles) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(bootstrapContext, + configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); + + compositePropertySource.addPropertySource(polarisPropertySource); + + polarisPropertySourceManager.addPropertySource(polarisPropertySource); + + log.info("[SCT Config] Load and inject polaris config file. file = " + configFile); + } + } + + private List getInternalConfigFiles(ConfigurableBootstrapContext bootstrapContext, + PolarisConfigDataResource resource) { + PolarisContextProperties polarisContextProperties = bootstrapContext.get(PolarisContextProperties.class); + String namespace = polarisContextProperties.getNamespace(); + String serviceName = polarisContextProperties.getService(); + if (!StringUtils.hasText(serviceName)) { + serviceName = resource.getServiceName(); + } + + List internalConfigFiles = new LinkedList<>(); + + // priority: application-${profile} > application > boostrap-${profile} > boostrap + List activeProfiles = resource.getProfiles().getActive(); + + for (String activeProfile : activeProfiles) { + if (!StringUtils.hasText(activeProfile)) { + continue; + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml")); + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); + + for (String activeProfile : activeProfiles) { + if (!StringUtils.hasText(activeProfile)) { + continue; + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml")); + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); + + + return internalConfigFiles; + } + + + private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, + List configFileGroups, + ConfigurableBootstrapContext bootstrapContext + ) { + PolarisContextProperties polarisContextProperties = bootstrapContext.get(PolarisContextProperties.class); + String namespace = polarisContextProperties.getNamespace(); + PolarisPropertySourceManager polarisPropertySourceManager = + bootstrapContext.get(PolarisPropertySourceManager.class); + for (ConfigFileGroup configFileGroup : configFileGroups) { + String group = configFileGroup.getName(); + + if (!StringUtils.hasText(group)) { + throw new IllegalArgumentException("polaris config group name cannot be empty."); + } + + List files = configFileGroup.getFiles(); + if (CollectionUtils.isEmpty(files)) { + return; + } + + for (String fileName : files) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(bootstrapContext, + namespace, group, fileName); + + compositePropertySource.addPropertySource(polarisPropertySource); + + polarisPropertySourceManager.addPropertySource(polarisPropertySource); + + String loggerFormat = "[SCT Config] Load and inject polaris config file success. " + + "namespace = %s, group = %s, fileName = %s"; + log.info(String.format(loggerFormat,namespace, group, fileName)); + } + } + } + + private PolarisPropertySource loadPolarisPropertySource( + ConfigurableBootstrapContext bootstrapContext, + String namespace, String group, String fileName) { + + SDKContext sdkContext = bootstrapContext.get(SDKContext.class); + + ConfigKVFile configKVFile; + ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext); + + // unknown extension is resolved as properties file + if (ConfigFileFormat.isPropertyFile(fileName) + || ConfigFileFormat.isUnknownFile(fileName)) { + configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); + } + else if (ConfigFileFormat.isYamlFile(fileName)) { + configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); + } + else { + String loggerFormat = "[SCT Config] Unsupported config file. namespace = %s, group = %s, fileName = %s"; + log.warn(String.format(loggerFormat,namespace, group, fileName) ); + throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + + " can be injected into the spring context"); + } + + Map map = new ConcurrentHashMap<>(); + for (String key : configKVFile.getPropertyNames()) { + map.put(key, configKVFile.getProperty(key, null)); + } + + return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java new file mode 100644 index 000000000..53598f738 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java @@ -0,0 +1,254 @@ +package com.tencent.cloud.polaris.config.configdata; + +import com.tencent.cloud.polaris.config.ConfigurationModifier; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.context.ModifyAddress; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import com.tencent.polaris.api.config.ConfigProvider; +import com.tencent.polaris.api.utils.CollectionUtils; +import com.tencent.polaris.api.utils.StringUtils; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.factory.ConfigAPIFactory; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import org.apache.commons.logging.Log; +import org.springframework.boot.BootstrapRegistry; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataLocationNotFoundException; +import org.springframework.boot.context.config.ConfigDataLocationResolver; +import org.springframework.boot.context.config.ConfigDataLocationResolverContext; +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; +import org.springframework.boot.context.config.Profiles; +import org.springframework.boot.context.properties.bind.BindHandler; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.core.Ordered; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Implementation of {@link ConfigDataLocationResolver}, used to resolve {@link ConfigDataLocation locations} + * into one or more {@link PolarisConfigDataResource polarisConfigDataResource}. + * + * @author wlx + * @date 2022/7/5 11:16 下午 + */ +public class PolarisConfigDataLocationResolver implements + ConfigDataLocationResolver, Ordered { + + + /** + * Prefix for Config Server imports. + */ + public static final String PREFIX = "polaris:"; + + /** + * Prefix for Polaris configurationProperties. + */ + public static final String POLARIS_PREFIX = "spring.cloud.polaris"; + + private final Log log; + + public PolarisConfigDataLocationResolver(DeferredLogFactory logFactory) { + this.log = logFactory.getLog(getClass()); + } + + + @Override + public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) { + if (!location.hasPrefix(PREFIX)) { + return false; + } + return context.getBinder() + .bind("spring.cloud.polaris.config.enabled", Boolean.class) + .orElse(true); + } + + @Override + public List resolve( + ConfigDataLocationResolverContext context, ConfigDataLocation location) + throws ConfigDataLocationNotFoundException, + ConfigDataResourceNotFoundException { + return Collections.emptyList(); + } + + + @Override + public List resolveProfileSpecific( + ConfigDataLocationResolverContext resolverContext, + ConfigDataLocation location, Profiles profiles) + throws ConfigDataLocationNotFoundException { + + ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); + + PolarisConfigProperties polarisConfigProperties = loadPolarisConfigProperties( + resolverContext, + PolarisConfigProperties.class, + POLARIS_PREFIX + ".config" + ); + + PolarisContextProperties polarisContextProperties = loadPolarisConfigProperties( + resolverContext, + PolarisContextProperties.class, + POLARIS_PREFIX + ); + + prepareAndInitPolaris(resolverContext, polarisConfigProperties, polarisContextProperties); + + bootstrapContext.registerIfAbsent(PolarisConfigProperties.class, + BootstrapRegistry.InstanceSupplier.of(polarisConfigProperties)); + + bootstrapContext.registerIfAbsent(PolarisContextProperties.class, + BootstrapRegistry.InstanceSupplier.of(polarisContextProperties)); + + bootstrapContext.registerIfAbsent(PolarisPropertySourceManager.class, + BootstrapRegistry.InstanceSupplier.of(new PolarisPropertySourceManager())); + + // stop sdkContext and register PolarisPropertySourceManager to context + bootstrapContext.addCloseListener( + event -> { + event.getApplicationContext().getBeanFactory().registerSingleton( + "sdkContext", event.getBootstrapContext().get(SDKContext.class) + + ); + event.getApplicationContext().getBeanFactory().registerSingleton( + "polarisPropertySourceManager", + event.getBootstrapContext().get(PolarisPropertySourceManager.class) + ); + } + ); + + return loadConfigDataResources(resolverContext, + location, profiles, polarisConfigProperties, polarisContextProperties); + } + + @Override + public int getOrder() { + return -1; + } + + + public T loadPolarisConfigProperties( + ConfigDataLocationResolverContext context, + Class typeClass, + String prefix) { + Binder binder = context.getBinder(); + BindHandler bindHandler = getBindHandler(context); + + T instance; + if (context.getBootstrapContext().isRegistered(typeClass)) { + instance = context.getBootstrapContext() + .get(typeClass); + } + else { + instance = binder + .bind(POLARIS_PREFIX, Bindable.of(typeClass), + bindHandler) + .map(properties -> binder + .bind(prefix, + Bindable.ofInstance(properties), bindHandler) + .orElse(properties)) + .orElseGet(() -> binder + .bind(prefix, + Bindable.of(typeClass), bindHandler) + .orElseGet(null)); + } + + return instance; + } + + private BindHandler getBindHandler(ConfigDataLocationResolverContext context) { + return context.getBootstrapContext().getOrElse(BindHandler.class, null); + } + + private List loadConfigDataResources(ConfigDataLocationResolverContext resolverContext, + ConfigDataLocation location, + Profiles profiles, + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties + + ) { + List result = new ArrayList<>(); + boolean optional = location.isOptional(); + String fileName = location.getNonPrefixedValue(PREFIX); + String serviceName = loadPolarisConfigProperties(resolverContext, + String.class, "spring.application.name"); + PolarisConfigDataResource polarisConfigDataResource = new PolarisConfigDataResource( + polarisConfigProperties, + polarisContextProperties, + profiles, optional, + fileName,serviceName + ); + result.add(polarisConfigDataResource); + return result; + } + + + private void prepareAndInitPolaris(ConfigDataLocationResolverContext resolverContext, + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { + ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); + if (!bootstrapContext.isRegistered(SDKContext.class)) { + SDKContext sdkContext = sdkContext(resolverContext, + polarisConfigProperties, polarisContextProperties); + sdkContext.init(); + bootstrapContext.register(SDKContext.class, + BootstrapRegistry.InstanceSupplier.of(sdkContext)); + } + + } + + private List modifierList(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { + // add ModifyAddress and ConfigurationModifier to load SDKContext + List modifierList = new ArrayList<>(); + ModifyAddress modifyAddress = new ModifyAddress(); + modifyAddress.setProperties(polarisContextProperties); + + ConfigurationModifier configurationModifier = new ConfigurationModifier(polarisConfigProperties, + polarisContextProperties); + modifierList.add(modifyAddress); + modifierList.add(configurationModifier); + return modifierList; + } + + private SDKContext sdkContext(ConfigDataLocationResolverContext resolverContext, + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { + + // 1. Read user-defined polaris.yml configuration + ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory + .defaultConfig(ConfigProvider.DEFAULT_CONFIG); + + // 2. Override user-defined polaris.yml configuration with SCT configuration + + String defaultHost = polarisContextProperties.getLocalIpAddress(); + if (StringUtils.isBlank(defaultHost)) { + defaultHost = loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); + } + + configuration.getGlobal().getAPI().setBindIP(defaultHost); + + Collection modifiers = modifierList(polarisConfigProperties, polarisContextProperties); + modifiers = modifiers.stream() + .sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder)) + .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(modifiers)) { + for (PolarisConfigModifier modifier : modifiers) { + modifier.modify(configuration); + } + } + return SDKContext.initContextByConfig(configuration); + } + + +} + diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataResource.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataResource.java new file mode 100644 index 000000000..a144e877f --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataResource.java @@ -0,0 +1,98 @@ +package com.tencent.cloud.polaris.config.configdata; + +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import org.springframework.boot.context.config.ConfigData; +import org.springframework.boot.context.config.ConfigDataResource; +import org.springframework.boot.context.config.Profiles; + +import java.util.Objects; + +/** + * A polaris configData resource from which {@link ConfigData} can be loaded. + * + * @author wlx + * @date 2022/7/5 11:13 下午 + */ +public class PolarisConfigDataResource extends ConfigDataResource { + + private final PolarisConfigProperties polarisConfigProperties; + + private final PolarisContextProperties polarisContextProperties; + + private final Profiles profiles; + + private final boolean optional; + + private final String fileName; + + private final String serviceName; + + public PolarisConfigDataResource(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties, + Profiles profiles, + boolean optional, + String fileName, + String serviceName) { + this.polarisConfigProperties = polarisConfigProperties; + this.polarisContextProperties = polarisContextProperties; + this.profiles = profiles; + this.optional = optional; + this.fileName = fileName; + this.serviceName = serviceName; + } + + public PolarisConfigProperties getPolarisConfigProperties() { + return polarisConfigProperties; + } + + public PolarisContextProperties getPolarisContextProperties() { + return polarisContextProperties; + } + + public Profiles getProfiles() { + return profiles; + } + + public boolean isOptional() { + return optional; + } + + public String getFileName() { + return fileName; + } + + public String getServiceName() { + return serviceName; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PolarisConfigDataResource that = (PolarisConfigDataResource) o; + return optional == that.optional && + polarisConfigProperties.equals(that.polarisConfigProperties) && + polarisContextProperties.equals(that.polarisContextProperties) && + profiles.equals(that.profiles) && + fileName.equals(that.fileName) && + serviceName.equals(that.serviceName); + } + + @Override + public int hashCode() { + return Objects.hash(polarisConfigProperties, polarisContextProperties, profiles, optional, fileName, serviceName); + } + + @Override + public String toString() { + return "PolarisConfigDataResource{" + + "polarisConfigProperties=" + polarisConfigProperties + + ", polarisContextProperties=" + polarisContextProperties + + ", profiles=" + profiles + + ", optional=" + optional + + ", fileName='" + fileName + '\'' + + ", serviceName='" + serviceName + '\'' + + '}'; + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories index 98221a39a..8926927cc 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories @@ -2,4 +2,12 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration,\ - com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration + com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration,\ + com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration + +# ConfigData Location Resolvers +org.springframework.boot.context.config.ConfigDataLocationResolver=\ + com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLocationResolver +# ConfigData Loaders +org.springframework.boot.context.config.ConfigDataLoader=\ + com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader diff --git a/spring-cloud-tencent-examples/polaris-config-data-example/pom.xml b/spring-cloud-tencent-examples/polaris-config-data-example/pom.xml new file mode 100644 index 000000000..03e53a17c --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-data-example/pom.xml @@ -0,0 +1,62 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + 1.6.0-2021.0.3-SNAPSHOT + + 4.0.0 + + polaris-config-data-example + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-config + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-config-data-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigDataExampleApplication.java b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigDataExampleApplication.java new file mode 100644 index 000000000..8c4e38762 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigDataExampleApplication.java @@ -0,0 +1,16 @@ +package com.tencent.cloud.polaris.config.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author wlx + * @date 2022/7/6 9:15 下午 + */ +@SpringBootApplication +public class PolarisConfigDataExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(PolarisConfigDataExampleApplication.class, args); + } +} diff --git a/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml new file mode 100644 index 000000000..6951d94d4 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml @@ -0,0 +1,23 @@ +server: + port: 48084 +spring: + application: + name: polaris-config-example + cloud: + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + config: + auto-refresh: true # auto refresh when config file changed + groups: + - name: ${spring.application.name} # group name + files: [ "config/application.properties", "config/bootstrap.yml" ] + config: + import: + - optional:polaris:test.yml +management: + endpoints: + web: + exposure: + include: + - polaris-config diff --git a/spring-cloud-tencent-examples/polaris-config-example/pom.xml b/spring-cloud-tencent-examples/polaris-config-example/pom.xml index bb7ef026e..23f641a08 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-config-example/pom.xml @@ -30,6 +30,11 @@ spring-boot-starter-actuator + + org.springframework.cloud + spring-cloud-starter-bootstrap + + diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index a4c6c9bda..c436f5ae3 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -24,6 +24,7 @@ polaris-router-example metadata-transfer-example polaris-router-grayrelease-example + polaris-config-data-example diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index aa9b8523b..6d7fda248 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -21,11 +21,6 @@ - - org.springframework.cloud - spring-cloud-starter-bootstrap - - com.tencent.polaris diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java index b57cbfe91..806c44a7b 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java @@ -54,4 +54,7 @@ public class ModifyAddress implements PolarisConfigModifier { return ContextConstant.ModifierOrder.FIRST; } + public void setProperties(PolarisContextProperties properties) { + this.properties = properties; + } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java index 1e5103150..f2dad57f8 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java @@ -101,11 +101,11 @@ public class PolarisContextProperties { this.address = address; } - String getLocalIpAddress() { + public String getLocalIpAddress() { return localIpAddress; } - void setLocalIpAddress(String localIpAddress) { + public void setLocalIpAddress(String localIpAddress) { this.localIpAddress = localIpAddress; }