consul宕机配置丢失_简单的配置死机
生活随笔
收集整理的這篇文章主要介紹了
consul宕机配置丢失_简单的配置死机
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
consul宕機(jī)配置丟失
編寫整個(gè)框架的目的是為了處理應(yīng)用程序的配置。 我更喜歡一種簡單的方法。
如果通過配置來表示“ 部署之間可能有所不同的所有內(nèi)容 ”,那么我們應(yīng)該嘗試使配置保持簡單。 在Java中,最簡單的選項(xiàng)是不起眼的屬性文件。 屬性文件的缺點(diǎn)是,當(dāng)您希望應(yīng)用程序接收更改時(shí)必須重新啟動它。 還是你
這是我在多個(gè)項(xiàng)目中使用的一種簡單方法:
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);}} }這樣可以高效地讀取配置文件,并根據(jù)需要更新設(shè)置。 它支持默認(rèn)的環(huán)境變量和系統(tǒng)屬性。 而且它甚至可以很好地記錄正在發(fā)生的事情。
- 有關(guān)完整的源代碼和自動更新的不可思議的數(shù)據(jù)源,請參見以下要點(diǎn):https://gist.github.com/jhannes/b8b143e0e5b287d73038
請享用!
翻譯自: https://www.javacodegeeks.com/2014/10/dead-simple-configuration.html
consul宕機(jī)配置丟失
總結(jié)
以上是生活随笔為你收集整理的consul宕机配置丢失_简单的配置死机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wmv电脑用什么播放器(wmv格式可以在
- 下一篇: 从工作中清除代码–使用JUnit 5,M