多语言-配置文件外置独立的命名空间-I18nMessageNacosService-独立管理configService,不在全局nacosConfigManager托管

This commit is contained in:
khalil
2025-06-25 14:06:22 +08:00
parent 905dd5b886
commit 46cc8d45e3
2 changed files with 15 additions and 16 deletions

View File

@@ -57,7 +57,7 @@ public class I18nMessageNacosAutoConfig {
@SneakyThrows @SneakyThrows
private void initTip(Locale locale, String content) { private void initTip(Locale locale, String content) {
NacosConfigProperties i18nNacosConfig = messageI18nNacosService.getI18nNacosConfigManager().getNacosConfigProperties(); NacosConfigProperties i18nNacosConfig = messageI18nNacosService.getI18nNacosServiceConfig();
log.info("开始初始化国际化配置! 语言 {} Nacos地址:{} 提示语命名空间:{}", log.info("开始初始化国际化配置! 语言 {} Nacos地址:{} 提示语命名空间:{}",
locale.getLanguage(), i18nNacosConfig.getServerAddr(), i18nNacosConfig.getNamespace()); locale.getLanguage(), i18nNacosConfig.getServerAddr(), i18nNacosConfig.getNamespace());
@@ -70,7 +70,7 @@ public class I18nMessageNacosAutoConfig {
dataId = dataIdSb.toString(); dataId = dataIdSb.toString();
if (null == content){ if (null == content){
content = messageI18nNacosService.getI18nNacosConfigManager().getConfigService().getConfigAndSignListener(dataId, i18nNacosConfig.getGroup(), 5000, content = messageI18nNacosService.getI18nNacosConfigService().getConfigAndSignListener(dataId, i18nNacosConfig.getGroup(), 5000,
new Listener() { new Listener() {
@Override @Override
public void receiveConfigInfo(String configInfo) { public void receiveConfigInfo(String configInfo) {

View File

@@ -1,10 +1,12 @@
package com.accompany.core.service; package com.accompany.core.service;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.accompany.core.base.SpringContextHolder;
import com.accompany.core.config.I18nMessageConfig; import com.accompany.core.config.I18nMessageConfig;
import com.accompany.core.config.I18nMessageNacosAutoConfig;
import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -37,28 +39,23 @@ public class MessageI18nNacosService implements InitializingBean {
private NacosConfigManager nacosConfigManager; private NacosConfigManager nacosConfigManager;
@Autowired @Autowired
private I18nMessageConfig i18nMessageConfig; private I18nMessageConfig i18nMessageConfig;
@Autowired
private I18nMessageNacosAutoConfig i18nMessageNacosAutoConfig;
@Getter @Getter
private NacosConfigManager i18nNacosConfigManager; private NacosConfigProperties i18nNacosServiceConfig;
@Getter
private ConfigService i18nNacosConfigService;
/** /**
* 国际化英文配置属性文件 * 国际化英文配置属性文件
*/ */
public static final String MESSAGES_EN_CONF_DATA_ID = "messages_en.properties"; public static final String MESSAGES_EN_CONF_DATA_ID = "messages_en.properties";
/** /**
* 国际化阿拉伯语语配置属性文件 * 国际化阿拉伯语语配置属性文件
*/ */
public static final String MESSAGES_AR_CONF_DATA_ID = "messages_ar.properties"; public static final String MESSAGES_AR_CONF_DATA_ID = "messages_ar.properties";
/** /**
* 国际化中文配置属性文件 * 国际化中文配置属性文件
*/ */
public static final String MESSAGES_ZH_CONF_DATA_ID = "messages_zh.properties"; public static final String MESSAGES_ZH_CONF_DATA_ID = "messages_zh.properties";
/** /**
* 国际化中文配置属性文件 * 国际化中文配置属性文件
*/ */
@@ -77,7 +74,7 @@ public class MessageI18nNacosService implements InitializingBean {
@SneakyThrows @SneakyThrows
public boolean saveOrUpdateProperties(String key, String value, String messageId) { public boolean saveOrUpdateProperties(String key, String value, String messageId) {
// 创建Nacos配置服务实例 // 创建Nacos配置服务实例
String content = i18nNacosConfigManager.getConfigService().getConfig(messageId, GROUP, 5000); String content = i18nNacosConfigService.getConfig(messageId, GROUP, 5000);
Properties properties = new Properties(); Properties properties = new Properties();
properties.load(new StringReader(content)); properties.load(new StringReader(content));
// 修改属性值 // 修改属性值
@@ -85,7 +82,7 @@ public class MessageI18nNacosService implements InitializingBean {
String newContent = propertiesToString(properties); String newContent = propertiesToString(properties);
//i18nMessageNacosAutoConfig.saveAsFileWriter(messageId, newContent); //i18nMessageNacosAutoConfig.saveAsFileWriter(messageId, newContent);
// 更新配置 // 更新配置
return i18nNacosConfigManager.getConfigService().publishConfig( return i18nNacosConfigService.publishConfig(
messageId, messageId,
GROUP, GROUP,
newContent); newContent);
@@ -94,7 +91,7 @@ public class MessageI18nNacosService implements InitializingBean {
@SneakyThrows @SneakyThrows
public Properties getProperties(String messageId) { public Properties getProperties(String messageId) {
// 创建Nacos配置服务实例 // 创建Nacos配置服务实例
String content = i18nNacosConfigManager.getConfigService().getConfig(messageId, GROUP, 5000); String content = i18nNacosConfigService.getConfig(messageId, GROUP, 5000);
Properties properties = new Properties(); Properties properties = new Properties();
properties.load(new StringReader(content)); properties.load(new StringReader(content));
return properties; return properties;
@@ -142,11 +139,13 @@ public class MessageI18nNacosService implements InitializingBean {
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
NacosConfigProperties i18nNacosServiceConfig = new NacosConfigProperties(); i18nNacosServiceConfig = new NacosConfigProperties();
BeanUtils.copyProperties(nacosConfigManager.getNacosConfigProperties(), i18nNacosServiceConfig); BeanUtils.copyProperties(nacosConfigManager.getNacosConfigProperties(), i18nNacosServiceConfig);
i18nNacosServiceConfig.setNamespace(i18nMessageConfig.getNameSpace()); i18nNacosServiceConfig.setNamespace(i18nMessageConfig.getNameSpace());
i18nNacosServiceConfig.setFileExtension(null);
i18nNacosServiceConfig.setSharedConfigs(null);
i18nNacosConfigManager = new NacosConfigManager(i18nNacosServiceConfig); i18nNacosConfigService = NacosFactory.createConfigService(i18nNacosServiceConfig.assembleConfigServiceProperties());
} }
} }