欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年04月27日 08:49:15   作者:碧海潮生吹玉簫  
這篇文章主要介紹了spring+apollo動(dòng)態(tài)獲取yaml格式的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

以下演示利用apollo來(lái)完成自動(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)
@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;
 });
 }
}

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

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

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

大小寫敏感

格式為:key= value

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

application.properties中

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

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

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

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

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

相關(guān)文章

  • SpringBoot框架整合Mybatis簡(jiǎn)單攻略

    SpringBoot框架整合Mybatis簡(jiǎn)單攻略

    這篇文章主要介紹了SpringBoot框架整合Mybatis的簡(jiǎn)單攻略,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-10-10
  • springBoot項(xiàng)目啟動(dòng)類啟動(dòng)無(wú)法訪問(wèn)的解決方法

    springBoot項(xiàng)目啟動(dòng)類啟動(dòng)無(wú)法訪問(wèn)的解決方法

    這篇文章主要介紹了springBoot項(xiàng)目啟動(dòng)類啟動(dòng)無(wú)法訪問(wèn)的解決方法,需要的朋友可以參考下
    2018-10-10
  • 詳解如何熟練使用java函數(shù)式接口

    詳解如何熟練使用java函數(shù)式接口

    最近剛好有空給大家整理下JDK8的特性,這個(gè)在實(shí)際開(kāi)發(fā)中的作用也是越來(lái)越重了,本文重點(diǎn)講解下函數(shù)式接口內(nèi)容,需要的朋友可以參考下
    2021-06-06
  • Java編程Retry重試機(jī)制實(shí)例詳解

    Java編程Retry重試機(jī)制實(shí)例詳解

    這篇文章主要介紹了Java編程Retry重試機(jī)制實(shí)例詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Java泛型定義與用法入門示例

    Java泛型定義與用法入門示例

    這篇文章主要介紹了Java泛型定義與用法,結(jié)合實(shí)例形式分析了java泛型的功能、定義、應(yīng)用場(chǎng)景及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • IDEA使用SpringAssistant插件創(chuàng)建SpringCloud項(xiàng)目

    IDEA使用SpringAssistant插件創(chuàng)建SpringCloud項(xiàng)目

    IDEA 功能強(qiáng)大,可以用來(lái)高效的開(kāi)發(fā)應(yīng)該程序。它還支持第三方插件、用戶可以根據(jù)需要添加自己喜歡的插件。下面介紹如何使用 IDEA 創(chuàng)建 Spring Cloud 項(xiàng)目
    2021-06-06
  • Java foreach循環(huán)的使用方法詳解

    Java foreach循環(huán)的使用方法詳解

    Java SE5引入了一種更加簡(jiǎn)潔的for語(yǔ)法用于數(shù)組和容器,即foreach語(yǔ)法,表示不必創(chuàng)建int變量去對(duì)由訪問(wèn)項(xiàng)構(gòu)成的序列進(jìn)行計(jì)數(shù),foreach將自動(dòng)產(chǎn)生每一項(xiàng),這種循環(huán)方式在我們后來(lái)遍歷集合時(shí)很常用,所以也有必要來(lái)學(xué)習(xí)一下,需要的朋友可以參考下
    2023-05-05
  • Java Method類及invoke方法原理解析

    Java Method類及invoke方法原理解析

    這篇文章主要介紹了Java Method類及invoke方法原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java多次嵌套循環(huán)查詢數(shù)據(jù)庫(kù)導(dǎo)致代碼中數(shù)據(jù)處理慢的解決

    java多次嵌套循環(huán)查詢數(shù)據(jù)庫(kù)導(dǎo)致代碼中數(shù)據(jù)處理慢的解決

    這篇文章主要介紹了java多次嵌套循環(huán)查詢數(shù)據(jù)庫(kù)導(dǎo)致代碼中數(shù)據(jù)處理慢的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot2.x配置多數(shù)據(jù)源方式

    SpringBoot2.x配置多數(shù)據(jù)源方式

    這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評(píng)論