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 2c12363 commit 9ebc236
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 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 @@ -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,13 +18,17 @@

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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -35,6 +39,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 +52,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 +65,11 @@ public class ConfigChangeListenerTest {
@Autowired
private TestApplication.TestConfig testConfig;

@BeforeAll
public static void setUp() {
clearCompositePropertySourceCache();
}

@Test
public void test() throws InterruptedException {
//before change
Expand Down Expand Up @@ -127,4 +139,16 @@ public void publishEvent(Object o) {
}
}
}

private static void clearCompositePropertySourceCache() {
try {
Class<?> clazz = PolarisConfigFileLocator.class;
Field field = clazz.getDeclaredField("compositePropertySourceCache");
field.setAccessible(true);
field.set(null, new CompositePropertySource("mock"));
}
catch (Exception e) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ spring:
polaris:
address: grpc://127.0.0.1:8091
namespace: default
config:
connect-remote-server: false

0 comments on commit 9ebc236

Please sign in to comment.