在线热更新配置
This commit is contained in:
@@ -17,8 +17,13 @@ import org.xyzh.common.core.page.PageDomain;
|
||||
|
||||
import org.xyzh.common.utils.id.IdUtil;
|
||||
import org.xyzh.common.utils.StringUtils;
|
||||
import org.xyzh.common.redis.service.RedisService;
|
||||
import org.xyzh.api.system.constance.SysConfigRedisPrefix;
|
||||
import org.xyzh.system.mapper.config.TbSysConfigMapper;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @description 系统配置服务实现类
|
||||
* @filename SysConfigServiceImpl.java
|
||||
@@ -41,6 +46,37 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
@Resource
|
||||
private TbSysConfigMapper configMapper;
|
||||
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
/** 异步发送事件的线程池 */
|
||||
private final ExecutorService eventExecutor = Executors.newSingleThreadExecutor(r -> {
|
||||
Thread t = new Thread(r, "sys-config-event");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
|
||||
/**
|
||||
* 异步发布配置变更事件
|
||||
* @param group 配置分组
|
||||
*/
|
||||
private void publishConfigChangeEvent(String group) {
|
||||
if (StringUtils.isBlank(group)) {
|
||||
return;
|
||||
}
|
||||
eventExecutor.execute(() -> {
|
||||
try {
|
||||
String channel = SysConfigRedisPrefix.getChannelByGroup(group);
|
||||
if (channel != null) {
|
||||
redisService.publish(channel, System.currentTimeMillis());
|
||||
logger.info("配置变更事件发布成功: channel={}", channel);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("配置变更事件发布失败: group={}", group, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key查询配置
|
||||
*/
|
||||
@@ -242,6 +278,8 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
int rows = configMapper.insertConfig(configDTO);
|
||||
if (rows > 0) {
|
||||
logger.info("新增配置成功, configId={}", configDTO.getConfigId());
|
||||
// 异步发布配置变更事件
|
||||
publishConfigChangeEvent(configDTO.getGroup());
|
||||
return ResultDomain.success("新增配置成功", configDTO);
|
||||
}
|
||||
logger.warn("新增配置失败, configId={}", configDTO.getConfigId());
|
||||
@@ -257,6 +295,8 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
int rows = configMapper.updateConfig(configDTO);
|
||||
if (rows > 0) {
|
||||
logger.info("更新配置成功, configId={}", configDTO.getConfigId());
|
||||
// 异步发布配置变更事件
|
||||
publishConfigChangeEvent(configDTO.getGroup());
|
||||
return ResultDomain.success("更新配置成功", configDTO);
|
||||
}
|
||||
logger.warn("更新配置失败, configId={}", configDTO.getConfigId());
|
||||
@@ -271,6 +311,8 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
int rows = configMapper.deleteConfig(configDTO);
|
||||
if (rows > 0) {
|
||||
logger.info("删除配置成功, configId={}", configDTO.getConfigId());
|
||||
// 异步发布配置变更事件
|
||||
publishConfigChangeEvent(configDTO.getGroup());
|
||||
return ResultDomain.success("删除配置成功", Boolean.TRUE);
|
||||
}
|
||||
logger.warn("删除配置失败, configId={}", configDTO.getConfigId());
|
||||
|
||||
Reference in New Issue
Block a user