Akka Cluster in use (Part 3): Setup a Local Akka Cluster

Reading Time: 4 minutes

Hello friends, I hope you all are safe in COVID-19 pandemic and learning new tools and tech while staying at home.

In our last blog post on Akka Cluster, we learnt about the configurations we need in order to form an Akka Cluster. But we didn’t saw it in action. Hence in this blog post, we will see one in action.

Step 1: Download the Sample

To start with forming an Akka Cluster, download the Reactive Banking sample from Techhub.

It is a simple Akka application for managing customer credibility money as part of the Reactive Banking system. It is built using Akka HTTP and Akka Cluster Sharding. It includes a credibility program that allows users to accumulate money each time they add money in their account. Once they have accumulated enough money they can debit that money. This is a skeleton of the Credibility Service with a minimal feature set. It allows us to credit money to an account and debit money from the account. It also ensures that an account balance never goes below zero (You can’t debit money that you don’t have).

Step 2: Run First Node

Now we are ready to run our first node which has to be the first seed node. Since we are going to run many nodes on one machine we have to use different port. Hence for first node we will use port: 2551

$ sbt "run -Dakka.http.server.default-http-port=8000 -Dakka.remote.artery.canonical.port=2551 -Dakka.management.http.port=8558"

Before we execute this command, let’s first understand it:

  • akka.http.server.default-http-port: It defines the default port to bind HTTP server to when no port was explicitly given.
  • akka.remote.artery.canonical.port: The default remote server port clients should connect to. It’s default value is 25520, we can use 0 if we want a random available port. This port needs to be unique for each actor system on the same machine.
  • akka.management.http.port: The port where the HTTP Server for Http Cluster Management will be bound. The value can be from 0 to 65535.

Now when we understand the configurations provided with the sbt command, let’s run our first node of the Akka Cluster.

In the above image, we can see that since there is only one node of the Akka Cluster running, it will join with itself and it will become the leader.

Step 3: Run Second Node

Since a single node Akka Cluster is formed now, let’s add another node to it and see the changes.

sbt "run -Dakka.http.server.default-http-port=8001 -Dakka.remote.artery.canonical.port=2552 -Dakka.management.http.port=8559"

In the above command we can see that we are using different ports for the second node. Let’s run the command and see the changes:

In this image we can see that first node is welcoming the second node to join the cluster. Similarly, first node receives a joining message from the second node and makes the second node as leader:

To confirm the Akka Cluster’s status, we can check it on http://localhost:8558/cluster/members:

Here we can see that the Akka Cluster has 2 nodes present in it.

Step 4: Test the Application

At this point, we have created an Akka Cluster of our application. This creates the foundation for other Akka features such as Akka Cluster Sharding. To test whether the application is running fine or not try the following experiment:

$ ./credibility.sh -p 8000 -a sample credit 100
$ ./credibility.sh -p 8001 -a sample credit 200
$ ./credibility.sh -p 8000 -a sample debit 100
$ ./credibility.sh -p 8001 -a sample debit 200
$ ./credibility.sh -p 8000 -a sample retrieve
$ ./credibility.sh -p 8001 -a sample retrieve

It should result into following:

We can see that the final balance in the account is 0. And it is confirmed via both the nodes. It means that the application is using Akka Cluster to handle account balance.

So, now we have seen an Akka Cluster in action. In our future posts we will get to know, how to manage an Akka Cluster and remove node(s) from the cluster. So stay tuned 🙂

References

Written by 

Himanshu Gupta is a software architect having more than 9 years of experience. He is always keen to learn new technologies. He not only likes programming languages but Data Analytics too. He has sound knowledge of "Machine Learning" and "Pattern Recognition". He believes that best result comes when everyone works as a team. He likes listening to Coding ,music, watch movies, and read science fiction books in his free time.

Discover more from Knoldus Blogs

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

Continue reading