scala akka remote 实例
所属分类 akka
浏览量 667
akka.actor.provider = akka.remote.RemoteActorRefProvider
akka.actor.allow-java-serialization = on
akka.remote.artery.enabled = on
akka.remote.artery.canonical.hostname = 127.0.0.1
akka.remote.artery.canonical.port = 8888
val conf = ConfigFactory.parseString(confStr)
val serverActorSystem = ActorSystem("serverActorSystem", conf)
serverActorSystem.actorOf(Props[ServerActor], "serverActor")
akka.remote.artery.enabled off
使用netty
ArteryTransport
注意开启 Java 序列化
akka.actor.allow-java-serialization = on
[WARN] [akka.remote.RemoteActorRefProvider] [serverActorSystem-akka.actor.default-dispatcher-5] [akka.remote.RemoteActorRefProvider] - Using the 'remote' ActorRefProvider directly, which is a low-level layer. For most use cases, the 'cluster' abstraction on top of remoting is more suitable instead.
[WARN] [akka.remote.RemoteActorRefProvider] [serverActorSystem-akka.actor.default-dispatcher-5] [akka.remote.RemoteActorRefProvider] - Akka Cluster not in use - Using Akka Cluster is recommended if you need remote watch and deploy.
[INFO] [akka.remote.artery.ArteryTransport] [serverActorSystem-akka.actor.default-dispatcher-5] [ArteryTransport(akka://serverActorSystem)] - Remoting started with transport [Artery tcp]; listening on address [akka://serverActorSystem@127.0.0.1:8888] with UID [1098002602925710486]
[WARN] [akka.serialization.Serialization(akka://serverActorSystem)] [serverActorSystem-akka.actor.default-dispatcher-11] [akka.serialization.Serialization(akka://serverActorSystem)] - Using the Java serializer for class [com.example.dyyx.remote.Response] which is not recommended because of performance implications.
Use another serializer or disable this warning using the setting 'akka.actor.warn-about-java-serializer-usage'
Using the 'remote' ActorRefProvider directly, which is a low-level layer.
For most use cases, the 'cluster' abstraction on top of remoting is more suitable instead.
Akka Cluster not in use - Using Akka Cluster is recommended if you need remote watch and deploy.
remote 低层次的api ,大多数场景 推荐使用 cluster
[ArteryTransport(akka://serverActorSystem)] - Remoting started with transport [Artery tcp]
listening on address [akka://serverActorSystem@127.0.0.1:8888] with UID [1098002602925710486]
akka://serverActorSystem@127.0.0.1:8888
Using the Java serializer for class [com.example.dyyx.remote.Response] which is not recommended because of performance implications.
Use another serializer or disable this warning using the setting 'akka.actor.warn-about-java-serializer-usage'
java 序列化不推荐使用
客户端
akka.actor.provider = akka.remote.RemoteActorRefProvider
akka.actor.allow-java-serialization=on
akka://serverActorSystem@127.0.0.1:8888/user/serverActor
val conf = ConfigFactory.parseString(confStr)
val clientActorSystem = ActorSystem("clientActorSystem", conf)
val clientActor = clientActorSystem.actorOf(Props[ClientActor], "clientActor")
clientActor ! Request("hello")
class ClientActor extends Actor {
val url = "akka://serverActorSystem@127.0.0.1:8888/user/serverActor"
//
val remoteActor = context.actorSelection(url)
println("remoteActor=" + remoteActor)
def receive: Receive = {
case request: Request =>
println("request=" + request)
remoteActor ! request
case response: Response =>
println("response=" + response)
case _ => println("error msg")
}
}
[INFO] [akka.remote.artery.ArteryTransport] [clientActorSystem-akka.actor.default-dispatcher-4] [ArteryTransport(akka://clientActorSystem)] - Remoting started with transport [Artery tcp]; listening on address [akka://clientActorSystem@192.168.1.101:25520] with UID [3357566597050948290]
[ArteryTransport(akka://clientActorSystem)] - Remoting started with transport [Artery tcp];
listening on address [akka://clientActorSystem@192.168.1.101:25520] with UID [3357566597050948290]
默认端口 25520
客户端如何 不暴露远程端口 ?
remoteActor=ActorSelection[Anchor(akka://serverActorSystem@127.0.0.1:8888/), Path(/user/serverActor)]
完整代码
https://gitee.com/dyyx/hellocode/tree/master/demo/scala/akka/akka-quickstart-scala-maven/src/main/java/com/example/dyyx/remote
上一篇
下一篇
Scala 偏函数 例子1
Scala 偏函数 例子2
akka typed actor ask 实例
scala akka remote ask 实例
scala future mapTo 用法
scala强制类型转换