运行 仅在一台服务器上进行批处理

Run batch only on one server

我有一个 Spring 引导 MVC 和批处理应用程序。批处理和 MVC 共享 DAO 和服务层,因此它们位于同一个 war 文件中。它们被部署到 4 个云服务器中,并且为 UI 应用程序配置了负载平衡和 vip。所以MVC应用程序没问题。

问题是作为批处理的一部分,我将文件 FTP 发送到外部服务器,然后外部服务器 FTP 将处理后的文件返回。处理后的文件仅返回到 4 个服务器中的一个。所以我希望批处理仅在 1 台服务器上 运行。我如何禁止批处理在其他服务器上执行。

这可以是许多方法中的一种,但实现的方法是在 属性 文件中保留一个值并将其值设置为布尔值 true

现在仅当 属性 文件值为 true 时才将您的批次处理为 运行。

通过这种方式,您可以灵活地更改要处理批处理作业的服务器。

解决方案变得更容易,因为您的 4 个实例 运行 在 4 个不同的云服务器上。批处理的起点可以是文件轮询器。因此,如果将文件放入服务器 1 上的轮询目录中,将调用服务器 1 上的批处理作业。其他实例不执行任何操作,因为该服务器上没有删除任何文件。

您需要在 spring 批处理之前集成文件轮询器。像这样 - http://docs.spring.io/spring-batch/reference/html/springBatchIntegration.html

<int:channel id="inboundFileChannel"/>
<int:channel id="outboundJobRequestChannel"/>
<int:channel id="jobLaunchReplyChannel"/>

<int-file:inbound-channel-adapter id="filePoller"
channel="inboundFileChannel"
directory="file:/tmp/myfiles/"
filename-pattern="*.csv">
<int:poller fixed-rate="1000"/>
</int-file:inbound-channel-adapter>

<int:transformer input-channel="inboundFileChannel"
output-channel="outboundJobRequestChannel">.    <bean class="io.spring.sbi.FileMessageToJobRequest">
<property name="job" ref="personJob"/>
<property name="fileParameterName" value="input.file.name"/>
 </bean>
</int:transformer>

<batch-int:job-launching-gateway request-channel="outboundJobRequestChannel"
reply-channel="jobLaunchReplyChannel"/>