Service registration, discovery, heartbeat

Time:2020-8-6

Using akka to achieve service registration, discovery and heartbeat

Message class:

package  org.examplecase  class RegisterWorkerInfo(id: String, cpu: Int, ram: Int);case class WorkerInfo(val id: String, val cpu: Int, val ram: Int) {  var lastheartbeat: Long =  System.currentTimeMillis ();};case object RegisteredWorkerInfo;case object SendHeartBeat;case object StartTimeOutWorker;case object RemoveTimeOutWorker; Case class heartbeanpackage (ID: String); server master:
package  org.exampleimport  java.util.concurrent .TimeUnitimport  akka.actor .{Actor, ActorSystem, Props}import  com.typesafe.config .ConfigFactoryimport  scala.collection.mutableimport  scala.concurrent.duration .FiniteDurationclass MasterActor extends Actor {  val register =  mutable.Map [String, WorkerInfo]();   override def receive: Receive = {    case "start" => {      println("master start !!!");      self ! StartTimeOutWorker;    }    case RegisterWorkerInfo(id, cpu, ram) => {      if (! register.contains (id)) {        val workerInfo = WorkerInfo(id, cpu, ram);        register(id) = workerInfo;      };      println("workers: " + register)      sender()  ! RegisteredWorkerInfo;    }    case HeartBeanPackage(id) => {      val workerInfo: WorkerInfo = register(id);       workerInfo.lastheartbeat  = System.currentTimeMillis (); println ("master updated" + ID + "heartbeat time");} case starttimeoutworker = > {import context.dispatcher ;       context.system.scheduler .schedule(FiniteDuration(0,  TimeUnit.SECONDS ), FiniteDuration(9,  TimeUnit.SECONDS ),        self, RemoveTimeOutWorker);    }    case RemoveTimeOutWorker =>{       register.filter (x =>   System.currentTimeMillis () -  x._ 2.lastheartbeat > 6000)        .foreach(x =>  register.remove (x._ 1) ); println ("number of online workers:+ register.size  );     }  }}object MasterActor {  def main(args: Array[String]): Unit = {    val config =  ConfigFactory.parseString (      s"""         | akka.actor.provider= " akka.remote.RemoteActorRefProvider "         | akka.remote.netty . tcp.hostname=localhost          A kind of akka.remote.netty . tcp.port=8080         """.stripMargin);    val masterSysterm = ActorSystem("master_ systerm", config);    val masterActor1 =  masterSysterm.actorOf (Props[MasterActor], "master_ Actor1 ") masteractor 1!" start ";} client Slave:
package  org.exampleimport  java.util.UUIDimport  java.util.concurrent .TimeUnitimport  akka.actor .{Actor, ActorSelection, ActorSystem, Props}import  com.typesafe.config .ConfigFactoryimport  scala.concurrent.duration .{FiniteDuration, TimeUnit}class SlaveActor extends Actor {  var masterProxy: ActorSelection = _ ;  val id: String =  UUID.randomUUID ().toString;  override def preStart(): Unit = {    val masterUrl = " akka.tcp ://master_ [email protected] :8080/user/master_ actor1";     this.masterProxy  = context.actorSelection (masterurl);} override def receive: receive = {case "start" = > {println ("client running"); this.masterProxy  ! registerworkerinfo (ID, 16, 16 * 1024);} case registeredworkerinfo = > {println ("workerid:" + ID + "registered successfully"); import context.dispatcher ;        context.system.scheduler .schedule(FiniteDuration(0,  TimeUnit.SECONDS ), FiniteDuration(3,  TimeUnit.SECONDS ), self, sendheartbeat);} case sendheartbeat = > {println ("workerid:" + ID + "send heartbeat to server"); masterproxy! Heartbeanpackage (ID);}}} object slaveactor {def main (args: array [string]): unit = {Val config = ConfigFactory.parseString (      s"""         | akka.actor.provider= " akka.remote.RemoteActorRefProvider "         | akka.remote.netty . tcp.hostname=localhost          A kind of akka.remote.netty . tcp.port=8090         """.stripMargin);    val slaverSysterm = ActorSystem("slaver_ systerm", config);    val slaverActor1 =  slaverSysterm.actorOf (Props[SlaveActor], "slaver_ actor1")    slaverActor1 ! "start";  }}