Akka.net: Access to remote actors in a cluster

In a clustered environment, I have the seed node and node1 and node2.

From node1, I want to send a message to the Actor that was created on node2. The local path to this node on node2 is akka: MyAkkaSystem / user / AnActor.

Now I want to send a message from an actor from node1 to this particular actor using ActorSelection as follows:

var actorSystem = ActorSystem.Create("MyTestSystem"); var c = actorSystem.ActorSelection("/user/ConsoleReceiver"); c.Tell("Hello World"); 

On node2, the actor is created in this way:

 var actorSystem = ActorSystem.Create("MyTestSystem"); var r = actorSystem.ActorOf(Props.Create<MessageReceiver>(), "ConsoleReceiver"); Console.WriteLine(r.Path); Console.ReadLine(); actorSystem.Terminate().Wait(); 

Unfortunately, this will not work, as the attempt ends in dead letters.

The HOCON configuration on node2 is as follows:

 akka { actor { provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster" deployment { } } remote { log-remote-lifecycle-events = DEBUG log-received-messages = on helios.tcp { transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote" applied-adapters = [] transport-protocol = tcp hostname = "127.0.0.1" port = 0 } } cluster { #will inject this node as a self-seed node at run-time seed-nodes = ["akka.tcp:// webcrawler@127.0.0.1 :4053"] #manually populate other seed nodes here, ie "akka.tcp:// lighthouse@127.0.0.1 :4053", "akka.tcp:// lighthouse@127.0.0.1 :4044" roles = [crawler] } } 

Like a seed node I use a beacon. In terms of connectivity, everything seems to work. Seed is found, and each node received receives a welcome message.

I thought that I have location transparency in the cluster and I can get the remote resources as if they were local.

+5
source share
1 answer

I thought that I have location transparency in the cluster and can reach remote resources as if they were local.

It's not so easy. Consider the following scenario: What if you created an actor on both nodes in the same way. If you try to use a relative path - without showing which node you mean - which participant should receive the message ?.

Using the main features of the cluster, you can easily select a node using Context.ActorSelection(_cluster.ReadView.Members.Single(m => /* which node you want to choose */).Address + "/user/ConsoleReceiver"); . The cluster extension gives you read data with information about all the elements visible from the current node.

There are many ways to send a message to another player, not knowing what node he lives on.

The first approach is to use Akka.Cluster.Tools cluster singleton - it allows you to create no more than one instance of the actor present in the cluster. In case of node failures, it will move to another node. Keep in mind that this solution should not be used if you want many participants to work this way. This is more for individual, special players.

The second approach is to use the Akka.Cluster.Tools Distributed Pub / Sub function to broadcast group events between cluster members subscribed to a specific topic without worrying about their actual location. This is a good choice for broadcast scenarios.

The last approach is to use the Akka.Cluster.Sharding function , which automatically controls the life cycle of actors - you do not need to explicitly create participants - it is also able to send messages to them from anywhere in the cluster and, if necessary, rebalance them across many nodes of the cluster.

+5
source

Source: https://habr.com/ru/post/1243875/


All Articles