Flume NG原始碼分析(二)支援執行時動態改動配置的配置模組

來源:互聯網
上載者:User

標籤:read   .post   pos   ide   代碼   super   基於   data   void   

在上一篇中講了Flume NG配置模組主要的介面的類,PropertiesConfigurationProvider提供了基於properties設定檔的靜態配置的能力,這篇細說一下PollingPropertiesFileConfigurationProvider提供的執行時動態改動配置並生效的能力。


要實現動態改動設定檔並生效,主要有兩個待實現的功能

1. 觀察設定檔是否改動

2. 假設改動,將改動的內容通知給觀察者


對於第一點,監控設定檔是否改動,Flume NG定義了一個FileWatcherRunnable對象來監控設定檔,啟動了一個單獨的線程採用定時輪詢的方式來監控,輪詢頻率是30毫秒一次。比較file.lastModified屬性與lastChange時間戳記,當file.lastModified > lastChange時表示檔案被改動

public class FileWatcherRunnable implements Runnable {    private final File file;    private final CounterGroup counterGroup;    private long lastChange;    public FileWatcherRunnable(File file, CounterGroup counterGroup) {      super();      this.file = file;      this.counterGroup = counterGroup;      this.lastChange = 0L;    }    @Override    public void run() {      LOGGER.debug("Checking file:{} for changes", file);      counterGroup.incrementAndGet("file.checks");      long lastModified = file.lastModified();      if (lastModified > lastChange) {        LOGGER.info("Reloading configuration file:{}", file);        counterGroup.incrementAndGet("file.loads");        lastChange = lastModified;        try {          eventBus.post(getConfiguration());        } catch (Exception e) {          LOGGER.error("Failed to load configuration data. Exception follows.",              e);        } catch (NoClassDefFoundError e) {          LOGGER.error("Failed to start agent because dependencies were not " +              "found in classpath. Error follows.", e);        } catch (Throwable t) {          // caught because the caller does not handle or log Throwables          LOGGER.error("Unhandled error", t);        }      }    }  }// PollingPropertiesFileConfigurationProvider.start()啟動一個單獨的線程來監控properties設定檔 public void start() {    LOGGER.info("Configuration provider starting");    Preconditions.checkState(file != null,        "The parameter file must not be null");    executorService = Executors.newSingleThreadScheduledExecutor(            new ThreadFactoryBuilder().setNameFormat("conf-file-poller-%d")                .build());    FileWatcherRunnable fileWatcherRunnable =        new FileWatcherRunnable(file, counterGroup);    executorService.scheduleWithFixedDelay(fileWatcherRunnable, 0, interval,        TimeUnit.SECONDS);    lifecycleState = LifecycleState.START;    LOGGER.debug("Configuration provider started");  }

對於第二點,利用Guava EventBus提供的公布訂閱模式機制,將配置改動封裝成事件傳遞給Application。來又一次載入配置

// FileWatcherRunnable.run方法 公布配置改動的事件   eventBus.post(getConfiguration());// Application.main方法來注冊事件訂閱      Application application;      if(reload) {        EventBus eventBus = new EventBus(agentName + "-event-bus");        PollingPropertiesFileConfigurationProvider configurationProvider =            new PollingPropertiesFileConfigurationProvider(agentName,                configurationFile, eventBus, 30);        components.add(configurationProvider);        application = new Application(components);        eventBus.register(application);      }// Application類採用@Subscribe標註來定義訂閱者法,即配置改動後會運行handleConfigurationEvent方法,這種方法是安全執行緒的@Subscribe  public synchronized void handleConfigurationEvent(MaterializedConfiguration conf) {    stopAllComponents();    startAllComponents(conf);  }


Flume NG原始碼分析(二)支援執行時動態改動配置的配置模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.