Akka(5): ConsistentHashing Router - 可选定Routee的任务分配模式
上一篇讨论里我们介绍了几种任务分配(Routing)模式。Akka提供的几种现成智能化Routing模式大多数是通过对用户屏蔽具体的运算Routee选择方式来简化Router使用,提高智能程度,所以我们提到Router的运算是一种无序的运算,消息之间绝对不容许任何形式的依赖,因为向Router发送的消息可能在任何Routee上运算。但是,如果我们能够把运算任务按照任务的类型分配给专门负责处理此等类型任务的Routee,那么我们就可以充分利用Routing模式所带来的运算拓展能力来提高整体运算效率。Akka的ConsistentHashingRouter就是为了满足这样的需求而提供的。ConsistentHashingRouter是通过消息的特征来分辨消息类型,然后自动构建和管理处理各种类型消息的Routees。当然,这就要求系统的消息必须具备预先设定的特征,使ConsistentHashingRouter可以正确分辨并分配给指定的Routee去运算。如果我们确定只有一个Routee负责处理一种类型消息的话,甚至可以在这个Routee中维护某种状态。我们可以设计一个场景来示范ConsistentHashingRouter的应用:模拟一个多货币的存钱盒,分n次随意从盒里取出钱币然后统计各种货币的总额。这个场景中的特征很明显:就是货币种类了,我们把抽出的货币按币种、金额合成消息发给ConsistentHashingRouter。例子里的Routee应该是按照币种由Router自动构建的,维护各种货币当前总额作为内部状态。向ConsistentHashingRouter发送的消息被分配给相应币种的Routee去登记更新货币当前总额。这个统计金额的Routee可以如下定义:
import akka.actor._ val currencies = List("RMB","USD","EUR","JPY","GBP","DEM","HKD","FRF","CHF") object MoneyCounter {
sealed trait Counting
case class OneHand(cur: String, amt: Double) extends Counting
case class ReportTotal(cur: String) extends Counting
}
class MoneyCounter extends Actor with ActorLogging {
import MoneyCounter._
var currency: String = "RMB"
var amount: Double = override def receive: Receive = {
case OneHand(cur,amt) =>
currency = cur
amount += amt
log.info(s"${self.path.name} received one hand of $amt$cur")
case ReportTotal(_) =>
log.info(s"${self.path.name} has a total of $amount$currency")
}
}
MoneyCounter支持两项功能:一是统计某种货币收到的总额,二是按指令汇报当前总额。我们在前一篇讨论里了解到如果MoneyCounter是Routee类型,那它们应该被视为具相同功能的Actor。而且用户无法分辨或者直接面对某个特定的Routee。任何MoneyCounter都可以收到一手任何货币,不同的货币金额相加结果是错误的。所以我们要用Akka提供的ConsistentHashingRouter来解决这个问题。ConsistentHashingRouter的主要特点是能够分辨消息类型,然后按照消息类型对应到选定的Routee。在我们上面的例子里每个Routee负责一种货币,这样就可以保证每个Routee里的金额总数都是正确的了。ConsistentHashingRouter有三种分辨消息的方法:
1、定义ConsistentHashingRouter的hashMapping函数:这是个PartialFunction[Any,Any],如下:
object HashingRouter extends App {
import MoneyCounter._ val currencies = List("RMB","USD","EUR","JPY","GBP","DEM","HKD","FRF","CHF") val routerSystem = ActorSystem("routerSystem") def mcHashMapping: PartialFunction[Any,Any] = {
case OneHand(cur,_) => cur
case ReportTotal(cur) => cur
} val router = routerSystem.actorOf(ConsistentHashingPool(
nrOfInstances = ,hashMapping = mcHashMapping,virtualNodesFactor = )
.props(MoneyCounter.props),name = "moneyCounter" ) router ! OneHand("RMB",10.00)
router ! OneHand("USD",10.00)
router ! OneHand("HKD",10.00)
router ! OneHand("RMB",10.00)
router ! OneHand("CHF",10.00) router ! ReportTotal("RMB")
router ! ReportTotal("USD") scala.io.StdIn.readLine() routerSystem.terminate()
}
我们在定义router时直接把mcHashingMapping传到ConsistentHashingPool的构建器里就行了。特别要注意nrOfInstances,这个参数必须比消息类型的数量大才行,否则Router会错误引导消息。测试运算结果显示如下:
INFO] [// ::09.334] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e received one hand of 10.0RMB
[INFO] [// ::09.334] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$b] $b received one hand of 10.0USD
[INFO] [// ::09.334] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$d] $d received one hand of 10.0CHF
[INFO] [// ::09.334] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$a] $a received one hand of 10.0HKD
[INFO] [// ::09.334] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e received one hand of 10.0RMB
[INFO] [// ::09.337] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$b] $b has a total of 10.0USD
[INFO] [// ::09.337] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e has a total of 20.0RMB
Router自动调用了e,b,d,a4个Routees,并且能把消息引导到正确的Routee。
2、可以让消息继承ConsistentHashable,如此我们要在消息里实现函数constentHashKey, 如下:
object MoneyCounter {
sealed class Counting(cur: String) extends ConsistentHashable {
override def consistentHashKey: Any = cur
}
case class OneHand(cur: String, amt: Double) extends Counting(cur)
case class ReportTotal(cur: String) extends Counting(cur)
def props = Props(new MoneyCounter)
}
现在消息都是ConsistentHashable类型的了。构建新的Router来测试效果:
val router = routerSystem.actorOf(ConsistentHashingPool(
nrOfInstances = , virtualNodesFactor = ).props(
MoneyCounter.props),name = "moneyCounter") router ! OneHand("RMB",10.00)
router ! OneHand("USD",10.00)
router ! OneHand("HKD",10.00)
router ! OneHand("RMB",10.00)
router ! OneHand("CHF",10.00) router ! ReportTotal("RMB")
router ! ReportTotal("USD")
运算结果同样正确:
[INFO] [// ::29.746] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e received one hand of 10.0RMB
[INFO] [// ::29.746] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$b] $b received one hand of 10.0USD
[INFO] [// ::29.746] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$a] $a received one hand of 10.0HKD
[INFO] [// ::29.746] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$d] $d received one hand of 10.0CHF
[INFO] [// ::29.746] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e received one hand of 10.0RMB
[INFO] [// ::29.749] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$e] $e has a total of 20.0RMB
[INFO] [// ::29.749] [routerSystem-akka.actor.default-dispatcher-] [akka://routerSystem/user/moneyCounter/$b] $b has a total of 10.0USD
3、直接把消息包在ConsistentHashableEnvelope里:
router ! ConsistentHashableEnvelope(message = OneHand("RMB",23.00),hashKey = "RMB")
这种方式需要用户手工指定Routee,如果用这种方式,我们其实不必用Router,直接把消息传给专职的Actor就行了。
看来还是第二种方法比较合适。因为比起第一种方法多了类型安全和与Router的松散耦合。下面就是一个用第二种方法的完整示范源代码:
import akka.actor._
import akka.routing.ConsistentHashingRouter.{ConsistentHashMapping, ConsistentHashable, ConsistentHashableEnvelope}
import akka.routing._ object MoneyCounter {
sealed class Counting(cur: String) extends ConsistentHashable {
override def consistentHashKey: Any = cur
}
case class OneHand(cur: String, amt: Double) extends Counting(cur)
case class ReportTotal(cur: String) extends Counting(cur)
def props = Props(new MoneyCounter)
}
class MoneyCounter extends Actor with ActorLogging {
import MoneyCounter._
var currency: String = "RMB"
var amount: Double = override def receive: Receive = {
case OneHand(cur,amt) =>
currency = cur
amount += amt
log.info(s"${self.path.name} received one hand of $amt$cur")
case ReportTotal(_) =>
log.info(s"${self.path.name} has a total of $amount$currency")
}
}
object HashingRouter extends App {
import MoneyCounter._
import scala.util.Random val currencies = List("RMB","USD","EUR","JPY","GBP","DEM","HKD","FRF","CHF") val routerSystem = ActorSystem("routerSystem") val router = routerSystem.actorOf(ConsistentHashingPool(
nrOfInstances = currencies.size+, virtualNodesFactor = ).props(
MoneyCounter.props),name = "moneyCounter") ( to ).toList foreach (_ => router ! OneHand(
currencies(Random.nextInt(currencies.size-))
,Random.nextInt() * 1.00)) currencies foreach (c => router ! ReportTotal(c)) scala.io.StdIn.readLine() routerSystem.terminate()
}
Akka(5): ConsistentHashing Router - 可选定Routee的任务分配模式的更多相关文章
- (转)akka Router实例
通常在分布式任务调度系统中会有这样的需求:一组actor提供相同的服务,我们在调用任务的时候只需要选择其中一个actor进行处理即可. 其实这就是一个负载均衡或者说路由策略,akka作为一个高性能支持 ...
- AKKA Router路由
路由概念 大量的actor在并行工作的时候,处理到来的消息流,这时候就需要一个组件或者东西来引导消息从源到目的地Actor,这个组件或者东西就是Router在Akka中,router也是一种actor ...
- AKKA集群中的分布式发布订阅
集群中的分布式发布订阅 如何向一个不知道在哪个节点上运行的actor发送消息呢? 如何向集群中的所有actor发送感兴趣的主题的消息? 这种模式提供了一个中介actor,akka.cluster.pu ...
- akka框架——异步非阻塞高并发处理框架
akka actor, akka cluster akka是一系列框架,包括akka-actor, akka-remote, akka-cluster, akka-stream等,分别具有高并发处理模 ...
- Akka源码分析-Router
akak中还有一个比较重要的概念,那就是Router(路由).路由的概念,相信大家都不陌生,在akka中,它就是其他actors的一个代理,会把消息按照路由规则,分发给指定的actor.我一般喜欢把R ...
- Akka(4): Routers - 智能任务分配
Actor模式最大的优点就是每个Actor都是一个独立的任务运算器.这种模式让我们很方便地把一项大型的任务分割成若干细小任务然后分配给不同的Actor去完成.优点是在设计时可以专注实现每个Actor的 ...
- Akka(11): 分布式运算:集群-均衡负载
在上篇讨论里我们主要介绍了Akka-Cluster的基本原理.同时我们也确认了几个使用Akka-Cluster的重点:首先,Akka-Cluster集群构建与Actor编程没有直接的关联.集群构建是A ...
- Akka(13): 分布式运算:Cluster-Sharding-运算的集群分片
通过上篇关于Cluster-Singleton的介绍,我们了解了Akka为分布式程序提供的编程支持:基于消息驱动的运算模式特别适合分布式程序编程,我们不需要特别的努力,只需要按照普通的Actor编程方 ...
- Akka(22): Stream:实时操控:动态管道连接-MergeHub,BroadcastHub and PartitionHub
在现实中我们会经常遇到这样的场景:有一个固定的数据源Source,我们希望按照程序运行状态来接驳任意数量的下游接收方subscriber.又或者我需要在程序运行时(runtime)把多个数据流向某个固 ...
随机推荐
- android studio 2.3 下载地址
android studio下载: Windows+SDK:(1.8GB)| Windows(428 MB) | Linux idea win.exe win.zip 序号 名称 中文 ...
- Machine Learning——Supervised Learning(机器学习之监督学习)
监督学习是指:利用一组已知类别的样本调整分类器的参数,使其达到所要求性能的过程. 我们来看一个例子:预测房价(注:本文例子取自业界大牛吴恩达老师的机器学习课程) 如下图所示:横轴表示房子的面积,单位是 ...
- Yii2框架---GII自动生成
本地环境配置完成后.访问路径直接加上/gii 例如 localhost/gii 即可生成YII活动记录类 即可生成模块
- 蓝桥杯-第39级台阶-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- Bash 的若干基本问题
Bash 的若干基本问题 这里介绍一些bash启动前.后的问题,以及一些使用bash需要注意的基本问题. 1.Bash的介绍 Bash是一种Shell程序,它是一般的Linux系统中的 ...
- innobackup全备与恢复
前提:xtrabackup.mysql安装完成,建立测试库reading.测试表test,并插入三条数据. 1.全备: innobackupex --user=root --password ...
- 关于cisco ccp 或sdm管理gns3中思科路由器的成功分享
本来工作环境中有一台c1841,闲来无事,升级了最新的IOS=c1841-adventerprisek9-mz.151-4.M6.bin,在xp虚拟机中安装sdm(新windows系统不支持)和在wi ...
- 学习MVC之租房网站(八)- 前台注册和登录
在上一篇<学习MVC之租房网站(七)-房源管理和配图上传>完成了在后台新增.编辑房源信息以及上传房源配图的功能.到此后台开发便告一段落了,开始实现前台的功能,也是从用户的登录.注册开始. ...
- [笔记]scanf的使用(主要是针对char)
学的是C++,用cin cout也用的很顺溜,写自己的类时重载"<<"与">>"运算符也很爽,但是发现在刷算法竞赛题时,cin cout ...
- js算法集合(一) 水仙花数 及拓展(自幂数的判断)
js算法集合(一) ★ 最近有些朋友跟我说对js中的一些算法感到很迷惑,知道这个算法到底是怎么回事,但是就是不会用代码把它写出来,这里我跟大家分享一下做水仙花数的算法的思路,并对其扩展到自幂数的算法, ...