Actor - Routing

概况

routing这个东西俗称路由.
akka里面的主要角色,就是一个中专分发工作站.
作为一个局部统一消息入口,根据不同的router 种类和配置 对管辖的routee进行消息分发,比较方便扩展,可以直接增加实例数量

Pool vs Group

主要区别是 Pool下的routee都是router自己的子actor
Group则没有这个限制,所以造成Pool的监管策略是对routee起作用的

简单实例

类型很多,选择的时候还是要看文档

class EchoActor extends Actor with ActorLogging {
def receive: Receive = {
case message =>
log.info("Received Message {} in Actor {}", message, self.path.name)
}
}

object RouteTest extends App {
val _system = ActorSystem("Router")
val randomRouter = _system.actorOf(FromConfig.props(Props[EchoActor]), name = "router1")
1 to 200 foreach {
i =>
// println(i)
randomRouter ! i
}
}

根据配置 实例顶级的router actor,然后给router发 200个消息

akka.actor.deployment {
/router1 {
// router = round-robin-pool
// router = random-pool
router = smallest-mailbox-pool
// router = broadcast-pool
// nr-of-instances = 10
resizer {
lower-bound = 2
upper-bound = 15
messages-per-resize = 100
}
}
}

推荐是application.conf里配置,最好不要在代码里配置

Output

[INFO] [05/10/2017 13:46:41.710] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$a] Received Message 1 in Actor $a
[INFO] [05/10/2017 13:46:41.710] [Router-akka.actor.default-dispatcher-5] [akka://Router/user/router1/$b] Received Message 2 in Actor $b
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$a] Received Message 3 in Actor $a
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-5] [akka://Router/user/router1/$b] Received Message 4 in Actor $b
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$a] Received Message 5 in Actor $a
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-5] [akka://Router/user/router1/$b] Received Message 6 in Actor $b
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$a] Received Message 7 in Actor $a
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$a] Received Message 9 in Actor $a
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-5] [akka://Router/user/router1/$b] Received Message 8 in Actor $b
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-5] [akka://Router/user/router1/$b] Received Message 10 in Actor $b
[INFO] [05/10/2017 13:46:41.711] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 12 in Actor $b
[INFO] [05/10/2017 13:46:41.712] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 14 in Actor $b
[INFO] [05/10/2017 13:46:41.712] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 16 in Actor $b
[INFO] [05/10/2017 13:46:41.712] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 18 in Actor $b
[INFO] [05/10/2017 13:46:41.712] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 20 in Actor $b
[INFO] [05/10/2017 13:46:41.713] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 22 in Actor $b
[INFO] [05/10/2017 13:46:41.713] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 24 in Actor $b
[INFO] [05/10/2017 13:46:41.713] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 26 in Actor $b
[INFO] [05/10/2017 13:46:41.713] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 28 in Actor $b
[INFO] [05/10/2017 13:46:41.713] [Router-akka.actor.default-dispatcher-4] [akka://Router/user/router1/$b] Received Message 30 in Actor $b
[INFO] [05/10/2017 13:46:41.714] [Router-akka.actor.default-dispatcher-7] [akka://Router/user/router1/$b] Received Message 32 in Actor $b
[INFO] [05/10/2017 13:46:41.714] [Router-akka.actor.default-dispatcher-7] [akka://Router/user/router1/$b] Received Message 34 in Actor $b
[INFO] [05/10/2017 13:46:41.715] [Router-akka.actor.default-dispatcher-7] [akka://Router/user/router1/$b] Received Message 36 in Actor $b
[INFO] [05/10/2017 13:46:41.715] [Router-akka.actor.default-dispatcher-7] [akka://Router/user/router1/$b] Received Message 38 in Actor $b

后面的 $a $b就是子actor routee 的名字,可以看到分发的情况