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

Co-authored-by: Haotian Zhang <skyebefreeman@qq.com>
pull/1385/head
Fishtail 4 months ago committed by GitHub
parent 6df2c4dcfe
commit a7f4c24555
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -28,6 +28,6 @@ closes #
- [ ] Will pull request to branch of 2023.
- [ ] Will pull request to branch of 2022.
- [ ] Will pull request to branch of 2020.
- [ ] Will pull request to branch of 2021.
- [ ] Will pull request to branch of 2020.
- [ ] Will pull request to branch of hoxton.

@ -11,3 +11,4 @@
- [fix: fix the lossless provider override in multiple registries scenario](https://github.com/Tencent/spring-cloud-tencent/pull/1380)
- [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1381)
- [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1382)
- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1383)

@ -13,7 +13,6 @@
* 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.config;
@ -26,7 +25,7 @@ import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.util.AddressUtils;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
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.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl;
@ -42,7 +41,7 @@ import org.springframework.util.CollectionUtils;
*
* @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 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.PolarisConfigPropertyRefresher;
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.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
@ -78,9 +77,9 @@ public class PolarisConfigAutoConfiguration {
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) {
return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher);
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties, ContextRefresher contextRefresher) {
return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, contextRefresher);
}
@Configuration(proxyBeanMethods = false)
@ -104,10 +103,10 @@ public class PolarisConfigAutoConfiguration {
}
@Bean
public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, polarisPropertySourceManager,
return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties,
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.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
@ -59,16 +58,10 @@ public class PolarisConfigBootstrapAutoConfiguration {
return new PolarisCryptoConfigProperties();
}
@Bean
@ConditionalOnMissingBean
public PolarisPropertySourceManager polarisPropertySourceManager() {
return new PolarisPropertySourceManager();
}
@Bean
@ConditionalOnConnectRemoteServerEnabled
public ConfigFileService configFileService(PolarisSDKContextManager polarisSDKContextManager) {
return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getSDKContext());
return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getConfigSDKContext());
}
@Bean
@ -77,11 +70,9 @@ public class PolarisConfigBootstrapAutoConfiguration {
PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties,
ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager,
Environment environment) {
return new PolarisConfigFileLocator(polarisConfigProperties,
polarisContextProperties, configFileService,
polarisPropertySourceManager, environment);
polarisContextProperties, configFileService, environment);
}
@Bean

@ -31,8 +31,9 @@ import org.springframework.core.env.Environment;
public interface PolarisConfigCustomExtensionLayer {
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);
boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource);
}

@ -64,17 +64,15 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
private final ConfigFileService configFileService;
private final PolarisPropertySourceManager polarisPropertySourceManager;
private final Environment environment;
// this class provides customized logic for some customers to configure special business group files
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.polarisContextProperties = polarisContextProperties;
this.configFileService = configFileService;
this.polarisPropertySourceManager = polarisPropertySourceManager;
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");
return;
}
polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, polarisPropertySourceManager, configFileService);
polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, configFileService);
}
private void afterLocatePolarisConfigExtension(CompositePropertySource compositePropertySource) {
@ -123,7 +121,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
compositePropertySource.addPropertySource(polarisPropertySource);
polarisPropertySourceManager.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile);
}
@ -206,7 +204,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
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);
}

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

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

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

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

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

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener;
import java.util.Collections;
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.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.RefreshType;
@ -112,7 +111,7 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
beanFactory.removeBeanDefinition(REFLECT_REBINDER_BEAN_NAME);
}
catch (BeansException e) {
// If there is a removeBean exception in this code, do not affect the main process startup. Some user usage may cause the polarisReflectPropertySourceAutoRefresher to not load, and the removeBeanDefinition will report an error
// If there is a removeBean exception in this code, do not affect the main process startup. Some user usage may cause the polarisReflectPropertySourceAutoRefresher to not load, and the removeBeanDefinition will report an error
LOGGER.debug("removeRelatedBeansOfReflect occur error:", e);
}
}
@ -122,12 +121,10 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class);
PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class);
PolarisPropertySourceManager polarisPropertySourceManager = beanFactory.getBean(PolarisPropertySourceManager.class);
ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class);
ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues();
constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties);
constructorArgumentValues.addIndexedArgumentValue(1, polarisPropertySourceManager);
constructorArgumentValues.addIndexedArgumentValue(2, contextRefresher);
constructorArgumentValues.addIndexedArgumentValue(1, contextRefresher);
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.polaris.configuration.api.core.ConfigFileService;
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.extension.ExtendWith;
import org.mockito.Mock;
@ -57,14 +58,17 @@ public class PolarisConfigFileLocatorTest {
@Mock
private ConfigFileService configFileService;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager;
@Mock
private Environment environment;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test
public void testLoadApplicationPropertiesFile() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment);
configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -99,7 +103,7 @@ public class PolarisConfigFileLocatorTest {
@Test
public void testActiveProfileFilesPriorityBiggerThanDefault() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment);
configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
@ -146,7 +150,7 @@ public class PolarisConfigFileLocatorTest {
@Test
public void testGetCustomFiles() {
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, polarisPropertySourceManager, environment);
configFileService, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);

@ -24,7 +24,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Lists;
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.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.ConfigKVFileChangeEvent;
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.extension.ExtendWith;
import org.mockito.Mock;
@ -59,8 +59,6 @@ public class PolarisPropertiesSourceAutoRefresherTest {
private final String testFileName = "application.properties";
@Mock
private PolarisConfigProperties polarisConfigProperties;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager;
@Mock
private SpringValueRegistry springValueRegistry;
@ -68,10 +66,14 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Mock
private PlaceholderHelper placeholderHelper;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test
public void testConfigFileChanged() throws Exception {
PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties,
polarisPropertySourceManager, springValueRegistry, placeholderHelper);
PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, springValueRegistry, placeholderHelper);
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class);
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
TypeConverter typeConverter = mock(TypeConverter.class);
@ -99,7 +101,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
file, content);
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
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.PolarisConfigBootstrapAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
@ -107,7 +106,6 @@ public class ConditionalOnReflectRefreshTypeTest {
.withPropertyValues("spring.cloud.polaris.config.enabled=true");
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisConfigProperties.class);
assertThat(context).hasSingleBean(PolarisPropertySourceManager.class);
assertThat(context).hasSingleBean(ContextRefresher.class);
assertThat(context).hasSingleBean(PolarisRefreshEntireContextRefresher.class);
});

@ -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.PolarisPropertySourceManager;
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.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
/**
* Test for polaris config endpoint.
@ -48,8 +48,11 @@ public class PolarisConfigEndpointTest {
@Mock
private PolarisConfigProperties polarisConfigProperties;
@Mock
private PolarisPropertySourceManager polarisPropertySourceManager;
@BeforeEach
public void setUp() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test
public void testPolarisConfigEndpoint() {
@ -60,9 +63,9 @@ public class PolarisConfigEndpointTest {
MockedConfigKVFile file = new MockedConfigKVFile(content);
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
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();
assertThat(polarisConfigProperties).isEqualTo(info.get("PolarisConfigProperties"));
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.Map;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
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.ConfigKVFileChangeEvent;
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.extension.ExtendWith;
import org.mockito.Mockito;
@ -77,6 +77,11 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
@Autowired
private ConfigurableApplicationContext context;
@BeforeAll
static void beforeAll() {
PolarisPropertySourceManager.clearPropertySources();
}
@Test
public void testNotSwitchConfigRefreshType() {
RefreshType actualRefreshType = context.getEnvironment()
@ -100,12 +105,12 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
file, content);
PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class);
when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class);
PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher);
refresher.setRegistered(false);
spyRefresher.onApplicationEvent(null);
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
@ -134,12 +139,6 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
@SpringBootApplication
protected static class TestApplication {
@Primary
@Bean
public PolarisPropertySourceManager polarisPropertySourceManager() {
return mock(PolarisPropertySourceManager.class);
}
@Primary
@Bean
public ContextRefresher contextRefresher() {

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

@ -21,11 +21,4 @@
<packaging>pom</packaging>
<name>Spring Cloud Tencent Polaris Router Lane Example</name>
<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>

@ -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;
import java.util.ArrayList;
import java.util.List;
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.LosslessAPI;
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.factory.AssemblyAPIFactory;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
@ -53,7 +55,8 @@ public class PolarisSDKContextManager {
* Constant for checking before destroy SDK context.
*/
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 ConsumerAPI consumerAPI;
private volatile static LosslessAPI losslessAPI;
@ -75,7 +78,7 @@ public class PolarisSDKContextManager {
* Don't call this method directly.
*/
public static void innerDestroy() {
if (Objects.nonNull(sdkContext)) {
if (Objects.nonNull(serviceSdkContext)) {
try {
// destroy ProviderAPI
if (Objects.nonNull(providerAPI)) {
@ -119,9 +122,9 @@ public class PolarisSDKContextManager {
assemblyAPI = null;
}
if (Objects.nonNull(sdkContext)) {
sdkContext.destroy();
sdkContext = null;
if (Objects.nonNull(serviceSdkContext)) {
serviceSdkContext.destroy();
serviceSdkContext = null;
}
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() {
if (null == sdkContext) {
if (null == serviceSdkContext) {
try {
// 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.polaris.local-port", Integer.class, 0)));
sdkContext.init();
serviceSdkContext.init();
// init ProviderAPI
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext);
// init losslessAPI
losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(sdkContext);
losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext);
// init ConsumerAPI
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext);
// init RouterAPI
routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext);
routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext);
// init CircuitBreakAPI
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(sdkContext);
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext);
// init LimitAPI
limitAPI = LimitAPIFactory.createLimitAPIByContext(sdkContext);
limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext);
// init AssemblyAPI
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(sdkContext);
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext);
// add shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@ -182,11 +208,25 @@ public class PolarisSDKContextManager {
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() {
init();
return sdkContext;
return serviceSdkContext;
}
public ProviderAPI getProviderAPI() {
@ -222,4 +262,36 @@ public class PolarisSDKContextManager {
public AssemblyAPI getAssemblyAPI() {
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.
*/
@Value("${spring.cloud.polaris.service:${spring.application.name:}}")
private String service;
public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier, Supplier<Integer> portSupplier) {

Loading…
Cancel
Save