akka-cluster-remote例子

单机上模拟两个 actor 通信的例子

maven 依赖

     
            com.typesafe.akka
            akka-actor_2.12
            2.6.16
        

        
            com.typesafe.akka
            akka-cluster_2.12
            2.6.16
        
        
            com.typesafe.akka
            akka-actor_2.12
            2.6.16
        

        
            com.typesafe.akka
            akka-cluster_2.12
            2.6.16
        

Client Actor代码

package akka.remote

import akka.actor.{Actor, ActorSystem, Props}
import com.typesafe.config.ConfigFactory

object ClientActor {

  class LocalActor extends Actor{
    override def receive: Receive = {
      case msg:String => {
        println("got message from remote reply: " + msg)
      }
    }

    override def preStart(): Unit = {

      val remoteActor = context.actorSelection("akka://[email protected]:2552/user/server")
      println("That 's remote:" + remoteActor)
      remoteActor ! "hi"

    }

  }



  val clientConfig="""akka {
                     |  actor {
                     |    provider = "cluster"
                     |  }
                     |    remote {
                     |      artery {
                     |        enabled = on
                     |        transport = tcp
                     |        canonical.hostname = "127.0.0.1"
                     |        canonical.port = 2551
                     |      }
                     |    }
                     |}""".stripMargin


  def main(args: Array[String]): Unit = {

    // 第一台机器上的配置
    val config1 = ConfigFactory.parseString(clientConfig)

    // 创建第一台机器上的Actor系统
    val system1 = ActorSystem("ActorCommunicationSystem", config1)

    // 在第一台机器上创建Greeter Actor
    val localActor = system1.actorOf(Props[LocalActor], "local")













  }



}

Server Actor代码

package akka.remote

import akka.actor.{Actor, ActorSystem, Props}
import com.typesafe.config.ConfigFactory

class RemoteActor extends Actor{
  override def receive: Receive = {
    case msg: String=>{
      println("remote received " + msg + " from " + sender)
      sender ! "hi"
    }
    case _=>{
      println("Received unknown msg ")
    }
  }
}

object ServerActor extends App {





  val serverConfig="""
                    akka {
                       actor {
                         provider = "cluster"
                       }
                      remote {
                        artery {
                          enabled = on
                          transport = tcp
                          canonical.hostname = "127.0.0.1"
                          canonical.port = 2552
                        }
                      }
                     }
                     """.stripMargin


    // 第二台机器上的配置
    val config2 = ConfigFactory.parseString(serverConfig)
    // 创建第二台机器上的Actor系统
    val system2 = ActorSystem("ActorCommunicationSystem", config2)
    // 在第二台机器上创建GreetingPrinter Actor
    val greetingPrinter2 = system2.actorOf(Props[RemoteActor], "server")
    println("ServerActor is ready")





}

运行结果

1,先拉起 Sever Actor,相当于监听对应的端口号,输出如下:

[INFO] [07/17/2023 19:21:13.537] [main] [ArteryTransport(akka://ActorCommunicationSystem)] Remoting started with transport [Artery tcp]; listening on address [akka://[email protected]:2552] with UID [-6133327117776502163]
[INFO] [07/17/2023 19:21:13.593] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Starting up, Akka version [2.6.16] ...
[INFO] [07/17/2023 19:21:13.741] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Registered cluster JMX MBean [akka:type=Cluster]
[INFO] [07/17/2023 19:21:13.741] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Started up successfully
[INFO] [07/17/2023 19:21:13.808] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - No downing-provider-class configured, manual cluster downing required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#downing
[INFO] [07/17/2023 19:21:13.809] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - No seed-nodes configured, manual cluster join required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#joining
ServerActor is ready

2,再拉起 Client Actor,发送消息,并收到回复

[INFO] [07/17/2023 19:22:02.615] [main] [ArteryTransport(akka://ActorCommunicationSystem)] Remoting started with transport [Artery tcp]; listening on address [akka://[email protected]:2551] with UID [6920370786141263359]
[INFO] [07/17/2023 19:22:02.664] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2551] - Starting up, Akka version [2.6.16] ...
[INFO] [07/17/2023 19:22:02.814] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2551] - Registered cluster JMX MBean [akka:type=Cluster]
[INFO] [07/17/2023 19:22:02.814] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2551] - Started up successfully
[INFO] [07/17/2023 19:22:02.875] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2551] - No downing-provider-class configured, manual cluster downing required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#downing
[INFO] [07/17/2023 19:22:02.875] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2551] - No seed-nodes configured, manual cluster join required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#joining
That 's remote:ActorSelection[Anchor(akka://[email protected]:2552/), Path(/user/server)]
got message from remote reply: hi


3,此时再看 Server Actor 的输出:

[INFO] [07/17/2023 19:21:13.537] [main] [ArteryTransport(akka://ActorCommunicationSystem)] Remoting started with transport [Artery tcp]; listening on address [akka://[email protected]:2552] with UID [-6133327117776502163]
[INFO] [07/17/2023 19:21:13.593] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Starting up, Akka version [2.6.16] ...
[INFO] [07/17/2023 19:21:13.741] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Registered cluster JMX MBean [akka:type=Cluster]
[INFO] [07/17/2023 19:21:13.741] [main] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - Started up successfully
[INFO] [07/17/2023 19:21:13.808] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - No downing-provider-class configured, manual cluster downing required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#downing
[INFO] [07/17/2023 19:21:13.809] [ActorCommunicationSystem-akka.actor.internal-dispatcher-3] [Cluster(akka://ActorCommunicationSystem)] Cluster Node [akka://[email protected]:2552] - No seed-nodes configured, manual cluster join required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#joining
ServerActor is ready
remote received hi from Actor[akka://[email protected]:2551/user/local#285260490]

你可能感兴趣的:(java,大数据,linux)