Apollo(阿波羅)是攜程框架部門研發(fā)的分布式配置中心,能夠集中化管理應(yīng)用不同環(huán)境、不同集群的配置,配置修改后能夠?qū)崟r(shí)推送到應(yīng)用端,并且具備規(guī)范的權(quán)限、流程治理等特性,適用于微服務(wù)配置管理場景。
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 對定時(shí)任務(wù) cron 語句的熱配置,需要使用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:13
cron_test2:0/5 * * * * ?
current_time2:10:53:13
在 Apollo 中將 cron 語句改為 0/10 * * * * ?
,不需要重啟,即可看到項(xiàng)目的輸出變?yōu)椋?/p>
cron_test1:0/10 * * * * ?
current_time2:10:54:05
cron_test2:0/10 * * * * ?
current_time1:10:54:05
-
JAVA
+關(guān)注
關(guān)注
20文章
2983瀏覽量
106499 -
Apollo
+關(guān)注
關(guān)注
5文章
345瀏覽量
18655 -
GitHub
+關(guān)注
關(guān)注
3文章
481瀏覽量
17320
發(fā)布評論請先 登錄
相關(guān)推薦
cron和crontab命令
如何對Apollo2.5 CANBUS進(jìn)行全面調(diào)試?
apollo2外掛的32.768kHz的晶振不起振的原因
Ambiq Apollo4的相關(guān)資料推薦
Apollo Heritage版音頻接口附帶的插件
在STM32F429-Apollo開發(fā)板配置以太網(wǎng)報(bào)錯(cuò)怎么辦
開關(guān)語句和循環(huán)語句
Linux操作系統(tǒng)定時(shí)任務(wù)系統(tǒng)Cron入門分析
Apollo計(jì)算平臺(tái)配置經(jīng)驗(yàn)分享:5000元搞定
PHP簡單實(shí)現(xiàn)不依賴于Unix系統(tǒng)Cron的定時(shí)任務(wù)程序資料說明

評論