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

這篇文章主要講解了spring+apollo怎么動(dòng)態(tài)獲取yaml格式的配置,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、平果網(wǎng)絡(luò)推廣、小程序定制開發(fā)、平果網(wǎng)絡(luò)營(yíng)銷、平果企業(yè)策劃、平果品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供平果建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

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

以下演示利用apollo來完成自動(dòng)更新ip白名單的功能

1.重寫配置工廠

public class YmlPropertySourceFactory extends DefaultPropertySourceFactory {
 public PropertySource<&#63;> 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<&#63;>> 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格式問題

以縮進(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ò)誤

看完上述內(nèi)容,是不是對(duì)spring+apollo怎么動(dòng)態(tài)獲取yaml格式的配置有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站名稱:spring+apollo怎么動(dòng)態(tài)獲取yaml格式的配置
網(wǎng)頁地址:http://muchs.cn/article0/gdssio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、搜索引擎優(yōu)化、Google、響應(yīng)式網(wǎng)站、全網(wǎng)營(yíng)銷推廣電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)