返回> 网站首页 

Springboot 动态更新properties配置文件参数

yoours2024-02-20 13:12:06 阅读 748

简介一边听听音乐,一边写写文章。

一、介绍

    作为与硬件设备交互使用的Web网站在运行时,有时候需要在不影响当前程序运行状态的情况下修改一些参数并直接生效,比如修改转发url地址、Tcp、Udp转发IP等。

    在此实现直接修改properties配置文件,使用定时器定时查询配置信息并更新当前环境变量。


二、环境变量替换更新

    1. 使用replace替换启动时加载的配置实例

    environment.getPropertySources().replace(propertyName, filePropertiesSource);

    2. 定时重新加载配置

    继承 MapPropertySource 类,创建定时执行函数实现定时读取配置文件


三、部分代码

    1. 获取配置文件

        实现获取三种情况的配置文件名,默认配置文件名、指定配置文件名、运行参数配置文件名

    public String getPropertiesFileName()

    {

        //-Dspring.config.location=e:\application-thinkpad.properties

        String propertyFile = environment.getProperty("spring.config.location");

        if(propertyFile!=null)

        {

            return propertyFile;

        }


        Resource propertiesFile = resourceLoader.getResource("classpath:/application.properties");

        // spring.profiles.active=thinkpad

        propertyFile = environment.getProperty("spring.profiles.active");

        if(propertyFile!=null)

        {

            propertiesFile = resourceLoader.getResource("classpath:/application-" + propertyFile + ".properties");

        }

        return propertiesFile.getFilename();

    }


    2. 替换原环境变量实例

    @Bean

    public FilePropertiesSource filePropertiesSource(ConfigurableEnvironment environment)

    {

        String fileName = propertiesFileNameReader.getPropertiesFileName();

        FilePropertiesSource filePropertiesSource = new FilePropertiesSource(fileName);


        String propertyName = "";

        MutablePropertySources mutablePropertySources = environment.getPropertySources();

        for(PropertySource ps : mutablePropertySources)

        {

            if(ps.getName().contains(fileName))

            {

                propertyName = ps.getName();

                break;

            }

        }


        environment.getPropertySources().replace(propertyName, filePropertiesSource);

        return filePropertiesSource;

    }


    3. 定时读取配置文件更新实例

    @PostConstruct

    @Scheduled(fixedRate = 10_000)

    public void refreshSource() throws IOException {

        File file = propertiesFileNameReader.getPropertiesFile();

        FileInputStream inputStream=new FileInputStream(file);

        if (inputStream == null) {

            throw new FileNotFoundException("配置文件 不存在");

        }


        Map<String, String> map = new HashMap<>();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

            String line;

            while ((line = reader.readLine()) != null) {

                line = line.trim();

                if (StringUtils.isEmpty(line) || line.startsWith("#")) {

                    continue;

                }

                String[] pair = StringUtils.split(line, "=");

                if (pair == null || pair.length != 2) {

                    continue;

                }

                String key = pair[0].trim();

                String value = pair[1].trim();

                map.put(key, value);

            }

        } catch (IOException e) {

            throw e;

        }


        source.clear();

        source.putAll(map);

    }


    4. 读取

    String serverUrl = environment.getProperty("universal.serverUrl");


微信小程序扫码登陆

文章评论

748人参与,0条评论