Commit 6781b213 by Jason Song

Add auto refresh config support

parent 1a3a2842
...@@ -24,6 +24,10 @@ import org.springframework.core.env.CompositePropertySource; ...@@ -24,6 +24,10 @@ import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
/** /**
...@@ -37,6 +41,9 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, ...@@ -37,6 +41,9 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
private ConfigLoaderManager configLoaderManager; private ConfigLoaderManager configLoaderManager;
private ConfigurableApplicationContext applicationContext; private ConfigurableApplicationContext applicationContext;
private ConfigUtil configUtil;
private ScheduledExecutorService executorService;
private AtomicLong counter;
private RefreshScope scope; private RefreshScope scope;
public ApolloConfigManager() { public ApolloConfigManager() {
...@@ -44,6 +51,15 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, ...@@ -44,6 +51,15 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
throw new IllegalStateException("There should be only one ApolloConfigManager instance!"); throw new IllegalStateException("There should be only one ApolloConfigManager instance!");
} }
this.configLoaderManager = ConfigLoaderFactory.getInstance().getConfigLoaderManager(); this.configLoaderManager = ConfigLoaderFactory.getInstance().getConfigLoaderManager();
this.configUtil = ConfigUtil.getInstance();
this.counter = new AtomicLong();
executorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "ApolloConfigManager-" + counter.incrementAndGet());
return thread;
}
});
} }
@Override @Override
...@@ -53,7 +69,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, ...@@ -53,7 +69,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
String.format("ApplicationContext must implement ConfigurableApplicationContext, but found: %s", applicationContext.getClass().getName())); String.format("ApplicationContext must implement ConfigurableApplicationContext, but found: %s", applicationContext.getClass().getName()));
} }
this.applicationContext = (ConfigurableApplicationContext) applicationContext; this.applicationContext = (ConfigurableApplicationContext) applicationContext;
ConfigUtil.getInstance().setApplicationContext(applicationContext); this.configUtil.setApplicationContext(applicationContext);
} }
/** /**
...@@ -65,6 +81,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, ...@@ -65,6 +81,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registerDependentBeans(registry); registerDependentBeans(registry);
initializePropertySource(); initializePropertySource();
schedulePeriodicRefresh();
} }
/** /**
...@@ -119,9 +136,23 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, ...@@ -119,9 +136,23 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
currentPropertySources.addFirst(currentPropertySource); currentPropertySources.addFirst(currentPropertySource);
} }
void schedulePeriodicRefresh() {
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
updatePropertySource();
} catch (Throwable e) {
logger.error("Refreshing config failed", e);
}
}
}, configUtil.getRefreshInterval(), configUtil.getRefreshInterval(), configUtil.getRefreshTimeUnit());
}
public List<PropertyChange> updatePropertySource() { public List<PropertyChange> updatePropertySource() {
PropertySourceReloadResult result = this.configLoaderManager.reloadPropertySource(); PropertySourceReloadResult result = this.configLoaderManager.reloadPropertySource();
if (result.hasChanges()) { if (result.hasChanges()) {
logger.info("Found changes, refresh environment and refreshscope beans.");
updateEnvironmentPropertySource(result.getPropertySource()); updateEnvironmentPropertySource(result.getPropertySource());
refreshBeans(); refreshBeans();
} }
......
...@@ -15,12 +15,17 @@ import java.net.URL; ...@@ -15,12 +15,17 @@ import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public class ConfigUtil { public class ConfigUtil {
public static final String APOLLO_PROPERTY = "apollo.properties"; public static final String APOLLO_PROPERTY = "apollo.properties";
//TODO read from config?
private static final int refreshInterval = 5;
private static final TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES;
private static ConfigUtil configUtil = new ConfigUtil(); private static ConfigUtil configUtil = new ConfigUtil();
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
...@@ -40,6 +45,14 @@ public class ConfigUtil { ...@@ -40,6 +45,14 @@ public class ConfigUtil {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }
public int getRefreshInterval() {
return refreshInterval;
}
public TimeUnit getRefreshTimeUnit() {
return refreshIntervalTimeUnit;
}
public List<ApolloRegistry> loadApolloRegistries() throws IOException { public List<ApolloRegistry> loadApolloRegistries() throws IOException {
List<URL> resourceUrls = List<URL> resourceUrls =
Collections.list(ClassLoaderUtil.getLoader().getResources(APOLLO_PROPERTY)); Collections.list(ClassLoaderUtil.getLoader().getResources(APOLLO_PROPERTY));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment