具有多个 JVM 的多个实例的应用程序中的 Spring Scheduler 代码 [英] Spring Scheduler code within an App with multiple instances with multiple JVMs
问题描述
我有一个使用 fixedDelay
或 cron
配置的 spring 调度程序任务,并且此应用程序的多个实例在多个 JVM 上运行.
I have a spring scheduler task configured with either of fixedDelay
or cron
, and have multiple instances of this app running on multiple JVMs.
默认行为是所有实例都在执行调度程序任务.
The default behavior is all the instances are executing the scheduler task.
有没有一种方法可以控制这种行为,以便只有一个实例会执行调度程序任务,而其他实例则不会.
Is there a way by which we can control this behavior so that only one instance will execute the scheduler task and others don't.
如果您知道任何方法,请告诉我.
Please let me know if you know any approaches.
谢谢
推荐答案
我们遇到了类似的问题.我们是这样修复的:
We had similar problem. We fixed it like this:
- 从我们的 Spring Boot 服务中删除了所有
@Scheduled
bean. - 根据所需的时间表创建了 AWS Lambda 函数.
- Lambda 函数通过调度请求访问我们的顶级域.
- 负载平衡器将此请求转发到服务实例之一.
通过这种方式,我们可以确保计划任务在我们的服务集群中只执行一次.
This way we are sure that scheduled task is executed only once across the cluster of our services.
这篇关于具有多个 JVM 的多个实例的应用程序中的 Spring Scheduler 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!