配置更改而不重新部署

问题描述 投票:0回答:1

我的Web应用程序与外部系统有多个集成,所有这些集成Rest URL都保存在Web应用程序的配置文件中。我的应用程序在启动时读取此配置文件,并在连接外部系统时使用URL值。但通常情况下,其中一个外部系统出现故障,我们必须使用备用URL。在这种情况下,我们通常必须修改配置并重新部署war文件。有没有办法用新值修改配置文件而不需要重新部署war文件?

java spring web jboss
1个回答
1
投票

在我的项目中,我通常使用Apache Commons Configuration来管理配置文件(属性)。此库具有在文件更改时自动重新加载值的功能。

这是我的实施建议:

创建一个类“MyAppConfigProperties”以加载属性文件并读取配置键:

public class MyAppConfig {

    //Apache Commons library object
    private PropertiesConfiguration configFile;

    private void init() {
        try {
            //Load the file            
            configFile = new PropertiesConfiguration(
                    MyAppConfig.class.getClassLoader().getResource("configFile.properties"));

            // Create refresh strategy with "FileChangedReloadingStrategy"
            FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
            fileChangedReloadingStrategy.setRefreshDelay(1000);
            configFile.setReloadingStrategy(fileChangedReloadingStrategy);

        } catch (ConfigurationException e) {
            //Manage the exception
        }
    }

    /**
     * Constructor por defecto.
     */
    public MyAppConfig() {
        super();
        init();
    }

    public String getKey(final String key) {

        try {
            if (configFile.containsKey(key)) {
                return configFile.getString(key);
            } else {
                return null;
            }

        } catch (ConversionException e) {
            //Manage Exception
        }
    }
}

现在你必须构造这个类的实例(单例)并在所有需要reed配置键的地方使用它。

每次使用方法“getKey”时,您将获得密钥的最后一个值,而无需部署和重新启动。

© www.soinside.com 2019 - 2024. All rights reserved.