实际上是在(一)的基础之上加入样例类和心跳等内容
1、Worker启动后,在preStart方法中与Master建立连接,向Master发送注册,将Worker的信息通过case class封装起来发送给Master
2、Master接受到Worker的注册消息后将Worker的信息保存起来,然后向Worker反馈注册成功
3、Worker定期向Master发送心跳,为了报活
4、Master会定时清理超时的Worker
附上代码实现:
Master类:
class Master(val host: String, val port :Int) extends Actor{
//workerId -> WorkerInfo
val idToWorker = new mutable.HashMap[String, WorkerInfo]()
//WorkInfo
val workers = new mutable.HashSet[WorkerInfo]()
//检测超时时间间隔
val CHECK_INTERVAL = 15000
override def preStart(): Unit = {
println("preStart invoked")
//导入隐式转换
import context.dispatcher
context.system.scheduler.schedule(0 millis, CHECK_INTERVAL millis, self, CheckTimeOutWorker)
}
//用于接收消息
override def receive: Receive = {
case RegisterWorker(id, memory, cores) => {
//判断是否已经注册
if (!idToWorker.contains(id)) {
//把Worker的信息封装起来保存到内存中
val workerInfo = new WorkerInfo(id, memory, cores)
idToWorker(id) = workerInfo
workers += workerInfo
sender ! RegisteredWorker(s"akka.tcp://MasterSystem@$host:$port/user/Master")
}
}
case HeartBeat(id) => {
if (idToWorker.contains(id)) {
val workerInfo = idToWorker(id)
//报活
val currentTime = System.currentTimeMillis()
workerInfo.lastHeartbeatTime = currentTime
}
}
case CheckTimeOutWorker => {
val currentTime = System.currentTimeMillis()
val toRemove = workers.filter(x => currentTime - x.lastHeartbeatTime > CHECK_INTERVAL)
for (w <- toRemove) {
workers -= w
idToWorker -= w.id
}
println(workers.size)
}
}
}
object Master {
def main(args: Array[String]): Unit = {
val host = args(0)
val port = args(1).toInt
//准备配置
val configStr =
s"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.remote.netty.tcp.hostname = "$host"
|akka.remote.netty.tcp.port = "$port"
""".stripMargin
val config = ConfigFactory.parseString(configStr)
//ActorSystem老大,辅助创建和监控下面的Actor,他是单例的
val actorSystem = ActorSystem("MasterSystem", config)
//创建Actor
//方式一:val master = actorSystem.actorOf(Props[Master], "Master")
val master = actorSystem.actorOf(Props(new Master(host, port)), "Master")
master ! "hello"
actorSystem.awaitTermination()
}
}
Worker类:
package cn.itcast.rpc
import java.util.UUID
import akka.actor.{Actor, ActorSelection, ActorSystem, Props}
import com.typesafe.config.ConfigFactory
import scala.concurrent.duration._
class Worker(val masterHost: String, val masterPort: Int, val memory: Int, val cores: Int) extends Actor{
var master: ActorSelection = _
val workerId = UUID.randomUUID().toString
val HEART_INTERVAL = 10000
override def preStart(): Unit = {
//跟Master建立连接
master = context.actorSelection(s"akka.tcp://MasterSystem@$masterHost:$masterPort/user/Master")
//向Master发送注册信息
master ! RegisterWorker(workerId, memory, cores)
}
override def receive: Receive = {
case RegisteredWorker(masterUrl) => {
println(masterUrl)
//定时器发送心跳
import context.dispatcher
context.system.scheduler.schedule(0 millis, HEART_INTERVAL millis, self, SendHeartbeat)
}
case SendHeartbeat => {
println("send heartbeat to master")
master ! HeartBeat(workerId)
}
}
}
object Worker {
def main(args: Array[String]): Unit = {
val host = args(0)
val port = args(1).toInt
val masterHost = args(2)
val masterPort = args(3).toInt
val memory = args(4).toInt
val cores = args(5).toInt
//准备配置
val configStr =
s"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.remote.netty.tcp.hostname = "$host"
|akka.remote.netty.tcp.port = "$port"
""".stripMargin
val config = ConfigFactory.parseString(configStr)
val actorSystem = ActorSystem("WorkerSystem", config)
//创建Actor
actorSystem.actorOf(Props(new Worker(masterHost, masterPort, memory, cores)), "Worker")
actorSystem.awaitTermination()
}
}
RemoteMessage接口:
trait RemoteMessage extends Serializable
//Worker -> Master
case class RegisterWorker(id: String, memory: Int, cores: Int) extends RemoteMessage
//Master -> Worker
case class RegisteredWorker(masterUrl: String) extends RemoteMessage
//worker -> self
case object SendHeartbeat
//Worker -> Master
case class HeartBeat(id: String) extends RemoteMessage
//Master -> self
case object CheckTimeOutWorker
WorkInfo实体:
class WorkerInfo(val id: String, val memory: Int, val cores: Int) {
var lastHeartbeatTime: Long = _
}