2011-12-15 18:05:24 +01:00
|
|
|
|
.. _remoting-scala:
|
|
|
|
|
|
|
|
|
|
|
|
#################
|
|
|
|
|
|
Remoting (Scala)
|
|
|
|
|
|
#################
|
|
|
|
|
|
|
|
|
|
|
|
For an introduction of remoting capabilities of Akka please see :ref:`remoting`.
|
|
|
|
|
|
|
|
|
|
|
|
Preparing your ActorSystem for Remoting
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
2011-12-15 21:16:19 +01:00
|
|
|
|
The Akka remoting is a separate jar file. Make sure that you have a dependency from your project to this jar::
|
|
|
|
|
|
|
|
|
|
|
|
akka-remote.jar
|
|
|
|
|
|
|
|
|
|
|
|
In you SBT project you should add the following as a dependency::
|
|
|
|
|
|
|
|
|
|
|
|
"com.typesafe.akka" % "akka-remote" % "2.0-SNAPSHOT"
|
|
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
To enable remote capabilities in your Akka project you should, at a minimum, add the following changes
|
|
|
|
|
|
to your ``application.conf`` file::
|
2011-12-15 18:05:24 +01:00
|
|
|
|
|
|
|
|
|
|
akka {
|
|
|
|
|
|
actor {
|
2011-12-23 10:25:05 +01:00
|
|
|
|
provider = "akka.remote.RemoteActorRefProvider"
|
2011-12-15 18:05:24 +01:00
|
|
|
|
}
|
2011-12-21 23:33:13 +01:00
|
|
|
|
remote {
|
2011-12-23 10:25:05 +01:00
|
|
|
|
transport = "akka.remote.netty.NettyRemoteSupport"
|
2011-12-21 23:33:13 +01:00
|
|
|
|
server {
|
2011-12-23 10:25:05 +01:00
|
|
|
|
hostname = "127.0.0.1"
|
2011-12-21 23:33:13 +01:00
|
|
|
|
port = 2552
|
|
|
|
|
|
}
|
2011-12-23 10:25:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
cluster.nodename = "someUniqueNameInTheCluster1"
|
2011-12-15 18:05:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
As you can see in the example above there are four things you need to add to get started:
|
|
|
|
|
|
|
|
|
|
|
|
* Change provider from ``LocalActorRefProvider`` to ``RemoteActorRefProvider``
|
|
|
|
|
|
* Add host name - the machine you want to run the actor system on
|
|
|
|
|
|
* Add port number - the port the actor system should listen on
|
|
|
|
|
|
* Add cluster node name - must be a unique name in the cluster
|
|
|
|
|
|
|
|
|
|
|
|
The example above only illustrates the bare minimum of properties you have to add to enable remoting.
|
|
|
|
|
|
There are lots of more properties that are related to remoting in Akka. We refer to the following
|
2011-12-15 18:05:24 +01:00
|
|
|
|
reference file for more information:
|
|
|
|
|
|
|
|
|
|
|
|
* `reference.conf of akka-remote <https://github.com/jboner/akka/blob/master/akka-remote/src/main/resources/reference.conf#L39>`_
|
|
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
Types of Remote Interaction
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
|
|
|
|
Akka has two ways of using remoting:
|
|
|
|
|
|
|
|
|
|
|
|
* Lookup : used to look up an actor on a remote node
|
|
|
|
|
|
* Creation : used to create an actor on a remote node
|
|
|
|
|
|
|
2011-12-23 11:29:51 +01:00
|
|
|
|
In the next sections the two alternatives are described in detail.
|
2011-12-23 10:25:05 +01:00
|
|
|
|
|
2011-12-21 23:33:13 +01:00
|
|
|
|
Looking up Remote Actors
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
``actorFor(path)`` will obtain an ``ActorRef`` to an Actor on a remote node, e.g.::
|
2011-12-21 23:33:13 +01:00
|
|
|
|
|
2011-12-23 11:29:51 +01:00
|
|
|
|
val actor = context.actorFor("akka://actorSystemName@10.0.0.1:2552/user/actorName")
|
2011-12-21 23:33:13 +01:00
|
|
|
|
|
|
|
|
|
|
As you can see from the example above the following pattern is used to find an ``ActorRef`` on a remote node::
|
|
|
|
|
|
|
2011-12-23 11:29:51 +01:00
|
|
|
|
akka://<actor system>@<hostname>:<port>/<actor path>
|
2011-12-23 10:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
Once you a reference to the actor you can interact with it they same way you would with a local actor, e.g.::
|
|
|
|
|
|
|
|
|
|
|
|
actor ! "Pretty awesome feature"
|
2011-12-21 23:33:13 +01:00
|
|
|
|
|
|
|
|
|
|
Creating Actors Remotely
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
2011-12-15 18:05:24 +01:00
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
If you want to use the creation functionality in Akka remoting you have to further amend the
|
|
|
|
|
|
``application.conf`` file in the following way::
|
2011-12-15 18:05:24 +01:00
|
|
|
|
|
|
|
|
|
|
akka {
|
|
|
|
|
|
actor {
|
2011-12-23 10:25:05 +01:00
|
|
|
|
provider = "akka.remote.RemoteActorRefProvider"
|
|
|
|
|
|
deployment { /sampleActor {
|
|
|
|
|
|
remote = "akka://sampleActorSystem@127.0.0.1:2553"
|
|
|
|
|
|
}}
|
2011-12-15 18:05:24 +01:00
|
|
|
|
}
|
2011-12-23 10:25:05 +01:00
|
|
|
|
...
|
|
|
|
|
|
|
2011-12-23 11:29:51 +01:00
|
|
|
|
The configuration above instructs Akka to react when an actor with path /sampleActor is created, i.e.
|
|
|
|
|
|
using ``system.actorOf(Props(...)`, sampleActor)``. This specific actor will not be directly instantiated,
|
|
|
|
|
|
but instead the remote daemon of the remote system will be asked to create the actor,
|
|
|
|
|
|
which in this sample corresponds to ``sampleActorSystem@127.0.0.1:2553``.
|
2011-12-23 10:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
Once you have configured the properties above you would do the following in code::
|
|
|
|
|
|
|
|
|
|
|
|
class SampleActor extends Actor { def receive = { case _ => println("Got something") } }
|
|
|
|
|
|
|
|
|
|
|
|
val actor = context.actorOf(Props[SampleActor], "sampleActor")
|
|
|
|
|
|
actor ! "Pretty slick"
|
|
|
|
|
|
|
|
|
|
|
|
``SampleActor`` has to be available to the runtimes using it, i.e. the classloader of the
|
|
|
|
|
|
actor systems has to have a JAR containing the class.
|
2011-12-15 18:05:24 +01:00
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
Remote Sample Code
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
2011-12-15 21:16:19 +01:00
|
|
|
|
|
2011-12-23 10:25:05 +01:00
|
|
|
|
There is a more extensive remote example that comes with the Akka distribution.
|
|
|
|
|
|
Please have a look here for more information:
|
|
|
|
|
|
`Remote Sample <https://github.com/jboner/akka/tree/master/akka-samples/akka-sample-remote>`_
|
2011-12-15 21:16:19 +01:00
|
|
|
|
|
2011-12-15 18:05:24 +01:00
|
|
|
|
Serialization
|
|
|
|
|
|
^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
|
|
|
|
When using remoting for actors you must ensure that the ``props`` and ``messages`` used for
|
|
|
|
|
|
those actors are serializable. Failing to do so will cause the system to behave in an unintended way.
|
|
|
|
|
|
|
2011-12-15 21:16:19 +01:00
|
|
|
|
For more information please see :ref:`serialization-scala`
|
|
|
|
|
|
|
2011-12-15 18:05:24 +01:00
|
|
|
|
Routers with Remote Destinations
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
2011-12-16 12:12:03 +13:00
|
|
|
|
It is absolutely feasible to combine remoting with :ref:`routing-scala`.
|
2011-12-15 18:05:24 +01:00
|
|
|
|
This is also done via configuration::
|
|
|
|
|
|
|
|
|
|
|
|
akka {
|
|
|
|
|
|
actor {
|
|
|
|
|
|
deployment {
|
|
|
|
|
|
/serviceA/aggregation {
|
|
|
|
|
|
router = “round-robin”
|
|
|
|
|
|
nr-of-instances = 10
|
2011-12-17 16:33:29 +01:00
|
|
|
|
routees {
|
2011-12-15 18:05:24 +01:00
|
|
|
|
nodes = [“akka://app@10.0.0.2:2552”, “akka://app@10.0.0.3:2552”]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
This configuration setting will clone the actor “aggregation” 10 times and deploy it evenly distributed across
|
2011-12-16 12:12:03 +13:00
|
|
|
|
the two given target nodes.
|