Apollo is a distributed configuration center developed by Ctrip's framework department. It can centrally manage the configuration of different application environments and different clusters. After the configuration is modified, it can be pushed to the application end in real time, and it has standardized permissions, process governance and other features. Suitable for microservice configuration management scenarios.
Apollo has the same function as the properties configuration file, and parameters can be set. The advantage of Apollo is that the values of parameters can be modified in real time without restarting the project.
1 Configure Apollo
Reference for how to configure Apollo locally:
2 Add Apollo parameters
Add parameters in Apollo:
param.cron_test1=0/5 * * * * ?
param.cron_test2=0/5 * * * * ?
To implement Apollo's hot configuration of scheduled task cron statements, you need to use ScheduledTaskRegistrar
.
The specific methods are as follows:
@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;
}
Complete project address:
GitHub address: https://github.com/Snowstorm0/learn-apollo-cron
Gitee address: https://gitee.com/Snowstorm0/learn-apollo-cron
Before running the project, you need to modify the value resource/application.properties
in , replace with the address of , and the port number is the port number of Eureka (default is 8080).apollo.meta
localhost
apollo
Run the project and you can see the output:
cron_test1:0/5 * * * * ?
current_time1:10:53:13
cron_test2:0/5 * * * * ?
current_time2:10:53:13
Change the cron statement in Apollo to 0/10 * * * * ?
, without restarting, you can see the output of the project becomes:
cron_test1:0/10 * * * * ?
current_time2:10:54:05
cron_test2:0/10 * * * * ?
current_time1:10:54:05
To learn more programming knowledge, please follow my official account: