Cluster

The Bakery is now in place, but we would still like the grocery store to run as a separate actor system, just like we had it in the previous chapter. With untyped Akka, we implemented this communication with the help of remoting, but remoting is not available in the typed setup. With Akka Typed, we can achieve this with the help of clustering.

Akka clustering is a group of Akka systems working as a dynamic whole. This is the main difference from Akka remote, on top of which clustering is built. A single system represents one node from the cluster. An actor can exist anywhere in the cluster. Some of the features of clustering include load balancing (routing messages to specific nodes in the cluster), node partitioning (assigning specific roles to nodes), and cluster management (fault-tolerant node membership), to name a few. In our example, we don't use any advanced clustering features, and instead just throw it in an order so that we have the possibility to communicate with a remote actor system.

To be able to use clustering in our project, we need to add the following dependency to build.sbt:

"com.typesafe.akka" %% "akka-cluster-typed" % akkaVersion,

Clustering also requires that a few configuration parameters are defined. We can provide them by putting the following additional lines into application.conf. This will be the default configuration used by the Bakery:

akka {
actor.provider = "cluster"
remote {
netty.tcp {
hostname = "127.0.0.1"
port = 2552
}
}
cluster.seed-nodes = [
"akka.tcp://[email protected]:2553",
"akka.tcp://[email protected]:2552"
]
}

The configuration for the Store is defined by importing the default configuration and overriding the port definition:

include "application"
akka.remote.netty.tcp.port = 2553

Now, we need to instantiate an actor system for Store:

object Store extends App {
val config = ConfigFactory.load("grocery.conf")
val system = ActorSystem(seller, "Typed-Bakery", config)
}

And we need another one for the Bakery itself:

object Bakery extends App {
...
val system = ActorSystem(Manager.openBakery, "Typed-Bakery")
}

Both of these defined actor systems can now be started and will simulate baking cookies by acquiring the required resources from the remote system via clustering. 

We just demonstrated Akka's location transparency by turning a local actor system into clustered one just by changing the configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.53