Netflix Archaius 分布式配置管理依赖构件

简介: Archaius 配置管理API,包含一系列配置管理API,提供动态类型化属性、线程安全配置操作、轮询框架、回调机制等功能。概述archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取。

Archaius 配置管理API,包含一系列配置管理API,提供动态类型化属性、线程安全配置操作、轮询框架、回调机制等功能。

概述

archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取。主要功能是对apache common configuration类库的扩展。在云平台开发中可以将其用作分布式配置管理依赖构件。同时,它有如下一些特性:

  • 动态类型化属性
  • 高效和线程安全的配置操作
  • 配置改变时的回调机制
  • 轮询框架
  • JMX,通过Jconsole检查和调用操作属性
  • 组合配置
img_2e96e88c42d2a78a88d44754a30da40e.png
image.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是线程安全的,读写可以并发进行。

个人介绍:

高广超:多年一线互联网研发与架构设计经验,擅长设计与落地高可用、高性能互联网架构。

本文首发在 高广超的简书博客 转载请注明!

img_7015b3c64a6b1e4a95d4739adf2bbaa0.png
image.png
目录
相关文章
|
11月前
|
消息中间件 网络协议 C#
C#使用Socket实现分布式事件总线,不依赖第三方MQ
`CodeWF.EventBus.Socket` 是一个轻量级的、基于Socket的分布式事件总线系统,旨在简化分布式架构中的事件通信。它允许进程之间通过发布/订阅模式进行通信,无需依赖外部消息队列服务。
C#使用Socket实现分布式事件总线,不依赖第三方MQ
|
12月前
|
消息中间件 Java 对象存储
数据一致性挑战:Spring Cloud与Netflix OSS下的分布式事务管理
数据一致性挑战:Spring Cloud与Netflix OSS下的分布式事务管理
158 2
|
存储 安全 Java
实现基于Spring Cloud的分布式配置管理
实现基于Spring Cloud的分布式配置管理
|
Java 开发工具 git
Spring Cloud中的分布式配置管理
Spring Cloud中的分布式配置管理
|
Java 开发工具 数据安全/隐私保护
Spring Cloud中的分布式配置管理最佳实践
Spring Cloud中的分布式配置管理最佳实践
|
Java 开发工具 git
Spring Cloud中的分布式配置管理
Spring Cloud中的分布式配置管理
|
存储 Java 开发工具
Spring Cloud中的分布式配置管理策略
Spring Cloud中的分布式配置管理策略
|
消息中间件 缓存 NoSQL
阿里云国际站代理商:Redis实现分布式配置管理的方法与应用案例
@luotuoemo飞机@TG阿里云国际站代理商:Redis实现分布式配置管理的方法与应用案例,为了实现高可用和负载均衡,可以将Redis部署成哨兵集群或集群模式。哨兵负责监控主从节点的状态,发现故障时自动进行故障转移。集群模式可以提高系统的可扩展性,通过添加更多的从节点来分摊负载压力。
|
算法 Java
分布式系统中引入物理时钟依赖
在分布式系统中,由于节点之间的通信存在网络延迟和不可靠性等因素,为了保证数据的一致性和正确性,通常需要引入物理时钟来对节点之间的事件进行排序和同步。当多个节点使用不同的本地时钟时,它们之间的时间戳可能存在不一致的情况,因此需要使用一些算法来解决这个问题,例如 Lamport 时钟和向量时钟等。
147 1
|
Java 关系型数据库 MySQL
分布式配置管理中心Apollo
分布式配置管理中心Apollo

热门文章

最新文章