• Archaius 原理


    Archaius 原理

    Archaius是什么?

    Archaius提供了动态修改配置的值的功能,在修改配置后,不需要重启应用服务。其核心思想就是轮询配置源,每一次迭代,检测配置是否更改,有更改重新更新配置。

    一个简单的例子:

    1. 获取配置源
    /**
     * Created by longfei on 17/1/19.
     */
    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等)

  • 相关阅读:
    空矩形星星排列图源程序
    点名源程序
    计数器
    按键
    游戏石头剪刀布
    PCB自动生成总图和子图
    对于电脑中文件的一些处理
    一般电脑软件整体缩进和缩退快捷键
    java从小到大循环打印
    STM(WIFI模块)
  • 原文地址:https://www.cnblogs.com/dragonfei/p/6308581.html
Copyright © 2020-2023  润新知