SpringBoot动态更新yml文件

尤三姐
• 阅读 1117

前言

在系统运行过程中,可能由于一些配置项的简单变动需要重新打包启停项目,这对于在运行中的项目会造成数据丢失,客户操作无响应等情况发生,针对这类情况对开发框架进行升级提供yml文件实时修改更新功能

项目依赖

项目基于的是2.0.0.RELEASE版本,所以snakeyaml需要单独引入,高版本已包含在内

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.23</version>
        </dependency>

网上大多数方法是引入spring-cloud-context配置组件调用ContextRefresher的refresh方法达到同样的效果,考虑以下两点未使用

  • 开发框架使用了logback日志,引入spring-cloud-context会造成日志配置读取错误
  • 引入spring-cloud-context会同时引入spring-boot-starter-actuator组件,会开放一些健康检查路由及端口,需要对框架安全方面进行额外控制

YML文件内容获取

读取resource文件下的文件需要使用ClassPathResource获取InputStream

    public String getTotalYamlFileContent() throws Exception {
        String fileName = "application.yml";
        return getYamlFileContent(fileName);
    }
    public String getYamlFileContent(String fileName) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        return onvertStreamToString(classPathResource.getInputStream());
    }
    public static String convertStreamToString(InputStream inputStream) throws Exception{
       return IOUtils.toString(inputStream, "utf-8");
    }

YML文件内容更新

我们获取到yml文件内容后可视化显示到前台进行展示修改,将修改后的内容通过yaml.load方法转换成Map结构,再使用yaml.dumpAsMap转换为流写入到文件

    public void updateTotalYamlFileContent(String content) throws Exception {
        String fileName = "application.yml";
        updateYamlFileContent(fileName, content);
    }
    public void updateYamlFileContent(String fileName, String content) throws Exception {
        Yaml template = new Yaml();
        Map<String, Object> yamlMap = template.load(content);

        ClassPathResource classPathResource = new ClassPathResource(fileName);

        Yaml yaml = new Yaml();
        //字符输出
        FileWriter fileWriter = new FileWriter(classPathResource.getFile());
        //用yaml方法把map结构格式化为yaml文件结构
        fileWriter.write(yaml.dumpAsMap(yamlMap));
        //刷新
        fileWriter.flush();
        //关闭流
        fileWriter.close();
    }

YML属性刷新

yml属性在程序中读取使用一般有三种

  • 使用Value注解
    @Value("${system.systemName}")
    private String systemName;
  • 通过enviroment注入读取
    @Autowired
    private Environment environment;
    
    environment.getProperty("system.systemName")
  • 使用ConfigurationProperties注解读取
@Component
@ConfigurationProperties(prefix = "system")
public class SystemConfig {
    private String systemName;
}

Property刷新

我们通过environment.getProperty方法读取的配置集合实际是存储在PropertySources中的,我们只需要把键值对全部取出存储在propertyMap中,将更新后的yml文件内容转换成相同格式的ymlMap,两个Map进行合并,调用PropertySources的replace方法进行整体替换即可

但是yaml.load后的ymlMap和PropertySources取出的propertyMap两者数据解构是不同的,需要进行手动转换

propertyMap集合就是单纯的key,value键值对,key是properties形式的名称,例如system.systemName=>xxxxx集团管理系统

ymlMap集合是key,LinkedHashMap的嵌套层次结构,例如system=>(systemName=>xxxxx集团管理系统)

  • 转换方法如下
  public HashMap<String, Object> convertYmlMapToPropertyMap(Map<String, Object> yamlMap) {
        HashMap<String, Object> propertyMap = new HashMap<String, Object>();
        for (String key : yamlMap.keySet()) {
            String keyName = key;
            Object value = yamlMap.get(key);
            if (value != null && value.getClass() == LinkedHashMap.class) {
                convertYmlMapToPropertyMapSub(keyName, ((LinkedHashMap<String, Object>) value), propertyMap);
            } else {
                propertyMap.put(keyName, value);
            }
        }
        return propertyMap;
    }

    private void convertYmlMapToPropertyMapSub(String keyName, LinkedHashMap<String, Object> submMap, Map<String, Object> propertyMap) {
        for (String key : submMap.keySet()) {
            String newKey = keyName + "." + key;
            Object value = submMap.get(key);
            if (value != null && value.getClass() == LinkedHashMap.class) {
                convertYmlMapToPropertyMapSub(newKey, ((LinkedHashMap<String, Object>) value), propertyMap);
            } else {
                propertyMap.put(newKey, value);
            }
        }
    }
  • 刷新方法如下
        String name = "applicationConfig: [classpath:/" + fileName + "]";
        MapPropertySource propertySource = (MapPropertySource) environment.getPropertySources().get(name);
        Map<String, Object> source = propertySource.getSource();
        Map<String, Object> map = new HashMap<>(source.size());
        map.putAll(source);

        Map<String, Object> propertyMap = convertYmlMapToPropertyMap(yamlMap);

        for (String key : propertyMap.keySet()) {
            Object value = propertyMap.get(key);
            map.put(key, value);
        }
        environment.getPropertySources().replace(name, new MapPropertySource(name, map));

注解刷新

不论是Value注解还是ConfigurationProperties注解,实际都是通过注入Bean对象的属性方法使用的,我们先自定注解RefreshValue来修饰属性所在Bean的class

通过实现InstantiationAwareBeanPostProcessorAdapter接口在系统启动时过滤筛选对应的Bean存储下来,在更新yml文件时通过spring的event通知更新对应

bean的属性即可

  • 注册事件使用EventListener注解
    @EventListener
    public void updateConfig(ConfigUpdateEvent configUpdateEvent) {
        if(mapper.containsKey(configUpdateEvent.key)){
            List<FieldPair> fieldPairList = mapper.get(configUpdateEvent.key);
            if(fieldPairList.size()>0){
                for (FieldPair fieldPair:fieldPairList) {
                    fieldPair.updateValue(environment);
                }
            }
        }
    }
  • 通知触发事件使用ApplicationContext的publishEvent方法
    @Autowired
    private ApplicationContext applicationContext;
    
      for (String key : propertyMap.keySet()) {
       applicationContext.publishEvent(new YamlConfigRefreshPostProcessor.ConfigUpdateEvent(this, key));
    }

YamlConfigRefreshPostProcessor的完整代码如下

@Component
public class YamlConfigRefreshPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements EnvironmentAware {
    private Map<String, List<FieldPair>> mapper = new HashMap<>();
    private Environment environment;

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        processMetaValue(bean);
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    private void processMetaValue(Object bean) {
        Class clz = bean.getClass();
        if (!clz.isAnnotationPresent(RefreshValue.class)) {
            return;
        }

        if (clz.isAnnotationPresent(ConfigurationProperties.class)) {
            //@ConfigurationProperties注解
            ConfigurationProperties config = (ConfigurationProperties) clz.getAnnotation(ConfigurationProperties.class);
            for (Field field : clz.getDeclaredFields()) {
                String key = config.prefix() + "." + field.getName();
                if(mapper.containsKey(key)){
                    mapper.get(key).add(new FieldPair(bean, field, key));
                }else{
                    List<FieldPair> fieldPairList = new ArrayList<>();
                    fieldPairList.add(new FieldPair(bean, field, key));
                    mapper.put(key, fieldPairList);
                }
            }
        } else {
            //@Valuez注解
            try {
                for (Field field : clz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(Value.class)) {
                        Value val = field.getAnnotation(Value.class);
                        String key = val.value().replace("${", "").replace("}", "");
                        if(mapper.containsKey(key)){
                            mapper.get(key).add(new FieldPair(bean, field, key));
                        }else{
                            List<FieldPair> fieldPairList = new ArrayList<>();
                            fieldPairList.add(new FieldPair(bean, field, key));
                            mapper.put(key, fieldPairList);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }
    }

    public static class FieldPair {
        private static PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}",
                ":", true);
        private Object bean;
        private Field field;
        private String value;

        public FieldPair(Object bean, Field field, String value) {
            this.bean = bean;
            this.field = field;
            this.value = value;
        }

        public void updateValue(Environment environment) {
            boolean access = field.isAccessible();
            if (!access) {
                field.setAccessible(true);
            }
            try {
                if (field.getType() == String.class) {
                    String updateVal = environment.getProperty(value);
                    field.set(bean, updateVal);
                }
                else if (field.getType() == Integer.class) {
                    Integer updateVal = environment.getProperty(value,Integer.class);
                    field.set(bean, updateVal);
                }
                else if (field.getType() == int.class) {
                    int updateVal = environment.getProperty(value,int.class);
                    field.set(bean, updateVal);
                }
                else if (field.getType() == Boolean.class) {
                    Boolean updateVal = environment.getProperty(value,Boolean.class);
                    field.set(bean, updateVal);
                }
                else if (field.getType() == boolean.class) {
                    boolean updateVal = environment.getProperty(value,boolean.class);
                    field.set(bean, updateVal);
                }
                else {
                    String updateVal = environment.getProperty(value);
                    field.set(bean, JSONObject.parseObject(updateVal, field.getType()));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(access);
        }

        public Object getBean() {
            return bean;
        }

        public void setBean(Object bean) {
            this.bean = bean;
        }

        public Field getField() {
            return field;
        }

        public void setField(Field field) {
            this.field = field;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public static class ConfigUpdateEvent extends ApplicationEvent {
        String key;

        public ConfigUpdateEvent(Object source, String key) {
            super(source);
            this.key = key;
        }
    }

    @EventListener
    public void updateConfig(ConfigUpdateEvent configUpdateEvent) {
        if(mapper.containsKey(configUpdateEvent.key)){
            List<FieldPair> fieldPairList = mapper.get(configUpdateEvent.key);
            if(fieldPairList.size()>0){
                for (FieldPair fieldPair:fieldPairList) {
                    fieldPair.updateValue(environment);
                }
            }
        }
    }
}
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
springboot解析自定义yml
springboot解析自定义yml在实际项目开发中我们经常需要用到一些自定义配置,并且希望单独配置,方便维护,现在介绍下方式:方式一手动加载对于一些不变动的配置,写死在项目中维护,如下!在这里插入图片描述(https://imgblog.csdnimg.cn/20191028204130679
Wesley13 Wesley13
3年前
JavaEE进阶知识学习
SpringCloudConfig配置中心概述就前面项目而言,分布面临的问题是配置问题,每一个项目都有一个yml文件,不好运维管理,所有需要一套集中式,动态的配置管理设施,SpringCloud提供了ConfigServer来解决这个问题。SpringCloudConfig是为微服务架构中的微服
Wesley13 Wesley13
3年前
ubuntu 18.04 server下tomcat部署sprongboot项目
1springboot项目相关1.1修改pom.xml文件依赖如果要将最终的打包形式改为war的话,还需要对pom.xml文件进行修改,因为springbootstarterweb中包含内嵌的tomcat容器,所以直接部署在外部容器会冲突报
Wesley13 Wesley13
3年前
Go配置文件热加载
在日常项目的开发中,我们经常会使用配置文件来保存项目的基本元数据,配置文件的类型有很多,如:JSON、xml、yaml、甚至可能是个纯文本格式的文件。不管是什么类型的配置数据,在某些场景下,我们可会有热更新当前配置文件内容的需求,比如:使用Go运行的一个常驻进程,运行了一个WebServer服务进程。此时,如果配置文件发生变化,我们
Easter79 Easter79
3年前
SpringBoot介绍
一、SpringBoot的特点1.为基于Spring的开发提供更快的入门体验。2.创建可以独立运行的Spring应用。3.直接潜入Tomcat或Jetty服务器,不需要打包成war文件。4.提供推荐的基础pom文件(starter)来简化ApacheMaven配置。5.尽可能的根据项目依赖来自动配置Spr
Stella981 Stella981
3年前
SpringBoot介绍
一、SpringBoot的特点1.为基于Spring的开发提供更快的入门体验。2.创建可以独立运行的Spring应用。3.直接潜入Tomcat或Jetty服务器,不需要打包成war文件。4.提供推荐的基础pom文件(starter)来简化ApacheMaven配置。5.尽可能的根据项目依赖来自动配置Spr
Stella981 Stella981
3年前
Linux下部署springboot项目的步骤及过程
最近在研究将springboot项目部署到Linux服务器上,由于springboot是内嵌了tomcat,所以可以直接将项目打包上传至服务器上。我是在idea上的项目,所以我就基于此说下过程。(一)打包你的项目1、在pom文件中添加springboot的maven插件 !(https://img2018.cnblogs.com/blog/1
Stella981 Stella981
3年前
SpringBoot2.0之六 多环境配置
  开发过程中面对不同的环境,例如数据库、redis服务器等的不同,可能会面临一直需要修改配置的麻烦中,在以前的项目中,曾通过Tomcat的配置来实现,有的项目甚至需要手动修改相关配置,这种方式费时费力,出错的概率还极大,SpringBoot为我们提供了更加简单方便的配置方案来解决多环境的配置问题,下面我们看看怎么实现。一、新建一个项目(本文以上篇的代码
Stella981 Stella981
3年前
SpringBoot 开发秘籍
!(https://oscimg.oschina.net/oscnet/0b0e2e35748141a9a2de5f4eb579de18.png)概述在项目开发过程中,某个功能需要依赖在配置文件中配置的参数。这时候就可能出现下面这种现象问题:有时候经常出现项目启动了,等到使用某个功能组件的时候出现异常,提示参数未配置或者bean
Stella981 Stella981
3年前
Spring Boot(十三):实现热部署
一、前言在实际开发过程中,每次修改代码就得将项目重启,重新部署,对于一些大型应用来说,重启时间需要花费大量的时间成本。对于一个后端开发者来说,重启过程确实很难受。在java开发领域,热部署一直是一个难以解决的问题,目前的java虚拟机只能实现方法体的热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作。!(htt
小万哥 小万哥
1年前
Git 分支管理:优化版本控制与应急处理的关键策略
使用Git分支:轻松管理不同版本和应对紧急情况的最佳实践使用Git分支在Git中,分支是主仓库的新/独立版本。假设你有一个大型项目,需要对其进行设计更新。没有使用Git时:复制所有相关文件以避免影响实时版本开始进行设计工作,并发现代码依赖于其他文件中的代码