gpt4 book ai didi

java - 使用 Java 的 Akka 远程路由

转载 作者:行者123 更新时间:2023-12-03 02:32:52 25 4
gpt4 key购买 nike

我正在尝试实现一个基本的广播路由器,其中路由者位于远程计算机上。

代码如下:

localApp.conf

akka {

log-dead-letters = 10
log-dead-letters-during-shutdown = off

actor {
provider = "akka.remote.RemoteActorRefProvider"
serialize-messages = on

serializers {
java = "akka.serialization.JavaSerializer"
}

serialization-bindings {
"java.lang.String" = java
"test.akkaLocal.LocalWrapper" = java
}

deployment {
/LocalMaster/broadcastRouter {
router = "broadcast"
nr-of-instances = 1
target {
nodes = ["akka.tcp://RemoteApp@127.0.0.1:10175"]
}
}
}
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty {
tcp {
hostname = "127.0.0.1"
port = 10174
}
}
}
}

LocalApp.java

public class LocalApp
{
public static void main(String[] args)
{
LocalApp app = new LocalApp();
app.executeLocal();
}

private void executeLocal() {
ActorSystem system = ActorSystem.create("LocalApp", ConfigFactory.load("localApp"));
final ActorRef master = system.actorOf(Props.create(LocalMaster.class), "LocalMaster");
master.tell(new LocalWrapper.Execute(), ActorRef.noSender());
}

public static class LocalMaster extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
if (message instanceof LocalWrapper.Execute) {

ActorSelection remoteActor =
getContext().actorSelection("akka.tcp://RemoteApp@127.0.0.1:10175/user/RemoteMaster");

ActorRef remoteRouter = getContext().actorOf(
Props.create(RemoteActor.class).withRouter(new FromConfig()), "broadcastRouter");

String msg = "Hello";
// remoteActor.tell(msg, getSelf());
remoteRouter.tell(msg, getSelf());
} else if (message instanceof String) {
String response = (String) message;
System.out.println(response);
}
}

}

public static class RemoteActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);

String resp = "World";
getSender().tell(resp, getSelf());

}
}
}
}

在remoteApp.conf中,端口指定为10175

RemoteApp.java

public class RemoteApp
{
public static void main(String[] args)
{
RemoteApp app = new RemoteApp();
app.executeRemote();
}

private void executeRemote() {
ActorSystem system = ActorSystem.create("RemoteApp", ConfigFactory.load("remoteApp"));
system.actorOf(Props.create(RemoteMaster.class), "RemoteMaster");
}

public static class RemoteMaster extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
String msg = (String) message;
System.out.println(msg);
String response = "World";
getSender().tell(response, getSelf());
}
}
}

}

现在我无法理解远程路由的概念。它是在远程计算机上部署本地参与者然后向它们发送消息,还是连接到远程计算机上的远程参与者然后向它们发送消息?

使用我的代码,我可以向远程计算机发送简单的消息(使用参与者选择)LocalApp中的remoteActor.tell(msg, getSelf())(注释代码)发送和接收消息并且不会给出任何错误。

但是当我使用本地 Actor 创建路由器时,出现死信错误。

[INFO] [02/04/2014 16:34:58.408] [RemoteApp-akka.actor.default-dispatcher-4] [akka://RemoteApp/system/endpointManager/reliableEndpointWriter-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0/endpointWriter/endpointReader-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0] 
Message [akka.remote.transport.AssociationHandle$InboundPayload] from Actor[akka://RemoteApp/deadLetters] to Actor[akka://RemoteApp/system/endpointManager/reliableEndpointWriter-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0/endpointWriter/endpointReader-akka.tcp%3A%2F%2FLocalApp%40127.0.0.1%3A10174-0#-288427524] was not delivered.
[1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

有人可以告诉我我做错了什么吗?

---------更新----------

我发现了问题。远程和本地类(class)位于不同的项目中。在本地和远程项目之间的基本通信过程中,String是传输对象的类型,这也是成功的原因。有没有办法在两个不同的项目之间传输自定义类的对象?我尝试实现 Serialized 并将其添加到 conf 文件中,但没有什么区别

最佳答案

默认情况下,akka 将对自定义消息类使用 java 序列化。如果类定义在系统两侧(发送方和接收方)都可用(即在类路径中),那么您应该能够使用它进行远程通信。我的建议是使用一个代表系统两侧的类路径中可用的消息类的 jar 文件。

Akka 还允许您对不同的消息类类型使用不同的序列化器,这样您就不会陷入 java 序列化的困境,但如果您愿意的话,我建议您先让它以这种方式工作,然后再尝试其他序列化器。

关于java - 使用 Java 的 Akka 远程路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com