mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-19 21:45:06 +08:00
将 sys_config 从 system 迁移到 infra 模块,并进行改名~
This commit is contained in:
@ -0,0 +1,16 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.core;
|
||||
|
||||
/**
|
||||
* 针对 {@link com.ctrip.framework.apollo.core.ConfigConsts} 的补充,主要增加:
|
||||
*
|
||||
* 1. apollo.jdbc.* 配置项的枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class ConfigConsts {
|
||||
|
||||
public static final String APOLLO_JDBC_URL = "apollo.jdbc.url";
|
||||
public static final String APOLLO_JDBC_USERNAME = "apollo.jdbc.username";
|
||||
public static final String APOLLO_JDBC_PASSWORD = "apollo.jdbc.password";
|
||||
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.internals;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.dashboard.framework.apollo.core.ConfigConsts;
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.config.SysConfigDO;
|
||||
import cn.iocoder.dashboard.modules.infra.dal.mysql.dataobject.config.InfConfigDO;
|
||||
import com.ctrip.framework.apollo.Apollo;
|
||||
import com.ctrip.framework.apollo.build.ApolloInjector;
|
||||
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
|
||||
@ -59,12 +60,9 @@ public class DBConfigRepository extends AbstractConfigRepository {
|
||||
this.m_namespace = namespace;
|
||||
this.propertiesFactory = ApolloInjector.getInstance(PropertiesFactory.class);
|
||||
this.m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
|
||||
// TODO 优化到配置
|
||||
String url = "jdbc:mysql://127.0.1:33061/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT";
|
||||
String username = "root";
|
||||
String password = "123456";
|
||||
// DataSource dataSource = DataSourceBuilder.create().url(url).username(username).password(password).build();
|
||||
DataSource dataSource = new DriverManagerDataSource(url, username, password);
|
||||
// 初始化 DB
|
||||
DataSource dataSource = new DriverManagerDataSource(System.getProperty(ConfigConsts.APOLLO_JDBC_URL),
|
||||
System.getProperty(ConfigConsts.APOLLO_JDBC_USERNAME), System.getProperty(ConfigConsts.APOLLO_JDBC_PASSWORD));
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
|
||||
// 初始化加载
|
||||
@ -76,7 +74,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
|
||||
@Override
|
||||
protected void sync() {
|
||||
// 第一步,尝试获取配置
|
||||
List<SysConfigDO> configs = this.loadConfigIfUpdate(this.maxUpdateTime);
|
||||
List<InfConfigDO> configs = this.loadConfigIfUpdate(this.maxUpdateTime);
|
||||
if (CollUtil.isEmpty(configs)) { // 如果没有更新,则返回
|
||||
return;
|
||||
}
|
||||
@ -111,7 +109,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
|
||||
return ConfigSourceType.REMOTE;
|
||||
}
|
||||
|
||||
private Properties buildProperties(List<SysConfigDO> configs) {
|
||||
private Properties buildProperties(List<InfConfigDO> configs) {
|
||||
Properties properties = propertiesFactory.getPropertiesInstance();
|
||||
configs.stream().filter(config -> 0 == config.getDeleted()) // 过滤掉被删除的配置
|
||||
.forEach(config -> properties.put(config.getKey(), config.getValue()));
|
||||
@ -145,7 +143,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
|
||||
* @param maxUpdateTime 当前配置的最大更新时间
|
||||
* @return 配置列表
|
||||
*/
|
||||
private List<SysConfigDO> loadConfigIfUpdate(Date maxUpdateTime) {
|
||||
private List<InfConfigDO> loadConfigIfUpdate(Date maxUpdateTime) {
|
||||
// 第一步,判断是否要更新。
|
||||
boolean isUpdate = maxUpdateTime == null; // 如果更新时间为空,说明 DB 一定有新数据
|
||||
if (!isUpdate) {
|
||||
@ -159,12 +157,12 @@ public class DBConfigRepository extends AbstractConfigRepository {
|
||||
}
|
||||
|
||||
private boolean existsNewConfig(Date maxUpdateTime) {
|
||||
return jdbcTemplate.query("SELECT id FROM sys_config WHERE update_time > ? LIMIT 1",
|
||||
return jdbcTemplate.query("SELECT id FROM inf_config WHERE update_time > ? LIMIT 1",
|
||||
ResultSet::next, maxUpdateTime);
|
||||
}
|
||||
|
||||
private List<SysConfigDO> getSysConfigList() {
|
||||
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM sys_config", new BeanPropertyRowMapper<>(SysConfigDO.class));
|
||||
private List<InfConfigDO> getSysConfigList() {
|
||||
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM inf_config", new BeanPropertyRowMapper<>(InfConfigDO.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1 +1,13 @@
|
||||
/**
|
||||
* 配置中心客户端,基于 Apollo Client 进行简化
|
||||
*
|
||||
* 差别在于,我们使用 {@link cn.iocoder.dashboard.modules.infra.dal.mysql.dataobject.config.InfConfigDO} 表作为配置源。
|
||||
* 当然,功能肯定也会相对少些,满足最小化诉求。
|
||||
*
|
||||
* 1. 项目初始化时,可以使用 SysConfigDO 表的配置
|
||||
* 2. 使用 Spring @Value 可以注入属性
|
||||
* 3. SysConfigDO 表的配置修改时,注入到 @Value 的属性可以刷新
|
||||
*
|
||||
* 另外,整个包结构会参考 Apollo 为主,方便维护与理解
|
||||
*/
|
||||
package cn.iocoder.dashboard.framework.apollo;
|
||||
|
@ -8,6 +8,11 @@ import com.ctrip.framework.apollo.internals.ConfigRepository;
|
||||
import com.ctrip.framework.apollo.internals.DefaultConfig;
|
||||
import com.ctrip.framework.apollo.spi.ConfigFactory;
|
||||
|
||||
/**
|
||||
* 基于 DB 的 ConfigFactory 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class DBConfigFactory implements ConfigFactory {
|
||||
|
||||
@Override
|
||||
@ -21,7 +26,7 @@ public class DBConfigFactory implements ConfigFactory {
|
||||
}
|
||||
|
||||
private ConfigRepository createDBConfigRepository(String namespace) {
|
||||
return new DBConfigRepository(namespace); // TODO 芋艿:看看怎么优化
|
||||
return new DBConfigRepository(namespace);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
package cn.iocoder.dashboard.framework.apollo.spring.boot;
|
||||
|
||||
import cn.iocoder.dashboard.framework.apollo.core.ConfigConsts;
|
||||
import com.google.common.base.Strings;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* 对 {@link com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer} 的补充,目前的目的有:
|
||||
*
|
||||
* 1. 将自定义的 apollo.jdbc 设置到 System 变量中
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class ApolloApplicationContextInitializer implements EnvironmentPostProcessor, Ordered {
|
||||
|
||||
/**
|
||||
* 优先级更高,要早于 Apollo 的 ApolloApplicationContextInitializer 的初始化
|
||||
*/
|
||||
public static final int DEFAULT_ORDER = -1;
|
||||
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
private static final String[] APOLLO_SYSTEM_PROPERTIES = {ConfigConsts.APOLLO_JDBC_URL,
|
||||
ConfigConsts.APOLLO_JDBC_USERNAME, ConfigConsts.APOLLO_JDBC_PASSWORD};
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
initializeSystemProperty(environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* To fill system properties from environment config
|
||||
*/
|
||||
void initializeSystemProperty(ConfigurableEnvironment environment) {
|
||||
for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
|
||||
fillSystemPropertyFromEnvironment(environment, propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment, String propertyName) {
|
||||
if (System.getProperty(propertyName) != null) {
|
||||
return;
|
||||
}
|
||||
String propertyValue = environment.getProperty(propertyName);
|
||||
if (Strings.isNullOrEmpty(propertyValue)) {
|
||||
return;
|
||||
}
|
||||
System.setProperty(propertyName, propertyValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user