spring+apollo动态获取yaml格式的配置方式
程序员文章站
2022-03-20 13:57:16
默认spring装载的都是.properties格式的配置文件,但是有时我们需要定义list或者map类型的配置,那么yaml就具有优势。以下演示利用apollo来完成自动更新ip白名单的功能1.重写...
默认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静态代码块作用及执行顺序解析