添加README.md说明

This commit is contained in:
eson 2022-06-20 12:09:51 +08:00
parent d7ab8efa32
commit 6abe852338
3 changed files with 205 additions and 10 deletions

130
README.md Normal file
View File

@ -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
<dependency>
<groupId>com.yuandian.common</groupId>
<artifactId>config</artifactId>
<version>${具体版本}</version>
</dependency>
```
### 使用例子
#### 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.

View File

@ -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;

View File

@ -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");