在 Docker 集群上安排应用程序 Spring

Schedule application Spring on Docker cluster

我需要在具有不同节点的 Docker 集群中批量安排我的应用程序 Spring。 我在 docker-compose 上找到了 set replicas=1 的解决方案,但我认为这不是最佳解决方案,因为它最大限度地减少了 Docker.

的可能性

有什么帮助或建议吗?谢谢。

如果我对你的理解是正确的,你想要 运行 一个 spring 应用程序的几个副本(这是否由 docker、k8s 管理并不重要,它是独立的, ETC)。然后您希望仅在一个实例上启动后台作业。正确的?在这种情况下,我可能会建议您查看 ShedLock

ShedLock does one and only one thing. It makes sure your scheduled tasks are executed at most once at the same time. If a task is being executed on one node, it acquires a lock which prevents execution of the same task from another node (or thread). Please note, that if one task is already being executed on one node, execution on other nodes does not wait, it is simply skipped.

它在 Spring 中顺利集成。例如,计划的批处理作业可能如下所示:

@Scheduled(cron = ...)
@SchedulerLock(name = "scheduledTaskName")
public void scheduledTask() {
   // do something
}

可以在后台使用各种选项来实现分布式锁,例如MySQL、Redis、Zookeeper 等