Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shedfreez committed Nov 20, 2024
1 parent 24d7b1c commit 3990670
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ private void initDataSource(ConfigurationImpl configuration) {
throw new RuntimeException("Config server address is blank. Please check your config in bootstrap.yml"
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
}

checkAddressAccessible(configAddresses);
if (polarisConfigProperties.isCheckAddress()) {
checkAddressAccessible(configAddresses);
}

configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
// this class provides customized logic for some customers to configure special business group files
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();

private volatile CompositePropertySource compositePropertySourceCache = null;
private volatile static CompositePropertySource compositePropertySourceCache = null;

public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileGroup;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
Expand Down Expand Up @@ -122,8 +123,11 @@ private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher
}

private void registerPolarisConfigGroupChangeListener(PolarisPropertySource polarisPropertySource) {
configFileService.getConfigFileGroup(polarisPropertySource.getNamespace(), polarisPropertySource.getGroup()).
addChangeListener(event -> {
ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(polarisPropertySource.getNamespace(), polarisPropertySource.getGroup());
if (configFileGroup == null) {
return;
}
configFileGroup.addChangeListener(event -> {
try {
LOGGER.debug("ConfigFileGroup receive onChange event:{}", event);
List<ConfigFileMetadata> oldConfigFileMetadataList = event.getOldConfigFileMetadataList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class PolarisConfigProperties {
*/
private boolean internalEnabled = true;

private boolean checkAddress = true;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -192,6 +194,14 @@ public void setInternalEnabled(boolean internalEnabled) {
this.internalEnabled = internalEnabled;
}

public boolean isCheckAddress() {
return checkAddress;
}

public void setCheckAddress(boolean checkAddress) {
this.checkAddress = checkAddress;
}

@Override
public String toString() {
return "PolarisConfigProperties{" +
Expand All @@ -207,6 +217,7 @@ public String toString() {
", dataSource='" + dataSource + '\'' +
", localFileRootPath='" + localFileRootPath + '\'' +
", internalEnabled=" + internalEnabled +
", checkAddress=" + checkAddress +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void setUp() {

@Test
public void testLoadApplicationPropertiesFile() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -105,6 +107,7 @@ public void testLoadApplicationPropertiesFile() {

@Test
public void testActiveProfileFilesPriorityBiggerThanDefault() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -154,6 +157,7 @@ public void testActiveProfileFilesPriorityBiggerThanDefault() {

@Test
public void testGetCustomFiles() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -208,6 +212,7 @@ public void testGetCustomFiles() {

@Test
public void testGetCustomGroupFiles() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -260,4 +265,16 @@ public void testGetCustomGroupFiles() {
assertThat(propertySource.getProperty("k2")).isEqualTo("v2");
assertThat(propertySource.getProperty("k3")).isEqualTo("v3");
}

private void clearCompositePropertySourceCache() {
try {
Class<?> clazz = PolarisConfigFileLocator.class;
Field field = clazz.getDeclaredField("compositePropertySourceCache");
field.setAccessible(true);
field.set(null, null);
}
catch (Exception e) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

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

import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.google.common.collect.Sets;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -35,6 +38,7 @@
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -47,7 +51,9 @@
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = ConfigChangeListenerTest.TestApplication.class,
properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml"})
properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml",
"spring.cloud.polaris.config.connect-remote-server=true", "spring.cloud.polaris.config.check-address=false"
})
public class ConfigChangeListenerTest {

private static final CountDownLatch hits = new CountDownLatch(2);
Expand All @@ -58,6 +64,19 @@ public class ConfigChangeListenerTest {
@Autowired
private TestApplication.TestConfig testConfig;

@BeforeAll
public static void setUp() {
try {
Class<?> clazz = PolarisConfigFileLocator.class;
Field field = clazz.getDeclaredField("compositePropertySourceCache");
field.setAccessible(true);
field.set(null, new CompositePropertySource("mock"));
}
catch (Exception e) {
// ignore
}
}

@Test
public void test() throws InterruptedException {
//before change
Expand Down

This file was deleted.

Loading

0 comments on commit 3990670

Please sign in to comment.