fix some bug
This commit is contained in:
parent
8295e36210
commit
d7ab8efa32
14
pom.xml
14
pom.xml
|
@ -13,6 +13,7 @@
|
|||
<java.version>17</java.version>
|
||||
<nacos.version>2.1.0</nacos.version>
|
||||
<snakeyaml.version>1.30</snakeyaml.version>
|
||||
<logback.version>1.2.11</logback.version>
|
||||
<slf4j.version>1.7.36</slf4j.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
@ -24,6 +25,19 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
|
|
@ -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<String, Config> configDict = new HashMap<>();
|
||||
|
||||
/** properties 的配置key. nacos地址 */
|
||||
private String DEFAULT_CONFIG_ADDR = "yuandian.dataflow.config.nacos.server.addr";
|
||||
// 配置的所有值主类
|
||||
public Map<String, Object> 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<String, Object>) 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
server.port=3440
|
||||
|
||||
yuandian.dataflow.config.nacos.server.addr=localhost:8848
|
||||
yuandian.dataflow.config.nacos.server.addr-test=localhost:8848
|
||||
|
||||
|
||||
yuandian.dataflow.config.nacos.server.addr=config.yuandian.local:8848
|
||||
yuandian.dataflow.config.nacos.server.addr-test=config.yuandian.local:8848
|
13
src/main/resources/logback.xml
Normal file
13
src/main/resources/logback.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<configuration>
|
||||
<appender name="CONSOLE"
|
||||
class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>
|
||||
%d{yyyyMMdd HH:mm:ss.SSS} %-5level[%thread\(%file:%line\)]: %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="INFO">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue
Block a user