国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式

瀏覽:38日期:2023-09-08 10:09:12

默認(rèn)spring裝載的都是.properties格式的配置文件,但是有時(shí)我們需要定義list或者map類型的配置,那么yaml就具有優(yōu)勢(shì)。

以下演示利用apollo來完成自動(dòng)更新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參數(shù)的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)@Slf4jpublic 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; }); }}

補(bǔ)充知識(shí):yml格式問題

以縮進(jìn)代表層級(jí)關(guān)系

空格個(gè)數(shù)不重要,但是同一層級(jí)必須左對(duì)齊

大小寫敏感

格式為:key= value

注釋單行用#,只能注釋單行

application.properties中

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

轉(zhuǎn)化為application.yml中

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

冒號(hào)后面必須跟空格,否則格式錯(cuò)誤

以上這篇spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日本免费视频观看在线播放 | 九九综合九九 | 在线视频一区二区日韩国产 | 亚洲国产精品热久久2022 | 在线国产三级 | 精品无码久久久久国产 | 亚洲视频日韩 | 黄网在线观看免费 | 日本在线亚州精品视频在线 | 天天综合色一区二区三区 | 99久久精品久久久久久婷婷 | 成人免费一级毛片在线播放视频 | 欧美在线香蕉在线现视频 | 欧美性色黄大片一级毛片视频 | 99视频精品全国免费 | 毛片成人永久免费视频 | 中国a毛片 | 国产夫妇肉麻对白 | 国产福利一区二区三区 | 国产精品久久久久久久久久一区 | 欧美精品日日鲁夜夜添 | 欧美做爱毛片 | 国产第一页久久亚洲欧美国产 | 国产日韩欧美一区二区三区综合 | 免费人欧美成又黄又爽的视频 | 欧美高清一级啪啪毛片 | 九色自拍视频 | 欧美国产在线观看 | 亚洲精品视频在线 | 韩国精品一区二区三区在线观看 | 在线中文字幕日韩 | 久草中文在线 | 亚洲无限看| 久久久久免费精品国产 | 特级一级全黄毛片免费 | 国产高清美女一级a毛片久久w | 狠狠综合久久 | 欧美性生交大片免费看 | 国产精品99久久久久久宅男 | 国产综合第一页 | 最新国产午夜精品视频不卡 |