首页 > 解决方案 > 配置 Akka.Net Remoting 以便(Windows)docker 容器内的进程可以与本地主机上的进程通信

问题描述

我有 2 个 Akka.Net 进程,它们通过远程处理相互通信。

进程 1 有以下 app.hocon:

 akka{
 actor{
    provider = remote
 }

 remote{
    dot-netty.tcp.port = 1111
 }
}

进程 2 有以下 app.hocon:

 akka{
 actor{
    provider = remote
 }

 remote{
    dot-netty.tcp.port = 2222
    dot-netty.tcp.hostname = "localhost"

 }
}

请注意,配置非常简单,因为两个进程都在本地主机上。

进程 1 使用以下命令连接到进程 2 参与者:

Process1ActorSystem.ActorSelection($"akka.tcp://Process2ActorSystem@localhost:2222/user/someActor");

进程 2,在接收到来自进程 1 的“SubscribeMessage”时,将数据“告诉”回进程 1 (Context.Sender.Tell)。

当两个进程都在同一台机器上运行时,它们可以很好地相互通信。

进程 2 在 docker 容器内运行时似乎运行良好(进程 1-<-> 进程 2 通信除外)。

但是,我完全不清楚我需要在进程 2 dockerfile、docker run 命令、进程 1 hocon 和进程 2 hocon 中放入什么,以便在进程 2 运行容器时进程 1 仍然可以通话。

我想我需要在 Process 1 hocon 和 Process 2 hocon 中获得这些值的某些子集的正确组合,并在我的 dockerfile 中获得正确的 EXPOSE 语句和/或在我的 docker run 中获得正确的端口映射。

Process 1 hocon:    
   "port" : "1111".     
    #"hostname": "???? or omit",
    #"bind-hostname": "???? or omit",
    #"bind-port": "???? or omit",
    #"public-hostname": "???? or omit",
    #"public-port": "???? or omit",

Process 2 hocon:    
    "port" : "2222".     
    #"hostname": "???? or omit",
    #"bind-hostname": "???? or omit",
    #"bind-port": "???? or omit",
    #"public-hostname": "???? or omit",
    #"public-port": "???? or omit",

Dockerfile:
    EXPOSE 2222 #or omit?

Docker run:
  docker run -p 2222:2222 mycontainer

非常感谢任何指导。

更新:

我提高了调试日志级别,现在当进程 1 尝试发送消息时,我从进程 2 中看到了这一点。注意:容器的 IP 是 172.22.87.66,这是进程 1 寻址的地方。但似乎因为进程 2 的主机名是 0.0.0.0 它接收到消息,但拒绝了它。

可能我可以通过将 Process 2 hocon 更改为主机名 172.22.87.66 来解决此问题,但是每次运行容器时 IP 地址都会更改,因此这不能成为整个解决方案。

[ERROR][3/7/2020 10:24:03 AM][Thread 0010][akka.tcp://ActorSystemRemote2@0.0.0.0:8222/system/endpointManager/reliableEndpointWriter-akka.tcp%3A%2F%2FActorSystemRemote1%40host.docker.internal%3A8111-1/endpointWriter] Dropping message [Akka.Actor.ActorSelectionMessage] for non-local recipient [[akka.tcp://ActorSystemRemote2@172.22.87.66:8222/]] arriving at [akka.tcp://ActorSystemRemote2@172.22.87.66:8222] inbound addresses [akka.tcp://ActorSystemRemote2@0.0.0.0:8222]

更新 2

好的,现在我确实有两个进程进行通信,但这不是一个可行的长期设置。我附加到正在运行的容器并做了一个 ipconfig 来获取它的 IP。我想我可以用

docker network inspect nat 

或类似的。

获得 IP 后,我在正在运行的容器中编辑 app.hocon 并重新启动 Process2 可执行文件。

然后从进程 1 我使用该 IP 寻址进程 2。

此外,进程 1 正在使用 host.docker.internal 的公共主机名,我认为这对于进程 2 能够解决它是必要的。

akka{
 actor{
    provider = remote
 }

 remote{
    dot-netty.tcp.port = 8111,
    dot-netty.tcp.hostname = 172.22.80.1
    dot-netty.tcp.public-hostname=host.docker.internal
 }
}

在此处输入图像描述

标签: docker-for-windowsakka.net

解决方案


结果很简单。

akka{
 actor{
    provider = remote
 }

 remote{
    dot-netty.tcp.port = 1111
 }
}
Process 2 has the following app.hocon:

 akka{
 actor{
    provider = remote
 }

 remote{
    dot-netty.tcp.port = 2222
    dot-netty.tcp.public-hostname = "localhost" //this was the key addition

 }
}

docker run -p 2222:2222 myimage

并且dockerfile中有EXPOSE 2222。


推荐阅读