From 18fca9841f057a1fa5c52ceae2f0be00e4fbfd8e Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 8 Apr 2025 11:38:00 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=88=B7=E6=96=B0config.properties=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=85=8D=E7=BD=AE=E5=8F=98=E9=87=8F=E5=80=BC=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1394812148744192]增加一个刷新config.properties文件配置变量值接口 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1394812148744192 --- .../framework/common/config/Config.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/neatlogic/framework/common/config/Config.java b/src/main/java/neatlogic/framework/common/config/Config.java index 28f58e93b..6866666d4 100644 --- a/src/main/java/neatlogic/framework/common/config/Config.java +++ b/src/main/java/neatlogic/framework/common/config/Config.java @@ -527,17 +527,42 @@ public class Config { // } } + public boolean readProperties(Properties prop) { + try { + String propertiesString = configService.getConfig("config", "neatlogic.framework", 3000); + if (StringUtils.isNotBlank(propertiesString)) { + prop.load(new InputStreamReader(new ByteArrayInputStream(propertiesString.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); + System.out.println("⚡" + I18nUtils.getStaticMessage("common.startloadconfig", "Nacos")); + return true; + } else { + // 如果从nacos中读不出配置,则使用本地配置文件配置 + prop.load(new InputStreamReader(Objects.requireNonNull(Config.class.getClassLoader().getResourceAsStream(CONFIG_FILE)), StandardCharsets.UTF_8)); + System.out.println("⚡" + I18nUtils.getStaticMessage("common.startloadconfig", "config.properties")); + return false; + } + } catch (NacosException | IOException e) { + logger.error(e.getMessage(), e); + } + return false; + } + @PostConstruct public void init() { try { initConfigFile(); - String propertiesString = configService.getConfig("config", "neatlogic.framework", 3000); - loadNacosProperties(propertiesString); - if (StringUtils.isNotBlank(propertiesString)) { + Properties prop = new Properties(); + boolean flag = readProperties(prop); + if (flag) { configService.addListener("config", "neatlogic.framework", new Listener() { @Override public void receiveConfigInfo(String configInfo) { - loadNacosProperties(configInfo); + Properties properties = new Properties(); + try { + properties.load(new InputStreamReader(new ByteArrayInputStream(configInfo.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + loadNacosProperties(properties); } @Override @@ -546,22 +571,14 @@ public class Config { } }); } + loadNacosProperties(prop); } catch (NacosException e) { logger.error(e.getMessage(), e); } } - private static void loadNacosProperties(String configInfo) { + public static void loadNacosProperties(Properties prop) { try { - Properties prop = new Properties(); - if (StringUtils.isNotBlank(configInfo)) { - prop.load(new InputStreamReader(new ByteArrayInputStream(configInfo.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); - System.out.println("⚡" + I18nUtils.getStaticMessage("common.startloadconfig", "Nacos")); - } else { - // 如果从nacos中读不出配置,则使用本地配置文件配置 - prop.load(new InputStreamReader(Objects.requireNonNull(Config.class.getClassLoader().getResourceAsStream(CONFIG_FILE)), StandardCharsets.UTF_8)); - System.out.println("⚡" + I18nUtils.getStaticMessage("common.startloadconfig", "config.properties")); - } DATA_HOME = prop.getProperty("data.home", "/app/data"); AUDIT_HOME = prop.getProperty("audit.home"); SERVER_HEARTBEAT_RATE = Integer.parseInt(prop.getProperty("heartbeat.rate", "1")); @@ -644,7 +661,7 @@ public class Config { } } properties = prop; - } catch (IOException e) { + } catch (Exception e) { logger.error(e.getMessage(), e); } } -- Gitee