亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

spring+apollo動態獲取yaml格式的配置方式

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

默認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)@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; }); }}

補充知識:yml格式問題

以縮進代表層級關系

空格個數不重要,但是同一層級必須左對齊

大小寫敏感

格式為:key= value

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

application.properties中

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

轉化為application.yml中

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

冒號后面必須跟空格,否則格式錯誤

以上這篇spring+apollo動態獲取yaml格式的配置方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本xxwwwxxxx网站 | 亚洲国产成人久久精品图片 | 欧美精品亚洲精品日韩专区 | 亚洲第一黄色网址 | 韩国美女激情视频一区二区 | 特一级黄色大片 | 色一色在线观看视频网站 | 伊人狠狠丁香婷婷综合色 | 国产孕妇孕交600集 国产在视频线精品视频www666 | 国产成人综合网在线播放 | 亚洲第一页视频 | 国产污视频在线播放 | 国产99久久九九精品免费 | 久久91精品久久91综合 | 夜夜夜爽bbbb性视频 | 麻豆va在线精品免费播放 | 免费看精品黄线在线观看 | 性美国xxxxx免费 | 国产美女无遮挡免费视频 | 韩国美女激情视频一区二区 | 国产闫盼盼私拍福利精品视频 | 国产夜趣福利免费视频 | 午夜精品久久久久久久久 | 婷婷综合激情 | 欧美一级毛片高清免费观看 | 欧美成人精品一级高清片 | 久久www成人看片 | 亚洲欧美日韩一级特黄在线 | 国产日本亚洲欧美 | 欧美特黄高清免费观看的 | 巨臀中文字幕一区二区视频 | 在线精品国精品国产不卡 | 伊人久久综合网亚洲 | 精品国产看高清国产毛片 | 免费中文字幕在线国语 | 国外成人在线视频 | 国产精品酒店视频免费看 | 一级美女片 | 日本aaaaa级毛片 | 欧美精品在欧美一区二区 | 国产丝袜视频在线 |