131 lines
2.8 KiB
Markdown
131 lines
2.8 KiB
Markdown
|
# 统一配置模块
|
||
|
|
||
|
## 内容介绍
|
||
|
|
||
|
- [统一配置模块](#统一配置模块)
|
||
|
- [内容介绍](#内容介绍)
|
||
|
- [关于](#关于)
|
||
|
- [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.
|