仅在一个服务器实例上激活批处理 [英] Activate Batch on only one Server instance

查看:61
本文介绍了仅在一个服务器实例上激活批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个tomcat实例的前面都有一个nginx负载均衡器,每个实例都包含一个spring boot应用程序.每个spring boot应用程序执行一个批处理,该批处理将数据写入数据库.该批处理每天凌晨1点执行.问题在于两个实例同时执行我不希望执行的批处理.

I have a nginx loadbalancer in front of two tomcat instances each contains a spring boot application. Each spring boot application executes a batch that writes data in a database. The batch executes every day at 1am. The problem is that both instances execute the batch simultaniously which i don't want.

有没有一种方法可以将批处理部署在两个实例中,并告诉tomcat或nginx在主服务器中启动该批处理(而从属服务器不运行该批处理).

Is there a way to keep the batchs deployed in two instances and tell tomcat or nginx to start the batch in master server (and the slave server doesn't run the batch).

如果其中一台服务器停止运行,则第二台服务器可以代表他启动批处理.

If one of the servers stops, the second server could start the batch on his behalf.

nginx或tomcat(或某些其他技术)中是否有工具可以做到这一点?

Is there a tool in nginx or tomcat (or some other technology) to do that ?

先谢谢您

推荐答案

这是一个简单的设计方法.

Here is a simplistic design approach.

由于在同时触发的两个VM中有两个计划的方法,因此请为这两个VM添加一个随机延迟.这个答案有许多关于如何将触发延迟随机时间的选择.Spring @Scheduled 注解随机延迟

Since you have two scheduled methods in the 2 VMs triggered at same time, add a random delay to both. This answer has many options on how to delay the trigger for a random duration. Spring @Scheduled annotation random delay

在方法内部,仅当尚未由其他VM启动该作业时,才运行该作业.可以使用一个新表来对此进行跟踪.

Inside the method run the job only if it is NOT already started (by the other VM). This could be done with a new table to track this.

这是此设计的伪代码:

@Scheduled(cron = "schedule expression")
public void batchUpdateMethod() {
     //Check database for signs of job running now.
     if (job is not running){
         //update database table to indicate job is running
         //Run the batch job
         //update database table to indicate job is finished
     }
}

由于这两个VM彼此独立,因此应该将数据库或某些公共文件位置用作锁定,以在两次运行之间进行同步.

The database, or some common file location, should be used as a lock to sync between the two runs, since the two VMs are independent of each other.

要获得更强大的设计,请考虑使用Spring Batch Spring Batch将数据库用于其作业(JobsRepository).默认情况下,内存数据源用于跟踪正在运行的作业及其状态.在您的设置中,这两个实例(很可能)使用它们自己的内存数据库.如果共享JobsRepository数据库,Spring Batch的多个实例可以作为一个群集彼此协调,一个实例可以运行作业,而另一个actasa备份可以运行.为此,您需要将2个实例配置为使用公共数据源.

For a more robust design, consider Spring Batch Spring Batch uses a database for its jobs (JobsRepository). By default an in memory datasource is used to keep track of running jobs and their status. In your setup, the 2 instances are (most likely) using their own in memory database. Multiple instances of Spring Batch can coordinate with each other as a cluster and one can run jobs, while the other actasa backup, if the jobsRepository database is shared. For this you need to configure the 2 instances to use a common datasource.

以下是一些文档: https://docs.spring.io/spring-batch/docs/current/reference/html/index-single.html#jobrepository

https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#configuringJobRepository

这篇关于仅在一个服务器实例上激活批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆