refactor:let the configuration SDK context stand alone. (#1266)

pull/1297/head
Haotian Zhang 3 months ago committed by GitHub
parent 7b7884bcb0
commit ad18f257e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -26,6 +26,8 @@ closes #
## Checklist (Optional) ## Checklist (Optional)
- [ ] Will pull request to branch of 2020.0. - [ ] Will pull request to branch of 2023.0.
- [ ] Will pull request to branch of 2022.0. - [ ] Will pull request to branch of 2022.0.
- [ ] Will pull request to branch of 2021.0.
- [ ] Will pull request to branch of 2020.0.
- [ ] Will pull request to branch of hoxton. - [ ] Will pull request to branch of hoxton.

@ -7,5 +7,6 @@
- [feat: add lane router examples](https://github.com/Tencent/spring-cloud-tencent/pull/1260) - [feat: add lane router examples](https://github.com/Tencent/spring-cloud-tencent/pull/1260)
- [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1262) - [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1262)
- [fix: fix the lossless provider override in multiple registries scenario](https://github.com/Tencent/spring-cloud-tencent/pull/1263) - [fix: fix the lossless provider override in multiple registries scenario](https://github.com/Tencent/spring-cloud-tencent/pull/1263)
- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1266)
- [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1272) - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1272)
- [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1274) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1274)

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId> <artifactId>spring-cloud-build</artifactId>
<version>4.1.0</version> <version>4.1.1</version>
<relativePath/> <relativePath/>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -92,13 +92,13 @@
<revision>1.14.0-2023.0.0-SNAPSHOT</revision> <revision>1.14.0-2023.0.0-SNAPSHOT</revision>
<!-- Spring Framework --> <!-- Spring Framework -->
<spring.framework.version>6.1.4</spring.framework.version> <spring.framework.version>6.1.6</spring.framework.version>
<!-- Spring Boot --> <!-- Spring Boot -->
<spring.boot.version>3.2.3</spring.boot.version> <spring.boot.version>3.2.4</spring.boot.version>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>2023.0.0</spring.cloud.version> <spring.cloud.version>2023.0.1</spring.cloud.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
<jacoco.version>0.8.8</jacoco.version> <jacoco.version>0.8.8</jacoco.version>
@ -113,14 +113,6 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<!-- This will be deleted later with the version upgrade of Spring Cloud. https://github.com/spring-cloud/spring-cloud-commons/issues/1315 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
<!-- Spring Cloud Tencent Dependencies --> <!-- Spring Cloud Tencent Dependencies -->
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>

@ -13,7 +13,6 @@
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*
*/ */
package com.tencent.cloud.polaris.config; package com.tencent.cloud.polaris.config;
@ -26,7 +25,7 @@ import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.common.util.AddressUtils;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigurationConfigModifier;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl; import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl;
@ -42,7 +41,7 @@ import org.springframework.util.CollectionUtils;
* *
* @author lepdou 2022-03-10 * @author lepdou 2022-03-10
*/ */
public class ConfigurationModifier implements PolarisConfigModifier { public class ConfigurationModifier implements PolarisConfigurationConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class);
private static final String DATA_SOURCE_POLARIS = "polaris"; private static final String DATA_SOURCE_POLARIS = "polaris";

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config;
import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector; import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor; import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
@ -78,9 +77,9 @@ public class PolarisConfigAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { PolarisConfigProperties polarisConfigProperties, ContextRefresher contextRefresher) {
return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, contextRefresher);
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ -104,10 +103,10 @@ public class PolarisConfigAutoConfiguration {
} }
@Bean @Bean
public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) { PlaceholderHelper placeholderHelper) {
return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, polarisPropertySourceManager, return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties,
springValueRegistry, placeholderHelper); springValueRegistry, placeholderHelper);
} }

@ -20,7 +20,6 @@ package com.tencent.cloud.polaris.config;
import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType; import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
@ -59,16 +58,10 @@ public class PolarisConfigBootstrapAutoConfiguration {
return new PolarisCryptoConfigProperties(); return new PolarisCryptoConfigProperties();
} }
@Bean
@ConditionalOnMissingBean
public PolarisPropertySourceManager polarisPropertySourceManager() {
return new PolarisPropertySourceManager();
}
@Bean @Bean
@ConditionalOnConnectRemoteServerEnabled @ConditionalOnConnectRemoteServerEnabled
public ConfigFileService configFileService(PolarisSDKContextManager polarisSDKContextManager) { public ConfigFileService configFileService(PolarisSDKContextManager polarisSDKContextManager) {
return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getSDKContext()); return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getConfigSDKContext());
} }
@Bean @Bean
@ -77,11 +70,9 @@ public class PolarisConfigBootstrapAutoConfiguration {
PolarisConfigProperties polarisConfigProperties, PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, PolarisContextProperties polarisContextProperties,
ConfigFileService configFileService, ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager,
Environment environment) { Environment environment) {
return new PolarisConfigFileLocator(polarisConfigProperties, return new PolarisConfigFileLocator(polarisConfigProperties,
polarisContextProperties, configFileService, polarisContextProperties, configFileService, environment);
polarisPropertySourceManager, environment);
} }
@Bean @Bean

@ -31,8 +31,9 @@ import org.springframework.core.env.Environment;
public interface PolarisConfigCustomExtensionLayer { public interface PolarisConfigCustomExtensionLayer {
void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher); void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher);
void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, PolarisPropertySourceManager polarisPropertySourceManager, ConfigFileService configFileService); void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, ConfigFileService configFileService);
void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource); void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource);
boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource); boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource);
} }

@ -64,17 +64,15 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
private final ConfigFileService configFileService; private final ConfigFileService configFileService;
private final PolarisPropertySourceManager polarisPropertySourceManager;
private final Environment environment; private final Environment environment;
// this class provides customized logic for some customers to configure special business group files // this class provides customized logic for some customers to configure special business group files
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();
public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.polarisContextProperties = polarisContextProperties; this.polarisContextProperties = polarisContextProperties;
this.configFileService = configFileService; this.configFileService = configFileService;
this.polarisPropertySourceManager = polarisPropertySourceManager;
this.environment = environment; this.environment = environment;
} }
@ -104,7 +102,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
LOGGER.debug("[SCT Config] PolarisConfigCustomExtensionLayer is not init, ignore the following execution steps"); LOGGER.debug("[SCT Config] PolarisConfigCustomExtensionLayer is not init, ignore the following execution steps");
return; return;
} }
polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, polarisPropertySourceManager, configFileService); polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, configFileService);
} }
private void afterLocatePolarisConfigExtension(CompositePropertySource compositePropertySource) { private void afterLocatePolarisConfigExtension(CompositePropertySource compositePropertySource) {
@ -123,7 +121,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
polarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile);
} }
@ -206,7 +204,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
polarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
} }

@ -53,8 +53,6 @@ public final class PolarisConfigFilePuller {
private ConfigFileService configFileService; private ConfigFileService configFileService;
private PolarisPropertySourceManager polarisPropertySourceManager;
private PolarisConfigFilePuller() { private PolarisConfigFilePuller() {
} }
@ -64,15 +62,12 @@ public final class PolarisConfigFilePuller {
* *
* @param polarisContextProperties polarisContextProperties * @param polarisContextProperties polarisContextProperties
* @param configFileService configFileService * @param configFileService configFileService
* @param polarisPropertySourceManager polarisPropertySourceManager
* @return PolarisConfigFilePuller instance * @return PolarisConfigFilePuller instance
*/ */
public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService) {
PolarisPropertySourceManager polarisPropertySourceManager) {
PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); PolarisConfigFilePuller puller = new PolarisConfigFilePuller();
puller.polarisContextProperties = polarisContextProperties; puller.polarisContextProperties = polarisContextProperties;
puller.configFileService = configFileService; puller.configFileService = configFileService;
puller.polarisPropertySourceManager = polarisPropertySourceManager;
return puller; return puller;
} }
@ -91,7 +86,7 @@ public final class PolarisConfigFilePuller {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(
configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
polarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile);
} }
} }
@ -129,7 +124,7 @@ public final class PolarisConfigFilePuller {
for (String fileName : files) { for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName); PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
polarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info( LOGGER.info(
"[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", "[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}",
namespace, group, fileName); namespace, group, fileName);

@ -46,16 +46,13 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
private final PolarisConfigProperties polarisConfigProperties; private final PolarisConfigProperties polarisConfigProperties;
private final PolarisPropertySourceManager polarisPropertySourceManager;
private final AtomicBoolean registered = new AtomicBoolean(false); private final AtomicBoolean registered = new AtomicBoolean(false);
// this class provides customized logic for some customers to configure special business group files // this class provides customized logic for some customers to configure special business group files
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();
public PolarisConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager) { public PolarisConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.polarisPropertySourceManager = polarisPropertySourceManager;
} }
@Override @Override
@ -68,7 +65,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
return; return;
} }
List<PolarisPropertySource> polarisPropertySources = polarisPropertySourceManager.getAllPropertySources(); List<PolarisPropertySource> polarisPropertySources = PolarisPropertySourceManager.getAllPropertySources();
if (CollectionUtils.isEmpty(polarisPropertySources)) { if (CollectionUtils.isEmpty(polarisPropertySources)) {
return; return;
} }
@ -144,4 +141,11 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
} }
polarisConfigCustomExtensionLayer.executeRegisterPublishChangeListener(polarisPropertySource); polarisConfigCustomExtensionLayer.executeRegisterPublishChangeListener(polarisPropertySource);
} }
/**
* Just for junit test.
*/
public void setRegistered(boolean registered) {
this.registered.set(registered);
}
} }

@ -28,15 +28,25 @@ import java.util.concurrent.ConcurrentHashMap;
* *
* @author lepdou 2022-03-28 * @author lepdou 2022-03-28
*/ */
public class PolarisPropertySourceManager { public final class PolarisPropertySourceManager {
private final Map<String, PolarisPropertySource> polarisPropertySources = new ConcurrentHashMap<>(); private static final Map<String, PolarisPropertySource> polarisPropertySources = new ConcurrentHashMap<>();
public void addPropertySource(PolarisPropertySource polarisPropertySource) { private PolarisPropertySourceManager() {
polarisPropertySources.putIfAbsent(polarisPropertySource.getPropertySourceName(), polarisPropertySource);
} }
public List<PolarisPropertySource> getAllPropertySources() { public static void addPropertySource(PolarisPropertySource polarisPropertySource) {
polarisPropertySources.put(polarisPropertySource.getPropertySourceName(), polarisPropertySource);
}
public static List<PolarisPropertySource> getAllPropertySources() {
return new ArrayList<>(polarisPropertySources.values()); return new ArrayList<>(polarisPropertySources.values());
} }
/**
* Just for test.
*/
public static void clearPropertySources() {
polarisPropertySources.clear();
}
} }

@ -59,9 +59,8 @@ public class PolarisRefreshAffectedContextRefresher extends PolarisConfigPropert
private TypeConverter typeConverter; private TypeConverter typeConverter;
public PolarisRefreshAffectedContextRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisRefreshAffectedContextRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) {
PlaceholderHelper placeholderHelper) { super(polarisConfigProperties);
super(polarisConfigProperties, polarisPropertySourceManager);
this.springValueRegistry = springValueRegistry; this.springValueRegistry = springValueRegistry;
this.placeholderHelper = placeholderHelper; this.placeholderHelper = placeholderHelper;
} }

@ -35,9 +35,8 @@ public class PolarisRefreshEntireContextRefresher extends PolarisConfigPropertyA
private final ContextRefresher contextRefresher; private final ContextRefresher contextRefresher;
public PolarisRefreshEntireContextRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisRefreshEntireContextRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager,
ContextRefresher contextRefresher) { ContextRefresher contextRefresher) {
super(polarisConfigProperties, polarisPropertySourceManager); super(polarisConfigProperties);
this.contextRefresher = contextRefresher; this.contextRefresher = contextRefresher;
} }

@ -22,9 +22,9 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;
@ -90,13 +90,12 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
PolarisConfigDataResource resource) { PolarisConfigDataResource resource) {
CompositePropertySource compositePropertySource = new CompositePropertySource( CompositePropertySource compositePropertySource = new CompositePropertySource(
POLARIS_CONFIG_PROPERTY_SOURCE_NAME); POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
SDKContext sdkContext = bootstrapContext.get(SDKContext.class); SDKContext sdkContext = PolarisSDKContextManager.innerGetConfigSDKContext();
if (null == this.configFileService) { if (null == this.configFileService) {
this.configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext); this.configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext);
} }
if (null == this.puller) { if (null == this.puller) {
this.puller = PolarisConfigFilePuller.get(resource.getPolarisContextProperties(), this.puller = PolarisConfigFilePuller.get(resource.getPolarisContextProperties(), configFileService);
configFileService, bootstrapContext.get(PolarisPropertySourceManager.class));
} }
Profiles profiles = resource.getProfiles(); Profiles profiles = resource.getProfiles();
if (INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) { if (INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) {

@ -23,11 +23,11 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import com.tencent.cloud.polaris.config.ConfigurationModifier; 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.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
import com.tencent.cloud.polaris.context.ModifyAddress; import com.tencent.cloud.polaris.context.ModifyAddress;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
@ -150,21 +150,6 @@ public class PolarisConfigDataLocationResolver implements
bootstrapContext.registerIfAbsent(PolarisContextProperties.class, bootstrapContext.registerIfAbsent(PolarisContextProperties.class,
BootstrapRegistry.InstanceSupplier.of(polarisContextProperties)); BootstrapRegistry.InstanceSupplier.of(polarisContextProperties));
bootstrapContext.registerIfAbsent(PolarisPropertySourceManager.class,
BootstrapRegistry.InstanceSupplier.of(new PolarisPropertySourceManager()));
bootstrapContext.addCloseListener(
event -> {
// destroy earlier Polaris sdkContext
event.getBootstrapContext().get(SDKContext.class).destroy();
// register PolarisPropertySourceManager to context
PolarisPropertySourceManager polarisPropertySourceManager = event.getBootstrapContext()
.get(PolarisPropertySourceManager.class);
event.getApplicationContext().getBeanFactory().registerSingleton(
"polarisPropertySourceManager", polarisPropertySourceManager);
}
);
return loadConfigDataResources(resolverContext, return loadConfigDataResources(resolverContext,
location, profiles, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); location, profiles, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties);
} }
@ -273,7 +258,7 @@ public class PolarisConfigDataLocationResolver implements
((ConfigurationImpl) sdkContext.getConfig()).getGlobal().getStatReporter().setEnable(false); ((ConfigurationImpl) sdkContext.getConfig()).getGlobal().getStatReporter().setEnable(false);
} }
sdkContext.init(); sdkContext.init();
bootstrapContext.register(SDKContext.class, BootstrapRegistry.InstanceSupplier.of(sdkContext)); PolarisSDKContextManager.setConfigSDKContext(sdkContext);
} }
} }
@ -282,11 +267,10 @@ public class PolarisConfigDataLocationResolver implements
PolarisConfigProperties polarisConfigProperties, PolarisCryptoConfigProperties polarisCryptoConfigProperties, PolarisConfigProperties polarisConfigProperties, PolarisCryptoConfigProperties polarisCryptoConfigProperties,
PolarisContextProperties polarisContextProperties) { PolarisContextProperties polarisContextProperties) {
List<PolarisConfigModifier> modifierList = modifierList(polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); List<PolarisConfigModifier> modifierList = modifierList(polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties);
return SDKContext.initContextByConfig(polarisContextProperties.configuration(modifierList, () -> { return SDKContext.initContextByConfig(polarisContextProperties.configuration(
return loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); modifierList,
}, () -> { () -> loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"),
return loadPolarisConfigProperties(resolverContext, Integer.class, "spring.cloud.polaris.local-port"); () -> loadPolarisConfigProperties(resolverContext, Integer.class, "spring.cloud.polaris.local-port")));
}));
} }
private List<PolarisConfigModifier> modifierList(PolarisConfigProperties polarisConfigProperties, private List<PolarisConfigModifier> modifierList(PolarisConfigProperties polarisConfigProperties,

@ -37,12 +37,9 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
public class PolarisConfigEndpoint { public class PolarisConfigEndpoint {
private final PolarisConfigProperties polarisConfigProperties; private final PolarisConfigProperties polarisConfigProperties;
private final PolarisPropertySourceManager polarisPropertySourceManager;
public PolarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, public PolarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties) {
PolarisPropertySourceManager polarisPropertySourceManager) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.polarisPropertySourceManager = polarisPropertySourceManager;
} }
@ReadOperation @ReadOperation
@ -50,7 +47,7 @@ public class PolarisConfigEndpoint {
Map<String, Object> configInfo = new HashMap<>(); Map<String, Object> configInfo = new HashMap<>();
configInfo.put("PolarisConfigProperties", polarisConfigProperties); configInfo.put("PolarisConfigProperties", polarisConfigProperties);
List<PolarisPropertySource> propertySourceList = polarisPropertySourceManager.getAllPropertySources(); List<PolarisPropertySource> propertySourceList = PolarisPropertySourceManager.getAllPropertySources();
configInfo.put("PolarisPropertySource", propertySourceList); configInfo.put("PolarisPropertySource", propertySourceList);
return configInfo; return configInfo;

@ -18,7 +18,6 @@
package com.tencent.cloud.polaris.config.endpoint; package com.tencent.cloud.polaris.config.endpoint;
import com.tencent.cloud.polaris.config.ConditionalOnPolarisConfigEnabled; import com.tencent.cloud.polaris.config.ConditionalOnPolarisConfigEnabled;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
@ -41,8 +40,7 @@ public class PolarisConfigEndpointAutoConfiguration {
@Bean @Bean
@ConditionalOnAvailableEndpoint @ConditionalOnAvailableEndpoint
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties) {
PolarisPropertySourceManager polarisPropertySourceManager) { return new PolarisConfigEndpoint(polarisConfigProperties);
return new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager);
} }
} }

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener;
import java.util.Collections; import java.util.Collections;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector; import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.enums.RefreshType;
@ -122,12 +121,10 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition(); AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class); beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class);
PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class); PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class);
PolarisPropertySourceManager polarisPropertySourceManager = beanFactory.getBean(PolarisPropertySourceManager.class);
ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class); ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class);
ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues(); ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues();
constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties); constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties);
constructorArgumentValues.addIndexedArgumentValue(1, polarisPropertySourceManager); constructorArgumentValues.addIndexedArgumentValue(1, contextRefresher);
constructorArgumentValues.addIndexedArgumentValue(2, contextRefresher);
beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition); beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition);
} }

@ -29,6 +29,7 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -56,14 +57,17 @@ public class PolarisConfigFileLocatorTest {
@Mock @Mock
private ConfigFileService configFileService; private ConfigFileService configFileService;
@Mock @Mock
private PolarisPropertySourceManager polarisPropertySourceManager;
@Mock
private Environment environment; private Environment environment;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testLoadApplicationPropertiesFile() { public void testLoadApplicationPropertiesFile() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment); configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -98,7 +102,7 @@ public class PolarisConfigFileLocatorTest {
@Test @Test
public void testActiveProfileFilesPriorityBiggerThanDefault() { public void testActiveProfileFilesPriorityBiggerThanDefault() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment); configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -145,7 +149,7 @@ public class PolarisConfigFileLocatorTest {
@Test @Test
public void testGetCustomFiles() { public void testGetCustomFiles() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment); configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);

@ -27,6 +27,7 @@ import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -52,13 +53,15 @@ public class PolarisConfigFilePullerTest {
private PolarisContextProperties polarisContextProperties; private PolarisContextProperties polarisContextProperties;
@Mock @Mock
private ConfigFileService configFileService; private ConfigFileService configFileService;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager; @BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testPullInternalConfigFiles() { public void testPullInternalConfigFiles() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService);
polarisPropertySourceManager);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -90,8 +93,7 @@ public class PolarisConfigFilePullerTest {
@Test @Test
public void testPullInternalConfigFilesWithProfile() { public void testPullInternalConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService);
polarisPropertySourceManager);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -137,8 +139,7 @@ public class PolarisConfigFilePullerTest {
@Test @Test
public void testPullCustomConfigFilesWithProfile() { public void testPullCustomConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService);
polarisPropertySourceManager);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);

@ -24,7 +24,6 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValue; import com.tencent.cloud.polaris.config.spring.property.SpringValue;
@ -32,6 +31,7 @@ import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ChangeType;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -59,8 +59,6 @@ public class PolarisPropertiesSourceAutoRefresherTest {
private final String testFileName = "application.properties"; private final String testFileName = "application.properties";
@Mock @Mock
private PolarisConfigProperties polarisConfigProperties; private PolarisConfigProperties polarisConfigProperties;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager;
@Mock @Mock
private SpringValueRegistry springValueRegistry; private SpringValueRegistry springValueRegistry;
@ -68,10 +66,14 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Mock @Mock
private PlaceholderHelper placeholderHelper; private PlaceholderHelper placeholderHelper;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testConfigFileChanged() throws Exception { public void testConfigFileChanged() throws Exception {
PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, springValueRegistry, placeholderHelper);
polarisPropertySourceManager, springValueRegistry, placeholderHelper);
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class);
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
TypeConverter typeConverter = mock(TypeConverter.class); TypeConverter typeConverter = mock(TypeConverter.class);
@ -99,7 +101,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName, PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
file, content); file, content);
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED); ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);

@ -24,7 +24,6 @@ import java.util.Objects;
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
@ -107,7 +106,6 @@ public class ConditionalOnReflectRefreshTypeTest {
.withPropertyValues("spring.cloud.polaris.config.enabled=true"); .withPropertyValues("spring.cloud.polaris.config.enabled=true");
contextRunner.run(context -> { contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisConfigProperties.class); assertThat(context).hasSingleBean(PolarisConfigProperties.class);
assertThat(context).hasSingleBean(PolarisPropertySourceManager.class);
assertThat(context).hasSingleBean(ContextRefresher.class); assertThat(context).hasSingleBean(ContextRefresher.class);
assertThat(context).hasSingleBean(PolarisRefreshEntireContextRefresher.class); assertThat(context).hasSingleBean(PolarisRefreshEntireContextRefresher.class);
}); });

@ -25,14 +25,15 @@ import java.util.Map;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic; import org.mockito.MockedStatic;
@ -49,7 +50,7 @@ import org.springframework.core.env.PropertySource;
import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.CUSTOM_POLARIS_CONFIG_FILE_LOADED; import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.CUSTOM_POLARIS_CONFIG_FILE_LOADED;
import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.INTERNAL_CONFIG_FILES_LOADED; import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.INTERNAL_CONFIG_FILES_LOADED;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -68,11 +69,14 @@ public class PolarisConfigDataLoaderTest {
private final String testServiceName = "testServiceName"; private final String testServiceName = "testServiceName";
private final String polarisConfigPropertySourceName = "polaris-config"; private final String polarisConfigPropertySourceName = "polaris-config";
@BeforeAll
static void beforeAll() {
PolarisSDKContextManager.setConfigSDKContext(sdkContext);
}
@AfterAll @AfterAll
static void afterAll() { static void afterAll() {
if (sdkContext != null) { PolarisSDKContextManager.innerGetConfigSDKContext();
sdkContext.destroy();
}
} }
@Test @Test
@ -100,9 +104,6 @@ public class PolarisConfigDataLoaderTest {
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")) when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
.thenReturn(propertiesFile); .thenReturn(propertiesFile);
when(context.getBootstrapContext()).thenReturn(bootstrapContext); when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -123,7 +124,7 @@ public class PolarisConfigDataLoaderTest {
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
mockedStatic.when(() -> { mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext); ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class));
}).thenReturn(configFileService); }).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
@ -181,8 +182,6 @@ public class PolarisConfigDataLoaderTest {
when(profiles.getActive()).thenReturn(active); when(profiles.getActive()).thenReturn(active);
when(context.getBootstrapContext()).thenReturn(bootstrapContext); when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -202,7 +201,7 @@ public class PolarisConfigDataLoaderTest {
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
mockedStatic.when(() -> { mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext); ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class));
}).thenReturn(configFileService); }).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
@ -254,9 +253,6 @@ public class PolarisConfigDataLoaderTest {
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1); when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
when(context.getBootstrapContext()).thenReturn(bootstrapContext); when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -278,7 +274,7 @@ public class PolarisConfigDataLoaderTest {
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
mockedStatic.when(() -> { mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext); ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class));
}).thenReturn(configFileService); }).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);

@ -26,13 +26,13 @@ import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
/** /**
* Test for polaris config endpoint. * Test for polaris config endpoint.
@ -48,8 +48,11 @@ public class PolarisConfigEndpointTest {
@Mock @Mock
private PolarisConfigProperties polarisConfigProperties; private PolarisConfigProperties polarisConfigProperties;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager; @BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testPolarisConfigEndpoint() { public void testPolarisConfigEndpoint() {
@ -60,9 +63,9 @@ public class PolarisConfigEndpointTest {
MockedConfigKVFile file = new MockedConfigKVFile(content); MockedConfigKVFile file = new MockedConfigKVFile(content);
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName, PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
file, content); file, content);
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
PolarisConfigEndpoint endpoint = new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager); PolarisConfigEndpoint endpoint = new PolarisConfigEndpoint(polarisConfigProperties);
Map<String, Object> info = endpoint.polarisConfig(); Map<String, Object> info = endpoint.polarisConfig();
assertThat(polarisConfigProperties).isEqualTo(info.get("PolarisConfigProperties")); assertThat(polarisConfigProperties).isEqualTo(info.get("PolarisConfigProperties"));
assertThat(Lists.newArrayList(polarisPropertySource)).isEqualTo(info.get("PolarisPropertySource")); assertThat(Lists.newArrayList(polarisPropertySource)).isEqualTo(info.get("PolarisPropertySource"));

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
@ -31,6 +30,7 @@ import com.tencent.cloud.polaris.config.enums.RefreshType;
import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ChangeType;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -77,6 +77,11 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
@Autowired @Autowired
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
@BeforeAll
static void beforeAll() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testNotSwitchConfigRefreshType() { public void testNotSwitchConfigRefreshType() {
RefreshType actualRefreshType = context.getEnvironment() RefreshType actualRefreshType = context.getEnvironment()
@ -100,12 +105,12 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
file, content); file, content);
PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class); PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class);
PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher); PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher);
refresher.setRegistered(false);
spyRefresher.onApplicationEvent(null); spyRefresher.onApplicationEvent(null);
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
@ -134,12 +139,6 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
@Primary
@Bean
public PolarisPropertySourceManager polarisPropertySourceManager() {
return mock(PolarisPropertySourceManager.class);
}
@Primary @Primary
@Bean @Bean
public ContextRefresher contextRefresher() { public ContextRefresher contextRefresher() {

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
@ -31,6 +30,7 @@ import com.tencent.cloud.polaris.config.enums.RefreshType;
import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ChangeType;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -78,6 +78,11 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest {
@Autowired @Autowired
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test @Test
public void testSwitchConfigRefreshType() { public void testSwitchConfigRefreshType() {
RefreshType actualRefreshType = context.getEnvironment() RefreshType actualRefreshType = context.getEnvironment()
@ -101,12 +106,12 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest {
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
file, content); file, content);
PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
PolarisRefreshEntireContextRefresher refresher = context.getBean(PolarisRefreshEntireContextRefresher.class); PolarisRefreshEntireContextRefresher refresher = context.getBean(PolarisRefreshEntireContextRefresher.class);
PolarisRefreshEntireContextRefresher spyRefresher = Mockito.spy(refresher); PolarisRefreshEntireContextRefresher spyRefresher = Mockito.spy(refresher);
refresher.setRegistered(false);
spyRefresher.onApplicationEvent(null); spyRefresher.onApplicationEvent(null);
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
@ -135,12 +140,6 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
@Primary
@Bean
public PolarisPropertySourceManager polarisPropertySourceManager() {
return mock(PolarisPropertySourceManager.class);
}
@Primary @Primary
@Bean @Bean
public ContextRefresher contextRefresher() { public ContextRefresher contextRefresher() {

@ -21,11 +21,4 @@
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Spring Cloud Tencent Polaris Router Lane Example</name> <name>Spring Cloud Tencent Polaris Router Lane Example</name>
<description>Example of Spring Cloud Tencent Polaris Router Lane</description> <description>Example of Spring Cloud Tencent Polaris Router Lane</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project> </project>

@ -3,6 +3,8 @@ server:
spring: spring:
application: application:
name: LaneRouterGatewayService name: LaneRouterGatewayService
config:
import: optional:polaris
cloud: cloud:
polaris: polaris:
address: grpc://119.91.66.223:8091 address: grpc://119.91.66.223:8091

@ -20,11 +20,4 @@
<module>quickstart-callee-service-a</module> <module>quickstart-callee-service-a</module>
<module>quickstart-callee-service-b</module> <module>quickstart-callee-service-b</module>
</modules> </modules>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project> </project>

@ -3,6 +3,8 @@ server:
spring: spring:
application: application:
name: QuickstartCalleeService name: QuickstartCalleeService
config:
import: optional:polaris
cloud: cloud:
polaris: polaris:
address: grpc://119.91.66.223:8091 address: grpc://119.91.66.223:8091

@ -3,6 +3,8 @@ server:
spring: spring:
application: application:
name: QuickstartCalleeService name: QuickstartCalleeService
config:
import: optional:polaris
cloud: cloud:
polaris: polaris:
address: grpc://119.91.66.223:8091 address: grpc://119.91.66.223:8091

@ -3,6 +3,8 @@ server:
spring: spring:
application: application:
name: QuickstartCallerService name: QuickstartCallerService
config:
import: optional:polaris
cloud: cloud:
polaris: polaris:
address: grpc://119.91.66.223:8091 address: grpc://119.91.66.223:8091

@ -3,6 +3,8 @@ server:
spring: spring:
application: application:
name: QuickStartGatewayService name: QuickStartGatewayService
config:
import: optional:polaris
cloud: cloud:
tencent: tencent:
plugin: plugin:

@ -0,0 +1,26 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.tencent.cloud.polaris.context;
/**
* Config modifier interface for Polaris configuration.
*
* @author Haotian Zhang
*/
public interface PolarisConfigurationConfigModifier extends PolarisConfigModifier {
}

@ -17,6 +17,7 @@
package com.tencent.cloud.polaris.context; package com.tencent.cloud.polaris.context;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -25,6 +26,7 @@ import com.tencent.polaris.api.control.Destroyable;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.LosslessAPI; import com.tencent.polaris.api.core.LosslessAPI;
import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.assembly.api.AssemblyAPI; import com.tencent.polaris.assembly.api.AssemblyAPI;
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory; import com.tencent.polaris.assembly.factory.AssemblyAPIFactory;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI; import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
@ -53,7 +55,8 @@ public class PolarisSDKContextManager {
* Constant for checking before destroy SDK context. * Constant for checking before destroy SDK context.
*/ */
public volatile static boolean isRegistered = false; public volatile static boolean isRegistered = false;
private volatile static SDKContext sdkContext; private volatile static SDKContext configSDKContext;
private volatile static SDKContext serviceSdkContext;
private volatile static ProviderAPI providerAPI; private volatile static ProviderAPI providerAPI;
private volatile static ConsumerAPI consumerAPI; private volatile static ConsumerAPI consumerAPI;
private volatile static LosslessAPI losslessAPI; private volatile static LosslessAPI losslessAPI;
@ -75,7 +78,7 @@ public class PolarisSDKContextManager {
* Don't call this method directly. * Don't call this method directly.
*/ */
public static void innerDestroy() { public static void innerDestroy() {
if (Objects.nonNull(sdkContext)) { if (Objects.nonNull(serviceSdkContext)) {
try { try {
// destroy ProviderAPI // destroy ProviderAPI
if (Objects.nonNull(providerAPI)) { if (Objects.nonNull(providerAPI)) {
@ -119,9 +122,9 @@ public class PolarisSDKContextManager {
assemblyAPI = null; assemblyAPI = null;
} }
if (Objects.nonNull(sdkContext)) { if (Objects.nonNull(serviceSdkContext)) {
sdkContext.destroy(); serviceSdkContext.destroy();
sdkContext = null; serviceSdkContext = null;
} }
LOG.info("Polaris SDK context is destroyed."); LOG.info("Polaris SDK context is destroyed.");
} }
@ -131,35 +134,58 @@ public class PolarisSDKContextManager {
} }
} }
/**
* Used for config data.
*/
public static SDKContext innerGetConfigSDKContext() {
if (configSDKContext == null) {
throw new IllegalArgumentException("configSDKContext is not initialized.");
}
return configSDKContext;
}
public static void innerConfigDestroy() {
try {
if (Objects.nonNull(configSDKContext)) {
configSDKContext.destroy();
configSDKContext = null;
}
LOG.info("Polaris SDK config context is destroyed.");
}
catch (Throwable throwable) {
LOG.info("Polaris SDK config context is destroyed failed.", throwable);
}
}
public void init() { public void init() {
if (null == sdkContext) { if (null == serviceSdkContext) {
try { try {
// init SDKContext // init SDKContext
sdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList,
() -> environment.getProperty("spring.cloud.client.ip-address"), () -> environment.getProperty("spring.cloud.client.ip-address"),
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
sdkContext.init(); serviceSdkContext.init();
// init ProviderAPI // init ProviderAPI
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext); providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext);
// init losslessAPI // init losslessAPI
losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(sdkContext); losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext);
// init ConsumerAPI // init ConsumerAPI
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext); consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext);
// init RouterAPI // init RouterAPI
routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext); routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext);
// init CircuitBreakAPI // init CircuitBreakAPI
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(sdkContext); circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext);
// init LimitAPI // init LimitAPI
limitAPI = LimitAPIFactory.createLimitAPIByContext(sdkContext); limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext);
// init AssemblyAPI // init AssemblyAPI
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(sdkContext); assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext);
// add shutdown hook // add shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@ -182,11 +208,25 @@ public class PolarisSDKContextManager {
throw throwable; throw throwable;
} }
} }
initConfig();
}
/**
* Used for config data.
*/
public static void setConfigSDKContext(SDKContext context) {
if (configSDKContext == null) {
configSDKContext = context;
// add shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy));
LOG.info("create Polaris config SDK context successfully.");
}
} }
public SDKContext getSDKContext() { public SDKContext getSDKContext() {
init(); init();
return sdkContext; return serviceSdkContext;
} }
public ProviderAPI getProviderAPI() { public ProviderAPI getProviderAPI() {
@ -222,4 +262,36 @@ public class PolarisSDKContextManager {
public AssemblyAPI getAssemblyAPI() { public AssemblyAPI getAssemblyAPI() {
return assemblyAPI; return assemblyAPI;
} }
public SDKContext getConfigSDKContext() {
initConfig();
return configSDKContext;
}
public void initConfig() {
// get modifiers for configuration.
List<PolarisConfigModifier> configModifierList = new ArrayList<>();
for (PolarisConfigModifier modifier : modifierList) {
if (modifier instanceof PolarisConfigurationConfigModifier) {
configModifierList.add(modifier);
}
}
if (null == configSDKContext && CollectionUtils.isNotEmpty(configModifierList)) {
try {
// init config SDKContext
configSDKContext = SDKContext.initContextByConfig(properties.configuration(configModifierList,
() -> environment.getProperty("spring.cloud.client.ip-address"),
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
configSDKContext.init();
// add shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy));
LOG.info("create Polaris config SDK context successfully. properties: {}, ", properties);
}
catch (Throwable throwable) {
LOG.error("create Polaris config SDK context failed. properties: {}, ", properties, throwable);
throw throwable;
}
}
}
} }

@ -76,6 +76,7 @@ public class PolarisContextProperties {
/** /**
* polaris service name. * polaris service name.
*/ */
@Value("${spring.cloud.polaris.service:${spring.application.name:}}")
private String service; private String service;
public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier, Supplier<Integer> portSupplier) { public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier, Supplier<Integer> portSupplier) {

Loading…
Cancel
Save