mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-22 23:15:07 +08:00
实现内嵌的 apollo 的配置中心的支持
This commit is contained in:
@ -0,0 +1,95 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.internals;
|
||||
|
||||
import com.ctrip.framework.apollo.Apollo;
|
||||
import com.ctrip.framework.apollo.build.ApolloInjector;
|
||||
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
|
||||
import com.ctrip.framework.apollo.enums.ConfigSourceType;
|
||||
import com.ctrip.framework.apollo.internals.AbstractConfigRepository;
|
||||
import com.ctrip.framework.apollo.internals.ConfigRepository;
|
||||
import com.ctrip.framework.apollo.tracer.Tracer;
|
||||
import com.ctrip.framework.apollo.util.ConfigUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@Slf4j
|
||||
public class DBConfigRepository extends AbstractConfigRepository {
|
||||
|
||||
private final static ScheduledExecutorService m_executorService;
|
||||
|
||||
static {
|
||||
m_executorService = Executors.newScheduledThreadPool(1,
|
||||
ApolloThreadFactory.create(DBConfigRepository.class.getSimpleName(), true));
|
||||
}
|
||||
|
||||
private final ConfigUtil m_configUtil;
|
||||
|
||||
private final AtomicReference<Properties> m_configCache;
|
||||
private final String m_namespace;
|
||||
|
||||
public DBConfigRepository(String namespace) {
|
||||
// 初始化变量
|
||||
this.m_namespace = namespace;
|
||||
m_configCache = new AtomicReference<>();
|
||||
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
|
||||
|
||||
// 初始化加载
|
||||
this.trySync();
|
||||
// 初始化定时任务
|
||||
this.schedulePeriodicRefresh();
|
||||
}
|
||||
|
||||
private AtomicInteger index = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
protected void sync() {
|
||||
System.out.println("我同步啦");
|
||||
|
||||
index.incrementAndGet();
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("demo.test", String.valueOf(index.get()));
|
||||
m_configCache.set(properties);
|
||||
super.fireRepositoryChange(m_namespace, properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getConfig() {
|
||||
// 兜底,避免可能存在配置为 null 的情况
|
||||
if (m_configCache.get() == null) {
|
||||
this.trySync();
|
||||
}
|
||||
// 返回配置
|
||||
return m_configCache.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {
|
||||
// 啥事不做
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigSourceType getSourceType() {
|
||||
return ConfigSourceType.REMOTE;
|
||||
}
|
||||
|
||||
private void schedulePeriodicRefresh() {
|
||||
log.debug("Schedule periodic refresh with interval: {} {}",
|
||||
m_configUtil.getRefreshInterval(), m_configUtil.getRefreshIntervalTimeUnit());
|
||||
m_executorService.scheduleAtFixedRate(() -> {
|
||||
Tracer.logEvent("Apollo.ConfigService", String.format("periodicRefresh: %s", m_namespace));
|
||||
log.debug("refresh config for namespace: {}", m_namespace);
|
||||
|
||||
// 执行同步
|
||||
trySync();
|
||||
|
||||
Tracer.logEvent("Apollo.Client.Version", Apollo.VERSION);
|
||||
}, m_configUtil.getRefreshInterval(), m_configUtil.getRefreshInterval(),
|
||||
m_configUtil.getRefreshIntervalTimeUnit());
|
||||
// TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.internals;
|
||||
|
||||
import cn.iocoder.dashboard.framework.apollo.spi.DBConfigFactory;
|
||||
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
|
||||
import com.ctrip.framework.apollo.internals.*;
|
||||
import com.ctrip.framework.apollo.spi.*;
|
||||
import com.ctrip.framework.apollo.tracer.Tracer;
|
||||
import com.ctrip.framework.apollo.util.ConfigUtil;
|
||||
import com.ctrip.framework.apollo.util.factory.DefaultPropertiesFactory;
|
||||
import com.ctrip.framework.apollo.util.factory.PropertiesFactory;
|
||||
import com.ctrip.framework.apollo.util.http.HttpUtil;
|
||||
import com.ctrip.framework.apollo.util.yaml.YamlParser;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Guice injector
|
||||
*
|
||||
* 基于 Guice 注入器实现类
|
||||
*
|
||||
* @author Jason Song(song_s@ctrip.com)
|
||||
*/
|
||||
public class DefaultXInjector implements Injector {
|
||||
|
||||
private final com.google.inject.Injector m_injector;
|
||||
|
||||
public DefaultXInjector() {
|
||||
try {
|
||||
m_injector = Guice.createInjector(new ApolloModule());
|
||||
} catch (Throwable ex) {
|
||||
ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex);
|
||||
Tracer.logError(exception);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> clazz) {
|
||||
try {
|
||||
return m_injector.getInstance(clazz);
|
||||
} catch (Throwable ex) {
|
||||
Tracer.logError(ex);
|
||||
throw new ApolloConfigException(String.format("Unable to load instance for %s!", clazz.getName()), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> clazz, String name) {
|
||||
// Guice does not support get instance by type and name
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ApolloModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(ConfigManager.class).to(DefaultConfigManager.class).in(Singleton.class);
|
||||
bind(ConfigFactoryManager.class).to(DefaultConfigFactoryManager.class).in(Singleton.class);
|
||||
bind(ConfigRegistry.class).to(DefaultConfigRegistry.class).in(Singleton.class);
|
||||
|
||||
// 自定义 ConfigFactory 实现,使用 DB 作为数据源
|
||||
bind(ConfigFactory.class).to(DBConfigFactory.class).in(Singleton.class);
|
||||
|
||||
bind(ConfigUtil.class).in(Singleton.class);
|
||||
bind(HttpUtil.class).in(Singleton.class);
|
||||
bind(ConfigServiceLocator.class).in(Singleton.class);
|
||||
bind(RemoteConfigLongPollService.class).in(Singleton.class);
|
||||
bind(YamlParser.class).in(Singleton.class);
|
||||
bind(PropertiesFactory.class).to(DefaultPropertiesFactory.class).in(Singleton.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.dashboard.framework.apollo;
|
@ -0,0 +1,27 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.spi;
|
||||
|
||||
import cn.iocoder.dashboard.framework.apollo.internals.DBConfigRepository;
|
||||
import com.ctrip.framework.apollo.Config;
|
||||
import com.ctrip.framework.apollo.ConfigFile;
|
||||
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
|
||||
import com.ctrip.framework.apollo.internals.ConfigRepository;
|
||||
import com.ctrip.framework.apollo.internals.DefaultConfig;
|
||||
import com.ctrip.framework.apollo.spi.ConfigFactory;
|
||||
|
||||
public class DBConfigFactory implements ConfigFactory {
|
||||
|
||||
@Override
|
||||
public Config create(String namespace) {
|
||||
return new DefaultConfig(namespace, this.createDBConfigRepository(namespace));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
|
||||
throw new UnsupportedOperationException("暂不支持 Apollo 配置文件");
|
||||
}
|
||||
|
||||
private ConfigRepository createDBConfigRepository(String namespace) {
|
||||
return new DBConfigRepository(namespace); // TODO 芋艿:看看怎么优化
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user