TODO: 解决配置 一体问题
This commit is contained in:
parent
3d7ffe0c52
commit
b7a5ea5570
9
pom.xml
9
pom.xml
|
@ -25,6 +25,7 @@
|
|||
<spring.boot.web>2.7.0</spring.boot.web>
|
||||
<mongo.driver.version>3.12.11</mongo.driver.version>
|
||||
<nacos.version>2.1.0</nacos.version>
|
||||
<snakeyaml.version>1.30</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -50,6 +51,14 @@
|
|||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>jraft-core</artifactId>
|
||||
|
|
|
@ -50,6 +50,7 @@ import com.alipay.sofa.jraft.option.ReplicatorGroupOptions;
|
|||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@SpringBootConfiguration
|
||||
public class Server {
|
||||
|
||||
@Autowired
|
||||
|
@ -65,6 +66,7 @@ public class Server {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String[] peers = new String[]{"localhost:4440","localhost:4441","localhost:4442"};
|
||||
|
|
|
@ -9,11 +9,25 @@ package com.yuandian.dataflow.config;
|
|||
|
||||
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringBufferInputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.system.SystemProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
@ -21,41 +35,62 @@ import com.alibaba.nacos.api.config.listener.Listener;
|
|||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.google.api.client.http.InputStreamContent;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* description
|
||||
*
|
||||
* @author eson
|
||||
*2022年6月13日-17:08:46
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@Setter
|
||||
public class Config {
|
||||
|
||||
private Config() throws NacosException, IOException {
|
||||
String serverAddr = "192.168.1.113:8848";
|
||||
String dataId = "dataflow";
|
||||
String group = "yuandian";
|
||||
|
||||
|
||||
|
||||
public Map<String,Object> configMap;
|
||||
|
||||
|
||||
@Value("${yuandian.dataflow.config.nacos.server.addr}")
|
||||
public String serverAddr ;
|
||||
public String dataId ;
|
||||
public String group ;
|
||||
|
||||
private static Config config;
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
private Config() throws NacosException, IOException {
|
||||
|
||||
|
||||
|
||||
Properties prop = new Properties();
|
||||
|
||||
|
||||
|
||||
serverAddr = prop.getProperty("yuandian.dataflow.config.nacos.server.addr");
|
||||
dataId = prop.getProperty("yuandian.dataflow.config.nacos.dataid") ;
|
||||
group = prop.getProperty("yuandian.dataflow.config.nacos.group");
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
||||
ConfigService configService = NacosFactory.createConfigService(properties);
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
|
||||
String content = configService.getConfig(dataId, group, 5000);
|
||||
|
||||
|
||||
|
||||
Prop.load(new StringReader(content));
|
||||
|
||||
System.out.println(cnf);
|
||||
Yaml yaml = new Yaml();
|
||||
configMap = yaml.load(content);
|
||||
log.info(content);
|
||||
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
System.out.println("recieve:" + configInfo);
|
||||
try {
|
||||
Prop.load(new StringReader(configInfo));
|
||||
System.out.println(cnf);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
synchronized(configMap){
|
||||
configMap = (Map<String, Object>)new Yaml().load(configInfo);
|
||||
System.out.println(configMap);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,60 +99,127 @@ public class Config {
|
|||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// boolean isPublishOk = configService.publishConfig(dataId, group, Prop.toString());
|
||||
// System.out.println(isPublishOk);
|
||||
|
||||
// Thread.sleep(3000);
|
||||
// content = configService.getConfig(dataId, group, 5000);
|
||||
// System.out.println(content);
|
||||
|
||||
// boolean isRemoveOk = configService.removeConfig(dataId, group);
|
||||
// System.out.println(isRemoveOk);
|
||||
// Thread.sleep(3000);
|
||||
|
||||
// content = configService.getConfig(dataId, group, 5000);
|
||||
// System.out.println(content);
|
||||
// Thread.sleep(300000);
|
||||
}
|
||||
|
||||
public Properties Prop ;
|
||||
/**
|
||||
* 更新配置
|
||||
* @return 返回是否发布成功.
|
||||
* @throws NacosException
|
||||
*/
|
||||
public Boolean update() throws NacosException {
|
||||
return configService.publishConfig(dataId, group,new Yaml().dumpAsMap(configMap));
|
||||
}
|
||||
|
||||
private static Config cnf ;
|
||||
public Config(Properties prop) {
|
||||
Prop = prop;
|
||||
}
|
||||
|
||||
public static Config GetConfig() throws NacosException, IOException {
|
||||
if(cnf == null) {
|
||||
cnf = new Config();
|
||||
}
|
||||
return cnf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param execute 统一使用配置的入口
|
||||
* @return 函数返回的值, 如果不需要直接返回null
|
||||
* @throws NacosException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Object UseConfig(Function<Config, Object> execute) throws NacosException, IOException {
|
||||
if(config == null) {
|
||||
config = new Config();
|
||||
}
|
||||
synchronized(config) {
|
||||
return execute.apply(config);
|
||||
}
|
||||
}
|
||||
// public Properties Prop ;
|
||||
|
||||
// private static Config cnf ;
|
||||
// public Config(Properties prop) {
|
||||
// Prop = prop;
|
||||
// }
|
||||
|
||||
// public static Config GetConfig() throws NacosException, IOException {
|
||||
// if(cnf == null) {
|
||||
// cnf = new Config();
|
||||
// }
|
||||
// return cnf;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) throws NacosException, InterruptedException, IOException {
|
||||
String serverAddr = "192.168.1.113:8848";
|
||||
String dataId = "dataflow";
|
||||
String group = "yuandian";
|
||||
// String serverAddr = "192.168.1.113:8848";
|
||||
// String dataId = "dataflow";
|
||||
// String group = "yuandian";
|
||||
|
||||
// Properties properties = new Properties();
|
||||
// properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
||||
// ConfigService configService = NacosFactory.createConfigService(properties);
|
||||
|
||||
// String content = configService.getConfig(dataId, group, 5000);
|
||||
// Yaml yaml = new Yaml();
|
||||
// cnf = yaml.load(content);
|
||||
// log.info(content);
|
||||
// // Properties cnf = new Properties();
|
||||
// // cnf.load(new StringReader(content));
|
||||
|
||||
// System.out.println(cnf);
|
||||
|
||||
// configService.addListener(dataId, group, new Listener() {
|
||||
// @Override
|
||||
// public void receiveConfigInfo(String configInfo) {
|
||||
// System.out.println("recieve:" + configInfo);
|
||||
// synchronized(cnf){
|
||||
// cnf = (Map<String, Object>)new Yaml().load(configInfo);
|
||||
// }
|
||||
|
||||
// System.out.println(cnf);
|
||||
// }
|
||||
|
||||
// @Override
|
||||
// public Executor getExecutor() {
|
||||
// return null;
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
||||
ConfigService configService = NacosFactory.createConfigService(properties);
|
||||
String content = configService.getConfig(dataId, group, 5000);
|
||||
|
||||
Properties cnf = new Properties();
|
||||
|
||||
cnf.load(new StringReader(content));
|
||||
|
||||
System.out.println(cnf);
|
||||
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
System.out.println("recieve:" + configInfo);
|
||||
try {
|
||||
cnf.load(new StringReader(configInfo));
|
||||
System.out.println(cnf);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
// cnf.put("publish", "123423112332");
|
||||
|
||||
// log.info(yaml.dumpAsMap(cnf));
|
||||
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return null;
|
||||
Config.UseConfig((cnf) -> {
|
||||
log.info("{}",cnf.configMap);
|
||||
cnf.configMap.put("use_config", "12");
|
||||
try {
|
||||
log.info("{}",cnf.update());
|
||||
} catch (NacosException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
|
||||
// boolean isPublishOk = configService.publishConfig(dataId, group, yaml.dumpAsMap(cnf));
|
||||
// System.out.println(isPublishOk);
|
||||
|
||||
Thread.sleep(300000);
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
server.port=3440
|
||||
yuandian.dataflow.config.nacos.server.addr="192.168.1.113:8848"
|
||||
yuandian.dataflow.config.nacos.dataid=dataflow
|
||||
yuandian.dataflow.config.nacos.group=yuandian
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user