<ul id="seequ"><sup id="seequ"></sup></ul><tfoot id="seequ"></tfoot>
  • <tfoot id="seequ"><delect id="seequ"></delect></tfoot>
  • <ul id="seequ"></ul>
  • <small id="seequ"></small>

    今熱點(diǎn):Apollo實(shí)現(xiàn)cron語(yǔ)句的熱配置

    2023-03-24 14:11:00 來(lái)源:騰訊云

    GitHub項(xiàng)目地址


    (資料圖片)

    Gitee項(xiàng)目地址

    Apollo(阿波羅)是攜程框架部門(mén)研發(fā)的分布式配置中心,能夠集中化管理應(yīng)用不同環(huán)境、不同集群的配置,配置修改后能夠?qū)崟r(shí)推送到應(yīng)用端,并且具備規(guī)范的權(quán)限、流程治理等特性,適用于微服務(wù)配置管理場(chǎng)景。

    Apollo 與 properties 配置文件的功能相同,都可以設(shè)置參數(shù)。Apollo 的優(yōu)點(diǎn)在于,可以實(shí)時(shí)修改參數(shù)的值,而不需要重啟項(xiàng)目。

    1 配置 Apollo

    本地配置 Apollo 的方式參考:

    2 添加 Apollo 參數(shù)

    在Apollo中添加參數(shù):

    param.cron_test1=0/5 * * * * ?param.cron_test2=0/5 * * * * ?

    要實(shí)現(xiàn) Apollo 對(duì)定時(shí)任務(wù) cron 語(yǔ)句的熱配置,需要使用ScheduledTaskRegistrar

    具體方式如下:

    @Override    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {        Runnable runnable  = () -> {            System.out.println("cron_test:" + cron_test);        };        Trigger trigger  = triggerContext -> {            CronTrigger cronTrigger = new CronTrigger(cron_test);            return cronTrigger.nextExecutionTime(triggerContext);        };        taskRegistrar.addTriggerTask(runnable , trigger );    }    @Override    public int getOrder() {        return 0;    }

    完整項(xiàng)目地址:

    GitHub地址:https://github.com/Snowstorm0/learn-apollo-cron

    Gitee地址:https://gitee.com/Snowstorm0/learn-apollo-cron

    在運(yùn)行項(xiàng)目之前需要修改 resource/application.properties 中 apollo.meta 的值,將 localhost 替換為 apollo 的地址,端口號(hào)為 Eureka 的端口號(hào)(默認(rèn)為8080)。

    運(yùn)行該項(xiàng)目,可以看到輸出:

    cron_test1:0/5 * * * * ?current_time1:10:53:13cron_test2:0/5 * * * * ?current_time2:10:53:13

    在 Apollo 中將 cron 語(yǔ)句改為 0/10 * * * * ?,不需要重啟,即可看到項(xiàng)目的輸出變?yōu)椋?/p>

    cron_test1:0/10 * * * * ?current_time2:10:54:05cron_test2:0/10 * * * * ?current_time1:10:54:05

    標(biāo)簽:

    上一篇:
    上一篇: