spring+apollo动态获取yaml格式的配置方式

 更新时间:2020年04月27日 08:49:15   作者:碧海潮生吹玉箫  
这篇文章主要介绍了spring+apollo动态获取yaml格式的配置方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

补充知识:yml格式问题

以缩进代表层级关系

空格个数不重要,但是同一层级必须左对齐

大小写敏感

格式为:key= value

注释单行用#,只能注释单行

application.properties中

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

转化为application.yml中

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

冒号后面必须跟空格,否则格式错误

以上这篇spring+apollo动态获取yaml格式的配置方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 线程池ThreadPoolExecutor使用简介与方法实例

    线程池ThreadPoolExecutor使用简介与方法实例

    今天小编就为大家分享一篇关于线程池ThreadPoolExecutor使用简介与方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • spring自定义注解及使用方法详细例子

    spring自定义注解及使用方法详细例子

    这篇文章主要给大家介绍了关于spring自定义注解及使用方法的相关资料,Spring 是一个非常强大的框架,可以使用自定义注解来完成许多任务,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • Java 非静态初始化的例子

    Java 非静态初始化的例子

    非静态初始化和静态初始化一模一样,只不过少了static关键字。但是如果两者共存的话,非静态初始化是比静态初始化慢一拍的。下边我们举两个例子来看一下。
    2020-09-09
  • Java8的DateTimeFormatter与SimpleDateFormat的区别详解

    Java8的DateTimeFormatter与SimpleDateFormat的区别详解

    这篇文章主要介绍了Java8的DateTimeFormatter与SimpleDateFormat的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 使用Java手搓一个控制台进度条打印工具

    使用Java手搓一个控制台进度条打印工具

    这篇文章主要为大家详细介绍了如何使用Java手搓一个控制台进度条打印工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-04-04
  • Activiti工作流学习笔记之自动生成28张数据库表的底层原理解析

    Activiti工作流学习笔记之自动生成28张数据库表的底层原理解析

    这篇文章主要介绍了Activiti工作流学习笔记之自动生成28张数据库表的底层原理解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • SpringBoot+Resilience4j实现接口限流的示例代码

    SpringBoot+Resilience4j实现接口限流的示例代码

    Resilience4j 是一个用于实现熔断、限流、重试等功能的轻量级库,本文主要介绍了SpringBoot+Resilience4j实现接口限流的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-12-12
  • Java异步调用转同步方法实例详解

    Java异步调用转同步方法实例详解

    这篇文章主要介绍了Java异步调用转同步方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Jasypt的StandardPBEByteEncryptor使用源码解析

    Jasypt的StandardPBEByteEncryptor使用源码解析

    这篇文章主要介绍了Jasypt的StandardPBEByteEncryptor使用源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Java单例模式的线程安全,饿汉和懒汉模式详解

    Java单例模式的线程安全,饿汉和懒汉模式详解

    这篇文章主要为大家详细介绍了Java单例模式的线程安全,饿汉和懒汉模式。文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02

最新评论