From d7ab8efa32e347dd0e18a4dc2365133b0efae0ec Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 20 Jun 2022 01:05:30 +0800 Subject: [PATCH] fix some bug --- pom.xml | 14 +++++ src/main/java/com/yuandian/common/Config.java | 62 +++++++++++++------ src/main/resources/application.properties | 6 +- src/main/resources/logback.xml | 13 ++++ .../java/com/yuandian/common/ConfigTest.java | 1 + 5 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/logback.xml diff --git a/pom.xml b/pom.xml index 69aff39..d86f9ea 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ 17 2.1.0 1.30 + 1.2.11 1.7.36 @@ -24,6 +25,19 @@ test + + + ch.qos.logback + logback-classic + ${logback.version} + + + + ch.qos.logback + logback-core + ${logback.version} + + org.slf4j slf4j-api diff --git a/src/main/java/com/yuandian/common/Config.java b/src/main/java/com/yuandian/common/Config.java index 8a41649..4a4585a 100644 --- a/src/main/java/com/yuandian/common/Config.java +++ b/src/main/java/com/yuandian/common/Config.java @@ -35,16 +35,16 @@ import lombok.extern.slf4j.Slf4j; @Getter @Setter public class Config { + /** 加载的配置文件名, 在{@docRoot}/resources下 */ public static String DEFAULT_CONFIT_FILE = "application.properties"; - /** properties 的配置key. nacos地址 */ - public static String DEFAULT_CONFIG_ADDR = "yuandian.dataflow.config.nacos.server.addr"; - // 默认 public static String DEFAULT_GROUP_DATAID = "yuandian.dataflow"; // 所有生成的nacos客户端 private static HashMap configDict = new HashMap<>(); + /** properties 的配置key. nacos地址 */ + private String DEFAULT_CONFIG_ADDR = "yuandian.dataflow.config.nacos.server.addr"; // 配置的所有值主类 public Map data; // nacos地址 @@ -59,51 +59,61 @@ public class Config { private ConfigService configService; private Config(String GroupAndDataId) throws Exception { + String[] gad = GroupAndDataId.split("\\."); if (gad.length != 2) { throw new Exception("Group 或者 DataId 不能存在 '.' 的命令"); } - this.group = gad[0] + ENV_TEST; - this.dataId = gad[1] + ENV_TEST; + this.group = gad[0]; + this.dataId = gad[1]; + + if(ENV_TEST != null) { + DEFAULT_CONFIG_ADDR += ENV_TEST; + this.group += ENV_TEST; + this.dataId += ENV_TEST; + } + connect(); } /** * 连接nacos - * - * @throws IOException - * @throws NacosException + * @throws Exception */ - private void connect() throws IOException, NacosException { + private void connect() throws Exception { if (configService != null) { configService.shutDown(); configService = null; } // 获取 app Properties prop = new Properties(); - prop.load(Config.class.getClassLoader().getResourceAsStream(DEFAULT_CONFIT_FILE)); - - serverAddr = trim(prop.getProperty(DEFAULT_CONFIG_ADDR + ENV_TEST), "\" '"); + prop.load(this.getClass().getClassLoader().getResourceAsStream(DEFAULT_CONFIT_FILE)); + serverAddr = trim(prop.getProperty(DEFAULT_CONFIG_ADDR), "\" '"); + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); configService = NacosFactory.createConfigService(properties); - + String content = configService.getConfig(dataId, group, 5000); - Yaml yaml = new Yaml(); - data = yaml.load(content); - log.info(content); + if(content == null) { + throw new Exception( String.format("content is null. maybe timeout or the error of addr %s", serverAddr)); + } + + data = new Yaml().load(content); + log.debug(content); datalock = new ReentrantLock(); // 监听 配置更新事件 configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { - log.debug("recieve:" + configInfo); + // log.debug("recieve:" + configInfo); try { datalock.lock(); data = (Map) new Yaml().load(configInfo); - log.debug("{}", data); + // log.debug("{}", data); } finally { datalock.unlock(); } @@ -221,7 +231,7 @@ public class Config { return configService.publishConfig(dataId, group, new Yaml().dumpAsMap(data)); } - public static String ENV_TEST = ""; + public static String ENV_TEST = null; /** * 统一使用配置的入口函数 线程安全 @@ -301,4 +311,18 @@ public class Config { } return trimedstr.substring(start, end + 1); } + + + public static void main(String[] args) throws Exception { + try { + Config.UseConfig((cnf)->{ + log.info("{}", cnf); + return null; + }); + } catch (Exception e) { + //TODO: handle exception + e.printStackTrace(); + } + + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ac95d63..d3a2729 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,4 @@ server.port=3440 -yuandian.dataflow.config.nacos.server.addr=localhost:8848 -yuandian.dataflow.config.nacos.server.addr-test=localhost:8848 - - \ No newline at end of file +yuandian.dataflow.config.nacos.server.addr=config.yuandian.local:8848 +yuandian.dataflow.config.nacos.server.addr-test=config.yuandian.local:8848 \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..615c996 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{yyyyMMdd HH:mm:ss.SSS} %-5level[%thread\(%file:%line\)]: %msg%n + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/yuandian/common/ConfigTest.java b/src/test/java/com/yuandian/common/ConfigTest.java index 9dc3d3d..d9cb288 100644 --- a/src/test/java/com/yuandian/common/ConfigTest.java +++ b/src/test/java/com/yuandian/common/ConfigTest.java @@ -111,6 +111,7 @@ public class ConfigTest { @Test @Order(1) void testLabelConfig() throws Exception { + Config.ENV_TEST = "-test"; Config.UseConfig("org.fortest", (cnf)->{ log.info("{}", cnf.get("test")); Assertions.assertEquals(cnf.get("test"), "groupAndDataId");