Last active
May 19, 2022 18:12
-
-
Save phoenix24/6097895 to your computer and use it in GitHub Desktop.
sample akka cluster client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spikes.cluster | |
import akka.actor._ | |
import com.typesafe.config.ConfigFactory | |
import akka.contrib.pattern.{ClusterClient, ClusterReceptionistExtension} | |
object DemoMaster { | |
def main(args: Array[String]): Unit = { | |
val config = ConfigFactory.parseString(""" | |
akka { | |
actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
log-remote-lifecycle-events = off | |
netty.tcp { | |
hostname = "127.0.0.1" | |
port = 2551 | |
} | |
} | |
cluster { | |
seed-nodes = [ | |
"akka.tcp://[email protected]:2551" | |
] | |
roles = [master] | |
auto-down = on | |
} | |
}""") | |
val system = ActorSystem("ClusterSystem", ConfigFactory.load(config)) | |
val master = system.actorOf(Props[ClusterMaster], "master") | |
ClusterReceptionistExtension(system).registerService(master) | |
} | |
class ClusterMaster extends Actor with ActorLogging { | |
def receive = { | |
case e => | |
log.info(s"from master : $e : $sender") | |
sender ! "master : how are you?" | |
} | |
} | |
} | |
object DemoMember { | |
def main(args: Array[String]) { | |
val config = ConfigFactory.parseString(""" | |
akka { | |
actor { | |
provider = "akka.cluster.ClusterActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
log-remote-lifecycle-events = off | |
netty.tcp { | |
hostname = "127.0.0.1" | |
port = 3000 | |
} | |
} | |
cluster { | |
seed-nodes = [ | |
"akka.tcp://[email protected]:2551" | |
] | |
auto-down = on | |
} | |
}""") | |
val system = ActorSystem("ClusterSystem", ConfigFactory.load(config)) | |
val clusterMember = system.actorOf(Props[ClusterMember], "member") | |
ClusterReceptionistExtension(system).registerService(clusterMember) | |
} | |
class ClusterMember extends Actor with ActorLogging { | |
def receive = { | |
case e => | |
log.info(s"from member : $e : $sender") | |
sender ! "member : how are you?" | |
} | |
} | |
} | |
object DemoClient { | |
def main(args : Array[String]) { | |
val config = ConfigFactory.parseString(""" | |
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
} | |
remote { | |
transport = "akka.remote.netty.NettyRemoteTransport" | |
log-remote-lifecycle-events = off | |
netty.tcp { | |
hostname = "127.0.0.1" | |
port = 5000 | |
} | |
} | |
}""") | |
val system = ActorSystem("OTHERSYSTEM", ConfigFactory.load(config)) | |
val initialContacts = Set( | |
system.actorSelection("akka.tcp://[email protected]:2551/user/receptionist"), | |
system.actorSelection("akka.tcp://[email protected]:3000/user/receptionist")) | |
val c = system.actorOf(ClusterClient.props(initialContacts), "os-client") | |
(1 to 1000).map { i => | |
c ! ClusterClient.Send("/user/master", s"hello - $i", localAffinity = true) | |
c ! ClusterClient.Send("/user/member", s"hello - $i", localAffinity = true) | |
Thread.sleep(1000) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed to update this code with latest Akka version. Also, I added code so ClusterClient sends it's own actorRef in order to get a reply back directly, and took out DemoMember. The code below is tested with sbt.version = 0.13.15.
build.sbt
name := "ClusterClient"
version := "0.1"
scalaVersion := "2.12.4"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.5.14"
libraryDependencies += "com.typesafe.akka" %% "akka-cluster-tools" % "2.5.14"