Akka和ReactiveMongo [英] Akka and ReactiveMongo

查看:97
本文介绍了Akka和ReactiveMongo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到最佳方法,与集群工作者在参与者之间共享相同的连接池.我有以下结构:

I am trying to find the best approach to sharing the same pool of connection between actors withing the cluster workers. I have the following structure:

主要演员->工人演员(最多100个或更多)-> MongoDB

Master Actor -> Worker Actors(can be up to 100 or more) -> MongoDB

在工作人员和MongoDB之间,我想放置reactmongo,但是我不确定如何在所有参与者之间提供连接池共享.

Between workers and MongoDB I want to put reactivemongo, however I am not sure how exactly to provide connection pool sharing between all actors.

根据reactmongo文档:

According to reactivemongo documentation:

MongoDriver实例管理参与者系统;连接管理连接池.通常,MongoDriver或创建MongoConnection绝不会被多次实例化.您可以提供一台或多台服务器的列表;驱动程序将猜测它是独立服务器还是副本集配置.即使有一个副本节点,驱动程序也会探测其他节点并自动添加它们.

A MongoDriver instance manages an actor system; a connection manages a pool of connections. In general, MongoDriver or create a MongoConnection are never instantiated more than once. You can provide a list of one ore more servers; the driver will guess if it's a standalone server or a replica set configuration. Even with one replica node, the driver will probe for other nodes and add them automatically.

我应该只在Master actor中创建它,然后将其与每个消息捆绑在一起吗? 因此,这将在Master actor中进行:

Should I just create it in Master actor and then bundle with each message? So, this would be in Master actor:

val driver = new MongoDriver
val connection = driver.connection(List("localhost"))

然后我在消息中将连接传递给演员.还是应该查询每个Work Actor中的连接并在消息中仅传递驱动程序?

And then I pass connection to actors in a message. Or should I query a connection in each Work Actor and pass just driver in a message?

非常感谢您的帮助. 谢谢.

Any help is very appreciated. Thanks.

推荐答案

我将在主演员中创建driverconnection.然后,我将设置工作人员角色以使用MongoConnection的实例作为构造函数参数,以便每个工作人员都具有对连接的引用(实际上是对连接池的代理).然后,在类似preStart的东西中,让主角色创建工作程序(我假设已路由这些工作程序)并将连接作为arg提供.一个非常简化的示例可能看起来像这样:

I would create the driver and connection in the master actor. I would then set up the the worker actors to take an instance of MongoConnection as a constructor argument so that each worker has a reference to the connection (which is really a proxy to a pool of connections). Then, in something like preStart, have the master actor create the workers (which I am assuming are routed) and supply the connection as an arg. A very simplified example could look like this:

class MongoMaster extends Actor{
  val driver = new MongoDriver
  val connection = driver.connection(List("localhost"))

  override def preStart = {
    context.actorOf(Props(classOf[MongoWorker], connection).withRouter(FromConfig()))
  } 

  def receive = {
    //do whatever you need here
    ...
  }
}

class MongoWorker(conn:MongoConnection) extends Actor{
  def receive = {
    ...
  }
}

此代码并不准确,但至少它显示了我所描述的高级概念.

This code is not exact, but at least it shows the high level concepts I described.

这篇关于Akka和ReactiveMongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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