【问题标题】:Reconnecting to Remote Akka System after Restarting the Client重启客户端后重新连接远程 Akka 系统
【发布时间】:2017-01-28 01:02:38
【问题描述】:

我的用例如下。机器上的应用程序连接到远程机器,在它们上执行脚本并返回结果。我正在使用 Akka 框架进行远程处理,并为客户端应用程序使用 Play 框架。在我的远程机器上运行的服务器代码如下:

public static void main(String[] args)
{
    OnCallServer app = new OnCallServer();
    app.executeServer();
}

private void executeServer() {
    ActorSystem system = ActorSystem.create("OnCallServer");
}

(只是在远程机器上启动一个actor系统的实例)

现在,当客户端应用程序想要在远程机器上运行一个脚本时,它会在这个远程系统上部署一个执行脚本的参与者。

被部署的actor的代码如下:

public static class RemoteActor extends UntypedActor implements Serializable {
    private static final long serialVersionUID = 1L;

    @Override
    public void onReceive(Object message) throws Exception {
        Config config = context().system().settings().config();
        String host = config.getConfig("akka.remote.netty.ssl").getString("machineName");
        String sysDesc = host;
        if (message instanceof ScriptExecutionParams) {
            System.out.println("scriptParam");
            ScriptExecutionParams scriptParams = (ScriptExecutionParams) message;

            if (scriptParams.function == ScriptFunction.EXECUTE) {
                getSender().tell(executeScript(scriptParams.getName(), scriptParams.getArgument(), sysDesc), getSelf());
            } else if (scriptParams.function == ScriptFunction.DEPLOY) {
                getSender().tell(deployScript(scriptParams.getName(), scriptParams.getContent(), sysDesc), getSelf());
            } else if (scriptParams.function == ScriptFunction.REMOVE) {
                getSender().tell(removeScript(scriptParams.getName(), sysDesc), getSelf());
            }
        }
    }
}

(获取脚本参数,执行所需功能,返回结果)

我正在使用基于 SSL 的 TCP 连接进行远程处理。配置如下:

remote {
        enabled-transports = ["akka.remote.netty.ssl"]
        netty.ssl {
            hostname = "localhost" (for client) and hostname (for remote servers)
            port = 10174 (for client) and 10175 ( for server )
            enable-ssl = true
        }
        netty.ssl.security {
            key-store = "clientKeystore.jks"
            trust-store = "clientTruststore.jks"
            key-store-password = "xxx"
            key-password = "xxx"
            trust-store-password = "xxx"
            protocol = "SSLv3"
            enabled-algorithms = [SSL_RSA_WITH_NULL_SHA]
            random-number-generator = ""
        }
    }

此设置完美运行,但有时远程计算机无法访问。我注意到这发生在两种情况下:

  1. 我重新启动客户端应用程序
  2. 远程机器上长时间没有执行脚本时

现在让我感到困惑的是:

  1. 在远程机器上,netstat 显示端口 10175 仍处于打开状态并正在侦听
  2. 在我重新启动客户端应用程序并尝试执行actor后,当我检查远程机器的日志时,它显示actor已在机器上成功执行,但我的客户端应用程序没有收到响应,因此导致超时。

我尝试在客户端参与者中添加一个 supervisorStrategy,但它没有任何效果。难道我做错了什么 ?如果 TCP 连接有问题,有没有办法在每次执行后终止连接?如果问题是演员系统如果长时间不触摸就会关闭,是否有配置可以更改此设置?请询问您是否需要更多代码或信息。

更新

当我在本地机器上测试时尝试重新启动客户端时,它不会出现任何问题。远程服务器只是抛出 akka.remote.EndpointAssociationException 消息但重新连接并能够发送回复。只有在生产模式下,当应用程序部署在不同的机器上时才会出现此问题。我认为我的客户在重新启动时会被隔离,并且 akka.remote.quarantine-systems-for 已在新的 Akka 版本中被删除。

【问题讨论】:

    标签: java akka


    【解决方案1】:

    好的,我发现了问题。对于其他可能面临此问题的人: 在远程机器的配置文件中,在配置的 netty.ssl 部分中,我曾经在客户端应用程序中使用它来提供它们各自的主机名进行连接。但是在客户端应用程序配置中,我曾经将主机名指定为“localhost”,因为我认为我在任何地方都不需要这个。

    现在,在DEBUG模式下查看日志,发现初始连接建立时,关联如下:

    2014-05-01 18:35:38.503UTC 调试 [OnCallServer-akka.actor.default-dispatcher-3] 远程处理 - 关联 [akka.ssl.tcp://OnCallServer@sp-cms-backend4.nm。 Flipkart.com:10175]

    即使客户端应用程序不在机器 localhost 上。现在这个会话没有给出任何错误。但是在连接丢失后(重新启动客户端应用程序后),我尝试重新执行脚本,我得到了日志:

    2014-05-01 18:36:12.045UTC 错误 [OnCallServer-akka.actor.default-dispatcher-2] a.r.EndpointWriter - AssociationError [akka.ssl.tcp://OnCallServer@sp-cms-backend4.nm .flipkart.com:10175] -> [akka.ssl.tcp://application@localhost:10174]: 错误 [与 [akka.ssl.tcp://application@localhost:10174] 的关联失败]] [ akka.remote.EndpointAssociationException:与 [akka.ssl.tcp://application@localhost:10174] 的关联失败 原因:akka.remote.transport.netty.NettyTransport$$anonfun$associate$1$$anon$2:连接被拒绝:localhost/127.0.0.1:10174

    服务器应用程序出于某种原因试图将此消息发送回它的本地主机。

    将客户端配置中的主机名更改为实际的主机名解决了问题。

    【讨论】:

    • 最后一句中的“实际主机名”是指IP?喜欢不使用远程的域名而是使用它的IP?
    • 你可以使用任何东西。只是不要使用本地主机/环回地址
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多