资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

这篇文章主要讲解了spring+apollo怎么动态获取yaml格式的配置,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、网站制作、平果网络推广、小程序定制开发、平果网络营销、平果企业策划、平果品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供平果建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

默认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> 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 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 list) {
 this.list = list;
 }
 
 public boolean isAllow(String address) {
 return list.contains(address);
 }
 
 @ApolloConfigChangeListener(PROP_NAME)
 public void onChange(ConfigChangeEvent changeEvent) {
 Set 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 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格式的配置有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


当前标题:spring+apollo怎么动态获取yaml格式的配置
网站网址:http://cdkjz.cn/article/gdssio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220