Getting Started with Akka Remoting

Table of contents
Reading Time: 2 minutes

When we start with Akka we generally start with one actor system on our local machine but when we talk about the business application we can have multiple parts of an application and those part can run on different machine or node. Akka Remoting is a communication module to connect the actor system in peer to peer fashion. It also serves as the foundation for Akka Clustering.

So let’s begin with the implementation.

To start working with Akka remoting we need to add Akka Remoting’s dependency.

Now to start with Akka Remoting let’s start by adding configuration generally application.conf file.

The example above only illustrates the bare minimum of properties you have to add to enable remoting.

First, we should change the provider from local to remote. Second, we added tcp on enable-transport list. At last, we hast hostname and port.

Each Actor System requires a unique port if shared on the same host or they can use the same port if on different machines.

Now let’s start creating a worker.

Our worker actor will print a message when receiving any string message.

Now let’s create our Actor System using the configuration that we defined earlier.

Code is similar to any local actor system the only difference is this is a remote actor because of the configuration we provided.

Akka has two ways of using remoting:

  • Lookup
  • Creation

Lookup

Lookup is used to look up an actor on a remote node with actorSelection(path).

Using the above example we can obtain ActorSelection to an actor on a remote node.

ActorPath provides in the above example can be defined as below if we break it down into its different parts. Now we can use actorSelection in the same way as we do with a local remote.

Creation

Creation is used to create an actor on a remote node using actorOf(…)

Now to create actor using Akka remoting we need to add one more configuration in our application.conf to describe the deployment for remote Akka service. For example:

Once you have configured the properties above you would do the following in code:

That’s all for this blog. In our next blog, we will example Akka Clustering.

Resources:

https://doc.akka.io/docs/akka/current/remoting.html



Knoldus-blog-footer-image

Written by 

I am from India and developing Scala Microservices :)

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading