Netflix Archaius 分佈式配置管理依賴構件

Archaius 配置管理API,包含一系列配置管理API,提供動態類型化屬性、線程安全配置操作、輪詢框架、回調機制等功能。

概述

archaius是Netflix公司開源項目之一,基於java的配置管理類庫,主要用於多配置存儲的動態獲取。主要功能是對apache common configuration類庫的擴展。在雲平臺開發中可以將其用作分佈式配置管理依賴構件。同時,它有如下一些特性:

  • 動態類型化屬性

  • 高效和線程安全的配置操作

  • 配置改變時的回調機制

  • 輪詢框架

  • JMX,通過Jconsole檢查和調用操作屬性

  • 組合配置

p_w_picpath.png

p_w_picpath.png

適用場景

對於傳統的單體應用,properties等配置文件可以解決配置問題,同時也可以通過maven profile配置來區別各個環境,但在一個幾百上千節點的的微服務生態中,微服務採用多種語言開發,配置文件格式多樣,如何把每個微服務的配置文件都進行更新,並且很多時候還需要重啓服務,是一件無法忍受的事情。所以,對於微服務架構而言,一個通用的配置中心是必不可少的。

新接口邏輯上線,老接口面臨遷移,開發測試完成後,馬上要上線。但是接口調用發的研發同學對新接口的穩定性、性能存在一定的質疑,爲了避免風險,要求可以上線後緊急切換回老接口。這時候我們就需要一個手動開關。所以對於類似需求,一個通用的配置中心是必不可少的。

Archaius提供的DynamicIntProperty類可以在配置發生變化時動態地獲取配置,並且不需要重啓應用,而底層的配置存儲,建議使用zookeeper進行存儲,Archaius作爲客戶端的類庫使用。

代碼案例

引入依賴

<dependency>
    <groupId>com.netflix.archaius</groupId>
    <artifactId>archaius-core</artifactId>
</dependency>

自定義Configuration

PropertiesConfiguration

public class PropertiesConfiguration extends DynamicConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesConfiguration.class);
    private static final int INITIAL_DELAY_MILLIS = 0;
    private static final int DELAY_MILLIS = 60 * 1000;
    private static final boolean IGNORE_DELETES_FROM_SOURCE = true;

    public PropertiesConfiguration(String confDir) {
        this(new String[]{confDir});
    }

    public PropertiesConfiguration(final String...confDirs) {
        String[] propertiesPaths = Lists.newArrayList(Iterables.concat(Iterables.transform(Arrays.asList(confDirs), new Function<String, List<String>>() {
            @Nullable
            @Override
            public List<String> apply(String confDir) {
                Assert.isTrue(new File(confDir).isDirectory(), StringUtil.format("路徑[{}]無法查找[.properties]文件", confDirs));
                String[] propertiesPaths = getPaths(confDir);
                if (ArrayUtils.isNotEmpty(propertiesPaths)) {
                    return Lists.newArrayList(propertiesPaths);
                } else {
                    
                    return Lists.newArrayList();
                }
            }
        }))).toArray(new String[0]);
        if (ArrayUtils.isNotEmpty(propertiesPaths)) {
            super.startPolling(new URLConfigurationSource(propertiesPaths), new FixedDelayPollingScheduler(INITIAL_DELAY_MILLIS, DELAY_MILLIS, IGNORE_DELETES_FROM_SOURCE));
        }
        ConfigurationLog.successInit(PropertiesConfiguration.class, this.getProperties());
    }

    private static String[] getPaths(String confDir) {
        try {
            URL configHome = new File(confDir).toURI().toURL();
            List<String> urls = new ArrayList<String>();
            for (String filename : FileUtil.scan(confDir, ".properties$")) {
                String url = configHome.toString() + filename;
                urls.add(url);
            }
            return urls.toArray(new String[urls.size()]);
        } catch (MalformedURLException e) {
            throw Throwables.propagate(e);
        }
    }
}

SystemConfiguration

public class SystemConfiguration extends ConcurrentMapConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemConfiguration.class);

    public SystemConfiguration() {
        super();
        this.loadProperties(System.getProperties());
        ConfigurationLog.successInit(SystemConfiguration.class, this.getProperties());
    }
}

同理,可以使用zookeeper client 封裝一個基於zookeeper的 ConcurrentMapConfiguration

初始化

private static final ConcurrentCompositeConfiguration compositeConfig = new ConcurrentCompositeConfiguration();

public synchronized static void init() {
    Preconditions.checkState(! hadInit, StringUtil.format("[{}]只能加載一次!", ConfigAdapter.class.getSimpleName()));
    Preconditions.checkState(compositeConfig.getConfigurations().size() > 1,
            StringUtil.format("[{}]沒有加載任何配置", ConfigAdapter.class.getSimpleName()));
    if (! ConfigurationManager.isConfigurationInstalled()) {
        ConfigurationManager.install(compositeConfig);
        Preconditions.checkState(ConfigurationManager.isConfigurationInstalled(), StringUtil.format("[{}]加載失敗!",
                ConfigAdapter.class.getSimpleName()));
    }
    Iterable<String> configurationNames = Iterables.transform(compositeConfig.getConfigurations(), new Function<AbstractConfiguration, String>() {
        @Nullable
        @Override
        public String apply(AbstractConfiguration input) {
            return input.getClass().getSimpleName();
        }
    });
    ConfigurationLog.successInit(ConfigAdapter.class, getAll());
    hadInit = true;
}

獲取值

 public static DynamicBooleanProperty getDynamicBool(String key, boolean defaultValue) {
        return getFactory().getBooleanProperty(key, defaultValue);
    }

private static DynamicPropertyFactory getFactory() {
        return DynamicPropertyFactory.getInstance();
    }

注意

  • 在設置的時刻獲取配置,配置源不會隨着System#properties裏面的配置更新而更新

  • 更新配置方法不會更新實際的property文件,僅僅爲更新內存數據,重啓後失效

  • 微服務都從配置中心動態的讀取配置信息,而配置中心又在從配置源同步配置,所以這裏就很自然的出現了一個讀寫安全的問題,好消息是Archaius已經解決了這個問題,Archaius是線程安全的,讀寫可以併發進行。


個人介紹:

高廣超:多年一線互聯網研發與架構設計經驗,擅長設計與落地高可用、高性能互聯網架構。

本文首發在 高廣超的簡書博客 轉載請註明!

p_w_picpath.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章