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;
        });
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值