Glokka = Global + Akka
Glokka is a Scala library that allows you to register and lookup actors by names in an Akka cluster. See:
Glokka is used in Xitrum to implement its distributed SockJS feature.
See Glokka's Scaladoc.
In your SBT project's build.sbt:
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.6"
libraryDependencies += "com.typesafe.akka" %% "akka-cluster" % "2.3.6"
libraryDependencies += "com.typesafe.akka" %% "akka-contrib" % "2.3.6"
libraryDependencies += "tv.cntt" %% "glokka" % "2.0"
In your Scala code:
import akka.actor.ActorSystem
import glokka.Registry
val system = ActorSystem("MyClusterSystem")
val proxyName = "my proxy name"
val registry = Registry.start(system, proxyName)
- You can start multiple registry actors. They must have different
proxyName
. - For convenience,
proxyName
can be any String, you don't have to URI-escape it.
Send:
// For convenience, ``actorName`` can be any String, you don't have to URI-escape it.
val actorName = "my actor name"
// Props to create the actor you want to register.
val props = ...
registry ! Registry.Register(actorName, props)
If the named actor exists, the registry will just return it. You will receive:
Registry.Found(actorName, actorRef)
Otherwise props
will be used to create the actor locally (when the actor dies, it will be unregistered automatically). You will receive:
Registry.Created(actorName, actorRef)
If you don't need to differentiate Found
and Created
:
registry ! Registry.Register(actorName, props)
context.become {
case msg: Registry.FoundOrCreated =>
val actorName = msg.name
val actorRef = msg.ref
}
Send:
registry ! Lookup(actorName)
You will receive:
Found(actorName, actorRef)
Or:
NotFound(actorName)
Glokka can run in Akka non-cluster mode (local or remote). While developing, you can run Akka in local mode, then later config Akka to run in cluster mode.
In cluster mode, Glokka uses Akka's Cluster Singleton Pattern to maintain an actor that stores the name -> actorRef lookup table.
Akka config file for a node should look like this (note "ClusterSystem" in the source code example above and the config below):
akka {
actor {
provider = "akka.cluster.ClusterActorRefProvider"
}
# This node
remote {
log-remote-lifecycle-events = off
netty.tcp {
hostname = "127.0.0.1"
port = 2551 # 0 means random port
}
}
cluster {
seed-nodes = [
"akka.tcp://MyClusterSystem@127.0.0.1:2551",
"akka.tcp://MyClusterSystem@127.0.0.1:2552"]
auto-down-unreachable-after = 10s
}
}