spring+apollo动态获取yaml格式的配置方式

所属分类: 软件编程 / java 阅读数: 510
收藏 0 赞 0 分享

默认spring装载的都是.properties格式的配置文件,但是有时我们需要定义list或者map类型的配置,那么yaml就具有优势。

以下演示利用apollo来完成自动更新ip白名单的功能

1.重写配置工厂

public class YmlPropertySourceFactory extends DefaultPropertySourceFactory {
 public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
 String configName = resource.getResource().getFilename();
 ConfigFile configFile = ConfigService.getConfigFile(configName.substring(0, configName.indexOf(".")), ConfigFileFormat.YML);
 String ct = configFile.getContent();
 return YamlPropUtil.buildYaml(configName, ct);
 }
}

定义-D参数的appid和conf目录

public class YamlPropUtil {
 public static PropertySource buildYaml(String name, String content) throws IOException {
 String sysName = System.getProperty("app.id");
 String appDir = System.getProperty("apollo.cacheDir");
 if (appDir.endsWith(File.separator)) {
 appDir= appDir.substring(0, appDir.length() - 1);
 }
 String filePath = appDir+ File.separator + sysName + File.separator + name;
 File file = new File(filePath);
 if (file.exists()) {
 file.delete();
 }
 try (BufferedWriter bufferedWriter = Files.newWriter(file, Charsets.UTF_8)) {
 bufferedWriter.write(content);
 bufferedWriter.flush();
 List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(name, new FileSystemResource(filePath));
 return sources.get(0);
 } catch (IOException e) {
 throw e;
 }
 }
}

2.装载配置

whiteList.yml

注意本地也要存放上述文件在classpath下

white:
 ip:
 #ip白名单列表
 list:
 - 192.168.103.34
 - 192.168.1.102
@Configuration
@ConfigurationProperties(prefix = "white.ip")
@PropertySource(value = "classpath:whiteList.yml", factory = YmlPropertySourceFactory.class)
@Slf4j
public class IpWhiteListService {
 private List<String> list;
 private final static int MAX_PROP_ITEM = 1000;
 private final static String PROP_NAME = "whiteList.yml";
 private final static String KEY_PREFIX = "white.ip.list";
 
 public void setList(List<String> list) {
 this.list = list;
 }
 
 public boolean isAllow(String address) {
 return list.contains(address);
 }
 
 @ApolloConfigChangeListener(PROP_NAME)
 public void onChange(ConfigChangeEvent changeEvent) {
 Set<String> keys = changeEvent.changedKeys();
 
 keys.forEach(e -> {
 String newVal = changeEvent.getChange(e).getNewValue();
 log.debug("whiteList is changed={}", newVal);
 String ct = newVal;
 org.springframework.core.env.PropertySource propertySource = null;
 try {
 propertySource = YamlPropUtil.buildYaml(PROP_NAME, ct);
 } catch (IOException ex) {
 log.error("", e);
 }
 List<String> newList = Lists.newArrayList();
 for (int i = 0; i < MAX_PROP_ITEM; i++) {
 String pName = KEY_PREFIX + "[" + i + "]";
 if (propertySource.containsProperty(pName)) {
  String val = (String) propertySource.getProperty(pName);
  newList.add(val);
 }
 }
 list = newList;
 });
 }
}

补充知识:yml格式问题

以缩进代表层级关系

空格个数不重要,但是同一层级必须左对齐

大小写敏感

格式为:key= value

注释单行用#,只能注释单行

application.properties中

logging.level.root=DEBUG
logging.level.org.springframework=DEBUG
logging.level.org.org.mybatis=DEBUG

转化为application.yml中

logging:
level:
root: DEBUG
org.springframework: DEBUG
org.org.mybatis: DEBUG

冒号后面必须跟空格,否则格式错误

以上这篇spring+apollo动态获取yaml格式的配置方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java输入输出流复制文件所用时间对比

这篇文章主要介绍了Java输入输出流复制文件所用时间对比的相关资料,非常不错,具有参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程中start和run方法全面解析

这篇文章主要介绍了Java线程中start和run方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的JSON处理器fastjson使用方法详解

下面小编就为大家带来一篇Java的JSON处理器fastjson使用方法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java 二维码,QR码,J4L-QRCode 的资料整理

本文主要介绍Java 中二维码,QR码,J4L-QRCode,这里整理了详细的资料供大家学习参考关于二维码的知识,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

java哈夫曼树实例代码

这篇文章主要为大家介绍了java哈夫曼树实例代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android读取本地或网络图片并转换为Bitmap

这篇文章主要为大家详细介绍了Android读取本地或网络图片,并转换为Bitmap,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java日期时间操作的方法

这篇文章主要为大家详细介绍了Java日期时间操作的一些方法,获得Calendar,定义日期/时间的格式等,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 获取路径的各种方法(总结)

下面小编就为大家带来一篇java 获取路径的各种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java数据结构与算法之奇偶排序算法完整示例

这篇文章主要介绍了java数据结构与算法之奇偶排序算法,较为详细的分析了奇偶算法的原理并结合完整示例形式给出了实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

java数据结构与算法之双向循环队列的数组实现方法

这篇文章主要介绍了java数据结构与算法之双向循环队列的数组实现方法,结合实例形式分析了双向循环队列的原理与数组实现技巧,并附带说明了该算法的用途,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多