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>
|
<java.version>17</java.version>
|
||||||
<nacos.version>2.1.0</nacos.version>
|
<nacos.version>2.1.0</nacos.version>
|
||||||
<snakeyaml.version>1.30</snakeyaml.version>
|
<snakeyaml.version>1.30</snakeyaml.version>
|
||||||
|
<logback.version>1.2.11</logback.version>
|
||||||
<slf4j.version>1.7.36</slf4j.version>
|
<slf4j.version>1.7.36</slf4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -24,6 +25,19 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
@ -35,16 +35,16 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
/** 加载的配置文件名, 在{@docRoot}/resources下 */
|
/** 加载的配置文件名, 在{@docRoot}/resources下 */
|
||||||
public static String DEFAULT_CONFIT_FILE = "application.properties";
|
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";
|
public static String DEFAULT_GROUP_DATAID = "yuandian.dataflow";
|
||||||
// 所有生成的nacos客户端
|
// 所有生成的nacos客户端
|
||||||
private static HashMap<String, Config> configDict = new HashMap<>();
|
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;
|
public Map<String, Object> data;
|
||||||
// nacos地址
|
// nacos地址
|
||||||
@ -59,51 +59,61 @@ public class Config {
|
|||||||
private ConfigService configService;
|
private ConfigService configService;
|
||||||
|
|
||||||
private Config(String GroupAndDataId) throws Exception {
|
private Config(String GroupAndDataId) throws Exception {
|
||||||
|
|
||||||
String[] gad = GroupAndDataId.split("\\.");
|
String[] gad = GroupAndDataId.split("\\.");
|
||||||
if (gad.length != 2) {
|
if (gad.length != 2) {
|
||||||
throw new Exception("Group 或者 DataId 不能存在 '.' 的命令");
|
throw new Exception("Group 或者 DataId 不能存在 '.' 的命令");
|
||||||
}
|
}
|
||||||
this.group = gad[0] + ENV_TEST;
|
this.group = gad[0];
|
||||||
this.dataId = gad[1] + ENV_TEST;
|
this.dataId = gad[1];
|
||||||
|
|
||||||
|
if(ENV_TEST != null) {
|
||||||
|
DEFAULT_CONFIG_ADDR += ENV_TEST;
|
||||||
|
this.group += ENV_TEST;
|
||||||
|
this.dataId += ENV_TEST;
|
||||||
|
}
|
||||||
|
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接nacos
|
* 连接nacos
|
||||||
*
|
* @throws Exception
|
||||||
* @throws IOException
|
|
||||||
* @throws NacosException
|
|
||||||
*/
|
*/
|
||||||
private void connect() throws IOException, NacosException {
|
private void connect() throws Exception {
|
||||||
if (configService != null) {
|
if (configService != null) {
|
||||||
configService.shutDown();
|
configService.shutDown();
|
||||||
configService = null;
|
configService = null;
|
||||||
}
|
}
|
||||||
// 获取 app
|
// 获取 app
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
prop.load(Config.class.getClassLoader().getResourceAsStream(DEFAULT_CONFIT_FILE));
|
prop.load(this.getClass().getClassLoader().getResourceAsStream(DEFAULT_CONFIT_FILE));
|
||||||
|
|
||||||
serverAddr = trim(prop.getProperty(DEFAULT_CONFIG_ADDR + ENV_TEST), "\" '");
|
serverAddr = trim(prop.getProperty(DEFAULT_CONFIG_ADDR), "\" '");
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
|
|
||||||
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
||||||
configService = NacosFactory.createConfigService(properties);
|
configService = NacosFactory.createConfigService(properties);
|
||||||
|
|
||||||
String content = configService.getConfig(dataId, group, 5000);
|
String content = configService.getConfig(dataId, group, 5000);
|
||||||
Yaml yaml = new Yaml();
|
if(content == null) {
|
||||||
data = yaml.load(content);
|
throw new Exception( String.format("content is null. maybe timeout or the error of addr %s", serverAddr));
|
||||||
log.info(content);
|
}
|
||||||
|
|
||||||
|
data = new Yaml().load(content);
|
||||||
|
log.debug(content);
|
||||||
|
|
||||||
datalock = new ReentrantLock();
|
datalock = new ReentrantLock();
|
||||||
// 监听 配置更新事件
|
// 监听 配置更新事件
|
||||||
configService.addListener(dataId, group, new Listener() {
|
configService.addListener(dataId, group, new Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void receiveConfigInfo(String configInfo) {
|
public void receiveConfigInfo(String configInfo) {
|
||||||
log.debug("recieve:" + configInfo);
|
// log.debug("recieve:" + configInfo);
|
||||||
try {
|
try {
|
||||||
datalock.lock();
|
datalock.lock();
|
||||||
data = (Map<String, Object>) new Yaml().load(configInfo);
|
data = (Map<String, Object>) new Yaml().load(configInfo);
|
||||||
log.debug("{}", data);
|
// log.debug("{}", data);
|
||||||
} finally {
|
} finally {
|
||||||
datalock.unlock();
|
datalock.unlock();
|
||||||
}
|
}
|
||||||
@ -221,7 +231,7 @@ public class Config {
|
|||||||
return configService.publishConfig(dataId, group, new Yaml().dumpAsMap(data));
|
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);
|
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
|
server.port=3440
|
||||||
|
|
||||||
yuandian.dataflow.config.nacos.server.addr=localhost:8848
|
yuandian.dataflow.config.nacos.server.addr=config.yuandian.local:8848
|
||||||
yuandian.dataflow.config.nacos.server.addr-test=localhost: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
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
void testLabelConfig() throws Exception {
|
void testLabelConfig() throws Exception {
|
||||||
|
Config.ENV_TEST = "-test";
|
||||||
Config.UseConfig("org.fortest", (cnf)->{
|
Config.UseConfig("org.fortest", (cnf)->{
|
||||||
log.info("{}", cnf.get("test"));
|
log.info("{}", cnf.get("test"));
|
||||||
Assertions.assertEquals(cnf.get("test"), "groupAndDataId");
|
Assertions.assertEquals(cnf.get("test"), "groupAndDataId");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user