Cluster configuration
Akka { actor { Provider = "Akka.cluster.ClusterActorRefProvider" } Remote { Log-remote-lifecycle-events = off enabled-transports = ["Akka.remote.netty.tcp"] netty.tcp { hostname = " 127.0.0.1 " port = 0 } } cluster { seed-nodes = [ " Akka.tcp://[email protected]:2551 ", "Akka.tcp://[email protected]:2552"] Auto-down-unreachable-after = 10s } Persistence { Journal.plugin = " Akka.persistence.journal.leveldb-shared " Journal.leveldb-shared.store {# does not use ' native = Off ' in PRODUCTION!!! Native = off dir = "target/shared-journal" } snapshot-store.local.dir = "Target/snapshots" } log-dead-letters = off}
Actor.provider Settings Select Clusteractorrefprovider, in the IDE the String can jump to Clusteractorrefprovider, from the program's comments, Actorref provider This is not to say how the actor is provided, it is to introduce cluster extension, and automatically start cluster
i.e. the cluster would automatically be started if the ' Clusteractorrefprovider ' is used.
Create three Actorsystem composed of cluster
def main (args:array[string]): Unit = { if (args.isempty) startup (Seq ("2551", "2552", "0")) else Startup (args) } def startup (Ports:seq[string]): Unit = { ports foreach {port = //Override the CONFI Guration of the port val config = configfactory.parsestring ("akka.remote.netty.tcp.port=" + port). Withfallback (Configfactory.load ()) //Create an Akka system val system = Actorsystem ("Clustersystem", config ) System.actorof (Clustersingletonmanager.props (Master.props (Duration (), "second"), "active", Poisonpill, None), "Master") } }
Config when creating Actorsystem overrides Akka.remote.netty.tcp.port, because the default configuration is only port = 0 this option. Since three cluster are started natively, hostname does not require additional declarations, reusing 127.0.0.1 in application.conf
In addition, the name of Actorsystem must be unified, is Clustersystem, which is declared in the seed-nodes in application.conf, it is the ID of cluster.
I guess, when the seed-nodes all hung up, the new actorsystem should not be able to join cluster, because the cluster ID can not find the organization
Object Master { val resultstopic = "Results" def props (worktimeout:finiteduration): props = Props (classof[ Master], Worktimeout) Case Object Job case Object Parentgreetings}class Master (worktimeout:finiteduration) Extends Actor with actorlogging { context.system.scheduler.schedule (Duration (Ten, "second"), Duration (5, "second") , self, job) val timeout:timeout = ten second override def receive:receive = {case Job = + val info = Clusterprotocol.selfinfo (self) log.info (info._1 + ":" + info._2 + ":" + info._3) Context.system.actorSelection ("User/master/active"). Resolveone (4 second). Map (actor + actor!) parentgreetings) Case parentgreetings = log.info ("Greetings from parent") }
Above is the implementation of cluster Singleton actor, it has two things to do, one is to print out their own path, but find themselves. It is important to note that Resolveone cannot await (blocking, etc.), the person will report an exception, actor not found
The above code implements a simple cluster singleton, specifically, when a cluster has multiple actorsystem, when a actorsystem hangs, the master actor continues to serve, and the actor's I Nstance have and only one.
SBT "Run-main packagename.mainname portnum" can start Actorsystem on port Portnum, no port number, will start three actorsystem, but not convenient to simulate a actorsyst Em hangs off the case.
Akka Cluster First Experience