死机简单配置

编写整个框架的目的是为了处理应用程序的配置。 我更喜欢一种简单的方法。

如果通过配置我们的意思是“ 部署之间可能有所不同的所有内容 ”,那么我们应该尝试使配置保持简单。 在Java中,最简单的选项是不起眼的属性文件。 属性文件的缺点是,当您希望应用程序接收更改时必须重新启动它。 还是你

这是我在多个项目中使用的一种简单方法:


public class MyAppConfig extends AppConfiguration {

    private static MyAppConfig instance = new MyAppConfig();

    public static MyAppConfig instance() {
        return instance;
    }

    private MyAppConfig() {
        this("myapp.properties");
    }

    public String getServiceUrl() {
        return getRequiredProperty("service.url");
    }

    public boolean getShouldStartSlow() {
        return getFlag("start-slow", false);
    }
    
    public int getHttpPort(int defaultPort) {
        return getIntProperty("myapp.http.port", defaultPort);
    }

}

AppConfiguration类如下所示:

public abstract class AppConfiguration {

    private static Logger log = LoggerFactory.getLogger(AppConfiguration.class);

    private long nextCheckTime = 0;
    private long lastLoadTime = 0;
    private Properties properties = new Properties();
    private final File configFile;

    protected AppConfiguration(String filename) {
        this.configFile = new File(filename);
    }

    public String getProperty(String propertyName, String defaultValue) {
        String result = getProperty(propertyName);
        if (result == null) {
            log.trace("Missing property {} in {}", propertyName, properties.keySet());
            return defaultValue;
        }
        return result;
    }

    public String getRequiredProperty(String propertyName) {
        String result = getProperty(propertyName);
        if (result == null) {
            throw new RuntimeException("Missing property " + propertyName);
        }
        return result;
    }

    private String getProperty(String propertyName) {
        if (System.getProperty(propertyName) != null) {
            log.trace("Reading {} from system properties", propertyName);
            return System.getProperty(propertyName);
        }
        if (System.getenv(propertyName.replace('.', '_')) != null) {
            log.trace("Reading {} from environment", propertyName);
            return System.getenv(propertyName.replace('.', '_'));
        }

        ensureConfigurationIsFresh();
        return properties.getProperty(propertyName);
    }

    private synchronized void ensureConfigurationIsFresh() {
        if (System.currentTimeMillis() < nextCheckTime) return;
        nextCheckTime = System.currentTimeMillis() + 10000;
        log.trace("Rechecking {}", configFile);

        if (!configFile.exists()) {
            log.error("Missing configuration file {}", configFile);
        }

        if (lastLoadTime >= configFile.lastModified()) return;
        lastLoadTime = configFile.lastModified();
        log.debug("Reloading {}", configFile);

        try (FileInputStream inputStream = new FileInputStream(configFile)) {
            properties.clear();
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load " + configFile, e);
        }
    }
}

这样可以高效地读取配置文件,并根据需要更新设置。 它支持默认的环境变量和系统属性。 而且它甚至可以很好地记录正在发生的事情。

  • 有关完整的源代码和自动更新的不可思议的数据源,请参见以下要点:https://gist.github.com/jhannes/b8b143e0e5b287d73038

请享用!

翻译自: https://www.javacodegeeks.com/2014/10/dead-simple-configuration.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值