From 6abe852338980542c7c458d6d0a915e3cb9efd4d Mon Sep 17 00:00:00 2001 From: eson Date: Mon, 20 Jun 2022 12:09:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0README.md=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 130 ++++++++++++++++++ src/main/java/com/yuandian/common/Config.java | 4 +- .../java/com/yuandian/common/ConfigTest.java | 81 +++++++++-- 3 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6374fe8 --- /dev/null +++ b/README.md @@ -0,0 +1,130 @@ +# 统一配置模块 + +## 内容介绍 + +- [统一配置模块](#统一配置模块) + - [内容介绍](#内容介绍) + - [关于](#关于) + - [Getting Started](#getting-started) + - [预需要](#预需要) + - [使用例子](#使用例子) + - [get方法](#get方法) + - [set方法](#set方法) + - [createKeys方法](#createkeys方法) + - [remove方法](#remove方法) + - [非默认group.dataId方法](#非默认groupdataid方法) + - [Usage](#usage) + +## 关于 + +* 由于历史项目配置混乱, 循环去mysql表查询配置不合理. +* 重写一个基于nacos配置模块 + +## Getting Started + +### 预需要 + +``` +安装nacos. java项目resources文件夹里 application.properties 里配置 +``` + +``` +yuandian.dataflow.config.nacos.server.addr=config.yuandian.local:8848 +``` + +在pom.xml文件里添加依赖 +```pom + + com.yuandian.common + config + ${具体版本} + +``` + +### 使用例子 + +#### get方法 +```java +Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * key1: + * key2: + * value + */ + log.info("{}",cnf.get("key1", "key2")); //value + return null; +}); +``` + +#### set方法 +```java +Config.UseConfig((cnf) -> { + cnf.seek("key1", "key2").set("do_set"); + try { + cnf.update(); // 每个写操作都需要执行 update() + } catch (NacosException e) { + e.printStackTrace(); + } + /** + * nacos config 默认 group=yuandian dataId=dataflow + * key1: + * key2: + * do_set + */ + return null; +}); +``` + + +#### createKeys方法 +```java +Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * create_keys: do_set + */ + cnf.seek("create_keys").set("do_set"); + try { + cnf.update(); // 每个写操作都需要执行 update() + } catch (NacosException e) { + e.printStackTrace(); + } + return null; +}); +``` + +#### remove方法 +```java +Config.UseConfig((cnf) -> { + cnf.remove("create1", "create2"); + try { + log.info("{}",cnf.update()); // 所有增加删除操作要最后同步到nacos. 都需要update + } catch (NacosException e) { + e.printStackTrace(); + } + cnf.remove("create1"); + try { + log.info("{}",cnf.update()); // 所有增加删除操作要最后同步到nacos. 都需要update + } catch (NacosException e) { + e.printStackTrace(); + } + return null; +}); +``` + +#### 非默认group.dataId方法 +```java +Config.UseConfig("org.fortest", (cnf)->{ + /** + * nacos config group=org dataId=fortest + * test: groupAndDataId + */ + log.info("{}",cnf.get("test")); // groupAndDataId + return null; +}); +``` + +## Usage + +Add notes about how to use the system. diff --git a/src/main/java/com/yuandian/common/Config.java b/src/main/java/com/yuandian/common/Config.java index 4a4585a..478f659 100644 --- a/src/main/java/com/yuandian/common/Config.java +++ b/src/main/java/com/yuandian/common/Config.java @@ -6,7 +6,6 @@ */ package com.yuandian.common; -import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -15,12 +14,15 @@ import java.util.concurrent.Executor; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; + import org.yaml.snakeyaml.Yaml; + import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; + import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/com/yuandian/common/ConfigTest.java b/src/test/java/com/yuandian/common/ConfigTest.java index d9cb288..96ab104 100644 --- a/src/test/java/com/yuandian/common/ConfigTest.java +++ b/src/test/java/com/yuandian/common/ConfigTest.java @@ -1,30 +1,93 @@ package com.yuandian.common; import java.time.Instant; + import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; + import com.alibaba.nacos.api.exception.NacosException; -import com.yuandian.common.Config; import lombok.extern.slf4j.Slf4j; @Slf4j @TestMethodOrder(OrderAnnotation.class) public class ConfigTest { + + + @BeforeAll + static void beforeClass() { + Config.ENV_TEST = "-test"; + } + + @Test + @Order(1) + void testConfigGet() throws Exception { + Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * key1: + * key2: + * path_value + */ + log.info("{}",cnf.get("key1", "key2")); + return null; + }); + } + + @Test + @Order(1) + void testConfigSet() throws Exception { + Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * key1: + * key2: + * path_value + */ + cnf.seek("key1", "key2").set("do_set"); + try { + cnf.update(); // 每个写操作都需要执行 update() + } catch (NacosException e) { + e.printStackTrace(); + } + return null; + }); + } + + @Test + @Order(1) + void testConfigCreateKeys() throws Exception { + Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * create_keys: do_set + */ + cnf.seek("create_keys").set("do_set"); + try { + cnf.update(); // 每个写操作都需要执行 update() + } catch (NacosException e) { + e.printStackTrace(); + } + return null; + }); + } + + /** * 测试配置基础用法 * @throws Exception */ @Test - @Order(1) + @Order(2) void testUseConfig() throws Exception { - Config.ENV_TEST = "-test"; + Config.UseConfig((cnf) -> { log.info("{}",cnf.data); - Assertions.assertEquals(cnf.get("key1", "key2"), "key_path"); + Assertions.assertEquals(cnf.get("key1", "key2"), "do_set"); Instant now = Instant.now(); cnf.data.put("use_config", now.toString()); @@ -42,7 +105,7 @@ public class ConfigTest { @Test @Order(1) void testUseConfigUpdate() throws Exception { - Config.ENV_TEST = "-test"; + Config.UseConfig((cnf) -> { cnf.seek("create1","create2", "create3").createKeys().set("create_for_remove");; @@ -59,7 +122,7 @@ public class ConfigTest { @Test @Order(2) void testRemove() throws Exception { - Config.ENV_TEST = "-test"; + Config.UseConfig((cnf) -> { cnf.remove("create1", "create2"); try { @@ -79,7 +142,7 @@ public class ConfigTest { @Test @Order(3) void testCreateKeys() throws Exception { - Config.ENV_TEST = "-test"; + Config.UseConfig((cnf) -> { log.info("{}",cnf.data); @@ -96,7 +159,7 @@ public class ConfigTest { @Test @Order(1) void testUpdate() throws Exception { - Config.ENV_TEST = "-test"; + Config.UseConfig((cnf) -> { Instant now = Instant.now(); cnf.data.put("use_config", now.toString()); @@ -111,7 +174,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");