玩2.6 Akka集群

时间:2018-04-03 10:37:46

标签: scala playframework akka akka-cluster

我试图在我的Play应用程序中创建Akka Clusters,使用lightbend和Akka文档中提供的文档,我已经设法创建了一个包含2个节点的集群,运行了10个actor的实例,但是我被卡住了从一天开始,尝试访问节点集群的单个实例。 因此,Trial1.scala为集群创建第一个节点,并继续监听直到某个节点加入。 Trial1使用SystemTest.scala中创建的系统和配置。

Trialagain.scala是第二个节点,有10个自身实例,并与第一个集群连接。

这两部分工作正常。

现在,当我尝试使用此群集中的任何实例时,我认为我只需要使用actorselection来获取已经存在的节点的任何实例,如果我出错了,请纠正我,TrialThird我试图这样做,当我不使用actorselection并使用actorof时,非常确定,它创建了自己的实例并且运行简单,但是当我使用actorselection访问节点实例时,它会抛出跟随错误。     [Cluster1-akka.actor.default-dispatcher-4] [akka:// Cluster1 / deadLetters]没有发送者到Actor [akka:// Cluster1 / deadLetters]的消息[java.lang.String]未送达。 [1]遇到死信。可以使用配置设置关闭或调整此日志记录' akka.log-dead-letters'和' akka.log-dead-letters-during-shutdown'。

Trial.scala

import akka.actor.{Actor, ActorLogging, ActorSystem, Address, Props}
import akka.cluster.Cluster
import akka.cluster.routing.{ClusterRouterPool, ClusterRouterPoolSettings}
import akka.routing.RoundRobinPool
import com.typesafe.config.ConfigFactory

object Trial{
def main(args: Array[String]):Unit= {
 val system= SystemTest.system
   system.actorOf(Props[Trial].withRouter(RoundRobinPool(10)),name="Trial1")
 Cluster(system).join(Address("akka.tcp","Cluster1","127.0.0.1",2222))
}
}

class Trial extends Actor with ActorLogging{
import context.dispatcher
override def receive: Receive={
case msg => log.info("Got message: {}",msg)
}
}

Trialagain.scala

import akka.actor.{Actor, ActorLogging, ActorSystem, Address, Props}
import akka.cluster.Cluster
import akka.cluster.routing.{ClusterRouterPool,  ClusterRouterPoolSettings}
import akka.routing.RoundRobinPool
import com.typesafe.config.ConfigFactory
import scala.concurrent.ExecutionContext.Implicits.global


object Trialagain{
val conf=
  """
  |akka {
  |
  |  log-dead-letters = 1
  |  log-dead-letters-during-shutdown = off
  |  # extensions = ["akka.contrib.pattern.ClusterReceptionistExtension"]
  |
  |  actor {
  |    provider = "akka.cluster.ClusterActorRefProvider"
  |  }
  |  remote {
  |    log-remote-lifecycle-events = off
  |    netty.tcp {
  |      hostname = "127.0.0.1"
  |      port = 2551
  |    }
  |  }
  |}
  |

""".stripMargin
val config = ConfigFactory.parseString(conf)
val system = ActorSystem("Cluster1", config)
system.actorOf(Props[Trialagain].withRouter(RoundRobinPool(10)),name="Trialagain")
Cluster(system).join(Address("akka.tcp","Cluster1","127.0.0.1",2222))
}

}

class Trialagain extends Actor with ActorLogging{
override def receive: Receive={
case msg => log.info("Got message: {}",msg)
}
}

SystemTest.scala

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory


object SystemTest {

val conf=
"""
  |akka {
  |
  |  log-dead-letters = 1
  |  log-dead-letters-during-shutdown = off
  |  # extensions = ["akka.contrib.pattern.ClusterReceptionistExtension"]
  |
  |  actor {
  |    provider = "akka.cluster.ClusterActorRefProvider"
  |  }
  |  remote {
  |    log-remote-lifecycle-events = off
  |    netty.tcp {
  |      hostname = "127.0.0.1"
  |      port = 2222
  |
  |    }
  |  }
  |
  |  cluster {
  |    seed-nodes = [
  |      "akka.tcp://ClusterSystem@127.0.0.1:2551",
  |      "akka.tcp://ClusterSystem@127.0.0.1:2552"]
  |  }
  |}
  |
  |

""".stripMargin
val config = ConfigFactory.parseString(conf)
val system = ActorSystem("Cluster1", config)
sys.addShutdownHook(system.terminate())
}

TrialThird.scala

import akka.actor.{Actor, ActorSystem, Address, Props}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


class TrialThird extends Actor{
override def receive: Receive = ???
}


object TrialThird {

def main(args: Array[String]): Unit = {
val system= ActorSystem("Cluster1")
val router= system.actorSelection("akka.tcp://Cluster1@127.0.0.1:2222/user/Trial1")
Future{
router ! "hello"
}
} 
}

0 个答案:

没有答案