色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:47日期: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
相關文章:
主站蜘蛛池模板: 欧美大片毛片大片 | 国产激情视频在线播放 | 国产精品成人一区二区不卡 | 久久91精品国产91久久小草 | freesexvideo性欧美tv2021 | 成人爱做日本视频免费 | 日本三级免费网站 | 欧美久久久久久久一区二区三区 | 91精品视频播放 | 久久91这里精品国产2020 | 天堂8中文在线最新版在线 天堂8资源8在线 | 怡红院在线观看 | 午夜亚洲国产成人不卡在线 | 亚洲欧美日韩综合在线一区二区三区 | 三级黄色网 | ⅹxx中国xxx人妖 | 成人a视频片在线观看免费 成人a视频在线观看 | caoporen在线视频入口 | 欧美午夜不卡在线观看最新 | 日韩精品一区二区三区 在线观看 | 久草免费资源视频 | 亚洲视频在线观看 | 一级做人爱a视频正版免费 一级做性色a爱片久久片 | 久久精品国产欧美成人 | 精品国产午夜久久久久九九 | 国产精品亚洲欧美云霸高清 | 中文字幕99在线精品视频免费看 | 亚洲jjzzjjzz在线播放 | 国产午夜精品理论片在线 | 亚洲悠悠色综合中文字幕 | 亚洲第一视频在线观看 | 国产黄色小视频在线观看 | 国产精品在线观看 | 久久精品91| 国产精品一区伦免视频播放 | 精品动漫一区二区 | 91香蕉成人免费网站 | 亚洲伊人色综合网站亚洲伊人 | 亚洲一区二区在线视频 | 2022国内精品免费福利视频 | 欧美色另类|