我有一个基于 Play Framework 的服务,它是无状态的,旨在跨多台机器部署以进行水平扩展。
此服务正在处理 HTTP JSON 请求和响应,并再次使用 CouchDB 作为其数据存储以实现最大的可扩展性。
我们有少量后台作业需要在整个集群中每 X 秒运行一次。作业不要在每台机器上同时执行,这一点至关重要。
为了执行我们正在使用 Actors 和 Akka Scheduler 的作业(因为我们正在使用 Scala):
Akka.system().scheduler.schedule(
Duration.create(0, TimeUnit.MILLISECONDS),
Duration.create(10, TimeUnit.SECONDS),
Akka.system().actorOf(LoggingJob.props),
"tick")
(等)
object LoggingJob {
def props = Props[LoggingJob]
}
class LoggingJob extends UntypedActor {
override def onReceive(message: Any) {
Logger.info("Job executed! " + message.toString())
}
}
有没有:
- Akka/Actors/Play 中任何我错过的内置技巧都能为我做到这一点吗?
- 或者我可以在 Couchbase(分布式互斥锁?不完全是?)之上放置一个公认的算法来做到这一点?
我不想让任何实例变得“特殊”,因为它需要非常易于部署和管理。
最佳答案
查看 Akka 的 Cluster Singleton Pattern .
For some use cases it is convenient and sometimes also mandatory to ensure that you have exactly one actor of a certain type running somewhere in the cluster.
Some examples:
- single point of responsibility for certain cluster-wide consistent decisions, or coordination of actions across the cluster system
- single entry point to an external system
- single master, many workers
- centralized naming service, or routing logic
Using a singleton should not be the first design choice. It has several drawbacks, such as single-point of bottleneck. Single-point of failure is also a relevant concern, but for some cases this feature takes care of that by making sure that another singleton instance will eventually be started.
关于scala - 后台作业服务,如何确保每个集群的作业只定期运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18615048/