Archaius是什么?

一、Archaius是什么?

Archaius用于动态的管理属性配置文件。
使用相关的API使用属性就可以实现动态的数性加载。

参考自Getting-Started

* 引入项目中*
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.6.0</version>
</dependency>

使用本地配置文件作为配置源
默认的,Archaius将查找classpath下名为config.properties文件并读取,这个配置文件可以包含在一个jar包的根路径下。
另外,你可以使用属性archaius.configurationSource.additionalUrls来包含url形式的文件,多个文件用逗号分割。

使用下面的API在程序中得到你需要的属性

// create a property whose value is type long and use 1000 as the default
// if the property is not defined
DynamicLongProperty timeToWait = DynamicPropertyFactory.getInstance().getLongProperty("lock.waitTime", 1000);

// ...
ReentrantLock lock = ...;

// ...
lock.tryLock(timeToWait.get(), TimeUnit.MILLISECONDS); // timeToWait.get() returns up-to-date value of the property

默认的:Archaius会每分钟去重新加载下属性配置
注意:配置多属性文件时的属性覆盖,最后读到的属性会覆盖前面相同的属性

列出我们可以修改的一些系统属性

Operation HTTP action Notes
archaius.configurationSource.defaultFileName 指定Archaius默认加载的配置源属性文件名,默认:classpath:config.properties config.properties
archaius.fixedDelayPollingScheduler.initialDelayMills 延迟加载,默认30秒 30000
archaius.fixedDelayPollingScheduler.delayMills 两次属性读取时间间隔,默认1分钟 60000

高级使用:自定义configuration source和polling scheduler,即自己设计动态属性配置方案。

二、一个简单的例子

1. 获取配置源
public class DynamicConfigurationSource implements PolledConfigurationSource {
    @Override
    public PollResult poll(boolean initial,Object checkPoint) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("test",UUID.randomUUID().toString());
        return PollResult.createFull(map);
    }
}
2. 定义调度器
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(2000,2000,false);
3. 定义动态配置
DynamicConfiguration configuration = new DynamicConfiguration(source,scheduler);
4.简单单元测试
    @org.testng.annotations.Test
    public void testArchaius() throws Exception {
        PolledConfigurationSource source = new DynamicConfigurationSource();
        AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(2000,2000,false);
        DynamicConfiguration configuration = new DynamicConfiguration(source,scheduler);
        ConfigurationManager.install(configuration);
        final DynamicStringProperty stringProperty = DynamicPropertyFactory.getInstance().getStringProperty("test","nodata");
        Helpers.subscribePrint(Observable.interval(1,TimeUnit.SECONDS).take(20).doOnNext(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        System.out.println(stringProperty.get());
                    }
                }),"test");
        TimeUnit.MINUTES.sleep(1);
    }

实现

1. 启动轮询任务
public synchronized void startPolling(PolledConfigurationSource source, AbstractPollingScheduler scheduler) {
    this.scheduler = scheduler;
    this.source = source;
    init(source, scheduler);
    scheduler.startPolling(source, this);
}
2.轮询的Runnable和初始化:实现是一致的
    PollResult result = null;
    try {
       result = source.poll(false,getNextCheckPoint(checkPoint));
       checkPoint = result.getCheckPoint();
       fireEvent(EventType.POLL_SUCCESS, result, null);
       } catch (Throwable e) {
       log.error("Error getting result from polling source", e);
       fireEvent(EventType.POLL_FAILURE, null, e);
       return;
       }
       try {
          populateProperties(result, config);
       } catch (Throwable e) {
           log.error("Error occured applying properties", e);
      }                 

注意到,会调用source.poll方法,即PolledConfigurationSource的polled,我们实现的数据源接口,可以自定义数据源(jdbc,文件,scm等)

时间: 2024-08-27 07:52:27

Archaius是什么?的相关文章

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

Archaius 配置管理API,包含一系列配置管理API,提供动态类型化属性.线程安全配置操作.轮询框架.回调机制等功能. 概述 archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取.主要功能是对apache common configuration类库的扩展.在云平台开发中可以将其用作分布式配置管理依赖构件.同时,它有如下一些特性: 动态类型化属性 高效和线程安全的配置操作 配置改变时的回调机制 轮询框架 JMX,通过Jconsole检查

archaius源码分析之概述

archaius源码包括以下几个模块: 配置存储文件 就是配置实际存储配置的地方 配置源 获取配置文件数据的地方,通过配置源可以快速获取配置信息.详见:archaius源码分析之配置源 配置管理 实现配置读取,动态更新配置.参见配置:archaius源码分析之配置管理 属性对象 通过配置管理获取配置,实现以配置项来获取配置,具有缓存和指定类型特性. 配置 实现了apache commom 配置接口,对配置源进行管理,动态更新配置. 原文地址:https://www.cnblogs.com/zha

archaius源码分析之配置源

配置源 配置源定义和实现了获取配置文件的方式.有两种配置源,一种是主动拉去方式获取配置,一种是被动监听方式获取配置 类图结构: 接口PolledConfigurationSource定义了获取配置的方法 public PollResult poll(boolean initial, Object checkPoint) throws Exception; initial是否是初次获取,checkPoint上次获取的检查点. JDBCConfigurationSource 从数据库中获取配置信息.

archaius源码分析之属性对象

属性对象针对每个属性一对象方式实现操作. 结构如下: Property 定义了属性对象的基本方法,主要为获取属性值,获取默认值,获取属性命令,管理回调函数. public interface Property<T> { T getValue(); T getDefaultValue(); String getName(); long getChangedTimestamp(); void addCallback(Runnable callback); void removeAllCallbac

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

Spring cloud整体框架

研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统构建的要求,使我们以非常低的成本(技术或者硬件)搭建一套高效.分布式.容错的平台,但Spring Cloud也不是没有缺点,小型独立的项目不适合使用,另外对分布式事物的支持暂时也没有. Spring Cloud是什么鬼? Spring Cloud是一个基于Spring Boot实现的云应用开发工具,

Spring cloud子项目

目前来说spring主要集中于spring boot(用于开发微服务)和spring cloud相关框架的开发,我们从几张图着手理解,然后再具体介绍: spring cloud子项目包括: Spring Cloud Config:配置管理开发工具包,可以让你把配置放到远程服务器,目前支持本地存储.Git以及Subversion. Spring Cloud Bus:事件.消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署. Sprin

第五章 服务熔断(hystrix)+ retrofit底层通信(AsyncHttpclient)

一.集群容错 技术选型:hystrix.(就是上图中熔断器) 熔断的作用: 第一个作用: 假设有两台服务器server1(假设可以处理的请求阈值是1W请求)和server2,在server1上注册了三个服务service1.service2.service3,在server2上注册了一个服务service4,假设service4服务响应缓慢,service1调用service4时,一直在等待响应,那么在高并发下,很快的server1处很快就会达到请求阈值(server1很快就会耗尽处理线程)之后

SpringCloud分布式开发五大神兽

SpringCloud分布式开发五大神兽 服务发现--Netflix Eureka 客服端负载均衡--Netflix Ribbon 断路器--Netflix Hystrix 服务网关--Netflix Zuul 分布式配置--Spring Cloud Config Eureka 一个RESTful服务,用来定位运行在AWS地区(Region)中的中间层服务.由两个组件组成:Eureka服务器和Eureka客户端.Eureka服务器用作服务注册服务器.Eureka客户端是一个java客户端,用来简