0

I want to know how can we send a message to a particular actor in cluster without knowing the actual path..

For Example...
I have 5 nodes all have same actorsytem and formed a cluster.
Each node will have 2 actors in it all actors across cluster have unique name.
I have an actor system outside the cluster.which not part of this cluster.
I have to send message from this actor system to cluster actors.
How can i map respective message to respective actor in cluster each time.Without knowing the actual path of the actor.
Without cluster sharding how actors inside cluster will accessed by outside cluster.

Arun
  • 67
  • 11

2 Answers2

1

There is a direct answer to your question (at the end), but I think you need to go back to the docs and rethink your design. One of the primary reasons for this is that while what you are asking is possible, it goes against all of the best practices and many of the features involved are deprecated or not recommended. The best practice is that clusters should be self-contained and clusters should expose via well defined APIs and not actor internals. To quote from the remoting docs:

When building an Akka application, you would usually not use the Remoting concepts directly, but instead use the more high-level Akka Cluster utilities or technology-agnostic protocols such as HTTP, gRPC etc.

Essentially the docs are tell you that what you are trying to do is a bad idea. If two actors need to be able to discover each other, they should be in the same cluster. If for some reason you can't have them in the same cluster, for maintainability reasons you should expose the actor in the cluster via REST/gRPC or some other well defined API rather than trying to allow direct access to the actors.

Similarly, in the section about cluster client the docs say:

Cluster Client is deprecated in favor of using Akka gRPC. It is not advised to build new applications with Cluster Client, and existing users should migrate.

But, here's an attempt to answer the question directly. To quote the first sentence of the Actor discovery docs, "There are two general ways to obtain Actor references: by creating actors and by discovery using the Receptionist."

Unrelated side note: (That is a little misleading though, because there are some other ways in untyped Actors, and you also can obviously just be given a ActorRef. It's not the the docs are wrong, I'm just taking them a little out of context.).

Anyway, that leads to the direct answer to your question: ClientClusterReceptionist. You can find details on how to use it in the Classic Cluster Client docs. It's going to require some untyped actors, but since you are using deprecated approaches that probably isn't an issue.

But, moreso, you really should rethink this because based on your last question, it seems like you really aren't understanding how either clustering or sharding work.

David Ogren
  • 4,396
  • 1
  • 19
  • 29
  • The original question is related to Akka.NET and not the JVM/Scala Akka, there is no gRPC in Akka.NET, the only way to do what being asked using out of the box solution is by using `ClusterClient` and `ClusterClientReceptionist` – Greg May 18 '22 at 06:01
  • Fair enough (although couldn't they just expose a REST API instead of gRPC?). Typically I give an attempt at answering a question, even if it's tagged akka.net because something is better than nothing and it seems that most answers are portable. – David Ogren May 18 '22 at 12:24
  • By they you mean the Akka.NET developers? Not really, the core Akka.NET does not leverage any HTTP technology whatsoever. We are currently adding adapters for Microsoft hosting and dependency injection package, integrating Akka.NET with ASP.NET will be a lot easier in the near future. – Greg May 18 '22 at 13:10
  • Can you share any sources for Akka HTTP,gRPC for Akka cluster sharding? – Arun Jul 08 '22 at 06:49
1

As @david-ogren said, what you need is cluster client. You can read the documentation here. With cluster client, you still need to know at least one of the cluster node address and either:

  1. The name of the actor you're trying to communicate with, if you're trying to communicate with the actor instance directly, or
  2. A predefined topic to publish to. If you went for this route, you will need to make sure that all of the participating actors inside the cluster subscribes to the topic to receive them.

You can see a working example in this GitHub repository.

Greg
  • 119
  • 1
  • 7
  • 1
    Can we create actors like sending messages with Cluster client?Any other way to create Actors dynamically and pass messages to them without sharding? – Arun May 18 '22 at 05:27
  • 1
    If we using sharding how messages send from Cluster client to Actual actor? – Arun May 18 '22 at 05:28
  • 2
    Actor creation is arbitrary. For example, you can always wrap the payload inside an envelope that includes a key. The cluster actor can act as a dictionary router that reads the envelope and then routes the payload inside the envelope. The router can also create the actors if they did not yet exists. – Greg May 18 '22 at 05:56
  • 1
    You would not use `ClusterClient` if you're using cluster sharding. For cluster sharding, you will need `ShardRegion` if you're addressing actors from inside the cluster or `ShardRegion` in proxy mode if you're addressing the actors from outside the cluster. – Greg May 18 '22 at 06:08
  • Then when the Cluster Client will be used? Only with DistributedPubSubMediator? – Arun May 18 '22 at 07:22
  • `ClusterClient` is used for the scenario you described in your original question, when you need to access a subset of actors inside an Akka.NET cluster from an ActorSystem outside of the cluster dynamically without having to know the exact knowledge of the running cluster topology. – Greg May 18 '22 at 12:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244842/discussion-between-greg-and-arun). – Greg May 18 '22 at 13:35
  • Can you create a chat again? – Arun Jun 02 '22 at 05:15