优化配置中心,增加 ConfigFrameworkDAO 的抽象,进一步解耦 framework

This commit is contained in:
YunaiV 2021-01-22 19:07:30 +08:00
parent 092ec983a3
commit 8e67753a4b
4 changed files with 604 additions and 58 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,28 @@
package cn.iocoder.dashboard.framework.apollo.internals;
import cn.iocoder.dashboard.modules.infra.dal.mysql.dataobject.config.InfConfigDO;
import java.util.Date;
import java.util.List;
/**
* 配置 Framework DAO 接口
*/
public interface ConfigFrameworkDAO {
/**
* 查询是否存在比 maxUpdateTime 更新记录更晚的配置
*
* @param maxUpdateTime 最大更新时间
* @return 是否存在
*/
boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime);
/**
* 查询配置列表
*
* @return 配置列表
*/
List<InfConfigDO> getSysConfigList();
}

View File

@ -3,6 +3,7 @@ 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.infra.dal.mysql.dao.config.InfConfigDAOImpl;
import cn.iocoder.dashboard.modules.infra.dal.mysql.dataobject.config.InfConfigDO;
import com.ctrip.framework.apollo.Apollo;
import com.ctrip.framework.apollo.build.ApolloInjector;
@ -14,12 +15,7 @@ import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.util.factory.PropertiesFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@ -51,9 +47,9 @@ public class DBConfigRepository extends AbstractConfigRepository {
private volatile Date maxUpdateTime;
/**
* Spring JDBC 操作模板
* 配置读取 DAO
*/
private final JdbcTemplate jdbcTemplate;
private final ConfigFrameworkDAO configFrameworkDAO;
public DBConfigRepository(String namespace) {
// 初始化变量
@ -61,9 +57,8 @@ public class DBConfigRepository extends AbstractConfigRepository {
this.propertiesFactory = ApolloInjector.getInstance(PropertiesFactory.class);
this.m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
// 初始化 DB
DataSource dataSource = new DriverManagerDataSource(System.getProperty(ConfigConsts.APOLLO_JDBC_URL),
this.configFrameworkDAO = new InfConfigDAOImpl(System.getProperty(ConfigConsts.APOLLO_JDBC_URL),
System.getProperty(ConfigConsts.APOLLO_JDBC_USERNAME), System.getProperty(ConfigConsts.APOLLO_JDBC_PASSWORD));
this.jdbcTemplate = new JdbcTemplate(dataSource);
// 初始化加载
this.trySync();
@ -84,6 +79,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
Properties newProperties = this.buildProperties(configs);
this.m_configCache = newProperties;
// 第三步获取最大的配置时间
assert configs.size() > 0; // 断言避免告警
this.maxUpdateTime = configs.stream().max(Comparator.comparing(BaseDO::getUpdateTime)).get().getUpdateTime();
// 第四部触发配置刷新重要
super.fireRepositoryChange(m_namespace, newProperties);
@ -145,24 +141,16 @@ public class DBConfigRepository extends AbstractConfigRepository {
*/
private List<InfConfigDO> loadConfigIfUpdate(Date maxUpdateTime) {
// 第一步判断是否要更新
boolean isUpdate = maxUpdateTime == null; // 如果更新时间为空说明 DB 一定有新数据
if (!isUpdate) {
isUpdate = this.existsNewConfig(maxUpdateTime); // 判断数据库中是否有更新的配置
}
if (!isUpdate) {
if (maxUpdateTime == null) { // 如果更新时间为空说明 DB 一定有新数据
log.info("[loadConfigIfUpdate][首次加载全量配置]");
} else { // 判断数据库中是否有更新的配置
if (!configFrameworkDAO.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
return null;
}
log.info("[loadConfigIfUpdate][增量加载全量配置]");
}
// 第二步如果有更新则从数据库加载所有配置
return this.getSysConfigList();
}
private boolean existsNewConfig(Date maxUpdateTime) {
return jdbcTemplate.query("SELECT id FROM inf_config WHERE update_time > ? LIMIT 1",
ResultSet::next, maxUpdateTime);
}
private List<InfConfigDO> getSysConfigList() {
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM inf_config", new BeanPropertyRowMapper<>(InfConfigDO.class));
return configFrameworkDAO.getSysConfigList();
}
}

View File

@ -0,0 +1,39 @@
package cn.iocoder.dashboard.modules.infra.dal.mysql.dao.config;
import cn.iocoder.dashboard.framework.apollo.internals.ConfigFrameworkDAO;
import cn.iocoder.dashboard.modules.infra.dal.mysql.dataobject.config.InfConfigDO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
/**
* ConfigFrameworkDAO 实现类
*
* @author 芋道源码
*/
public class InfConfigDAOImpl implements ConfigFrameworkDAO {
private final JdbcTemplate jdbcTemplate;
public InfConfigDAOImpl(String jdbcUrl, String username, String password) {
DataSource dataSource = new DriverManagerDataSource(jdbcUrl, username, password);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
return jdbcTemplate.query("SELECT id FROM inf_config WHERE update_time > ? LIMIT 1",
ResultSet::next, maxUpdateTime);
}
@Override
public List<InfConfigDO> getSysConfigList() {
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM inf_config", new BeanPropertyRowMapper<>(InfConfigDO.class));
}
}