我想创建一个简单的单例集群并从远程节点发送消息

问题描述:

在这里,我创建了一个单身演员。主节点和种子节点是相同的。从一个不同的项目中,我尝试添加到群集中并想发送消息。我能够加入群集,但无法发送消息。我想创建一个简单的单例集群并从远程节点发送消息

我的主人和种子节点:

 package Demo 
    import akka.actor._ 
    import akka.cluster.singleton.{ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings} 
    import com.typesafe.config.ConfigFactory 
    import scala.concurrent.duration._ 
    object MainObject1 extends App{ 
    DemoMain1.start(8888) 

    } 
    object DemoMain1 { 
     val role = "test" 
     val singletonname = "Ruuner" 
     val mastername = "Master" 
     def start(port: Int): ActorSystem = { 
     val conf = ConfigFactory.parseString(
      s""" 
    |akka.actor.provider = "akka.cluster.ClusterActorRefProvider" 
    | 
    |akka.remote.netty.tcp.port = $port 
    |akka.remote.netty.tcp.hostname = 127.0.0.1 
    |akka.cluster.roles = ["$role"] 
    |akka.cluster.seed-nodes = ["akka.tcp://[email protected]:8888"] 

    """.stripMargin 
) 
val system = ActorSystem("DemoMain1", conf) 
val settings = ClusterSingletonManagerSettings(system).withRole(role) 
val manager = ClusterSingletonManager.props(Props[DemoMain1], PoisonPill, settings) 
val actor=system.actorOf(manager, mastername) 
system 
     } 

     class DemoMain1 extends Actor with Identification { 
     import context._ 
     override def preStart(): Unit = { 
      println(s"Master is created with id $id in $system") 
      println(self.path.address.host) 
      system.scheduler.scheduleOnce(100.seconds, self, 'exit) 

     } 

     override def receive : Receive={ 
    case 'exit => println("$id is exiting") 
    context stop self 
    //SupervisorStrategy.Restart 


    case msg => println(s"messasge from $system is $msg ") 

     sender() ! 'how 
     } 
     } 
    } 

这是试图加入集群,并发送消息的另一节点。

 import akka.actor._ 
    import akka.cluster.singleton.{ClusterSingletonProxy, ClusterSingletonProxySettings} 
    import com.typesafe.config.ConfigFactory 
    import scala.concurrent.duration._ 
    object Ping extends App{ 
     def ping: ActorSystem = { 
     val conf = ConfigFactory.parseString(
      s""" 
       |akka.actor.provider = "akka.cluster.ClusterActorRefProvider" 
    | 
    |akka.remote.netty.tcp.port = 0 
    |akka.remote.netty.tcp.hostname = 127.0.0.1 
    |akka.cluster.roles = ["slave"] 
    |akka.cluster.seed-nodes = ["akka.tcp://[email protected]:8888"] 
    |akka.cluster.auto-down-unreachable-after = 10s 
    """.stripMargin 
) 
val system = ActorSystem("DemoMain1", conf) 
system.actorOf(Props[Ping]) 
system 
     } 
     class Ping extends Actor { 
     import context._ 
     val path = "akka.tcp://[email protected]:8888/DemoMain1/user/Master/actor" 
     val settings = ClusterSingletonProxySettings(system).withRole("slave") 
     val actor = context.actorOf(ClusterSingletonProxy.props(path, settings)) 
     val pingInterval = 1.seconds 
     override def preStart(): Unit = { 
      system.scheduler.schedule(pingInterval, pingInterval) { 
      println(s"Locate Ping $system") 
      actor ! 'hi 
      } 
     } 

     override def receive: Receive = { 
      case msg => println(s"The message is $msg") 

     } 
     } 
     Ping.ping 
    } 

如果我给系统的ip地址,那么也不发送消息。

它出现在你的ClusterSingletonProxySettings(system).withRole("slave")设置role为您Ping演员不匹配,你ClusterSingletonManagerSettings(system).withRole(role)其中role = "test"的。

ClusterSingletonProxy被认为是本与在其上集群建立,因此它的设置role应匹配ClusterSingletonManager的指定role所有节点上。这是一个sample configuration

+0

我试着保持同样的名字,但它不工作 –