我有一个码头撰写文件:
version: '3.3'
services:
bifrost:
image: ivorytoast3853/bifrost
container_name: bifrost-app
ports:
- "8084:8084"
thor:
image: ivorytoast3853/thor
container_name: thor-app
ports:
- "8085:8084"
loki:
image: ivorytoast3853/loki
container_name: loki-app
ports:
- "8086:8084"这是为了测试ZeroMQ应用程序。
我正在使用ZeroMQ的start指南中的确切代码(当我在本地启动它时--如果没有Docker 工作(Loki通过Bifrost向Thor发送消息))。供参考的三个文件是:
LOKI
try (ZContext context = new ZContext()) {
ZMQ.Socket requester = context.createSocket(SocketType.REQ);
boolean didConnect = requester.connect("tcp://0.0.0.0:5559");
log.info("Loki connected to the bifrost: " + didConnect);
for (int request_nbr = 0; request_nbr < 10; request_nbr++) {
requester.send("One", 0);
String reply = requester.recvStr(0);
System.out.println("Received reply " + request_nbr + " [" + reply + "]");
}
}Thor
try (ZContext context = new ZContext()) {
ZMQ.Socket responder = context.createSocket(SocketType.REP);
boolean didConnect = responder.connect("tcp://0.0.0.0:5560");
log.info("Thor connected to the bifrost: " + didConnect);
while (!Thread.currentThread().isInterrupted()) {
String string = responder.recvStr(0);
System.out.printf("Received request: [%s]\n", string);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
responder.send("You sent me: " + string);
}
}Bifrost
while (true) {
try (ZContext context = new ZContext()) {
ZMQ.Socket frontend = context.createSocket(SocketType.ROUTER);
ZMQ.Socket backend = context.createSocket(SocketType.DEALER);
frontend.bind("tcp://*:5559");
backend.bind("tcp://*:5560");
log.info("Started Bifrost to connect Loki and Thor");
ZMQ.Poller items = context.createPoller(2);
items.register(frontend, ZMQ.Poller.POLLIN);
items.register(backend, ZMQ.Poller.POLLIN);
boolean more = false;
byte[] message;
while (!Thread.currentThread().isInterrupted()) {
items.poll();
if (items.pollin(0)) {
while (true) {
message = frontend.recv(0);
more = frontend.hasReceiveMore();
backend.send(message, more ? ZMQ.SNDMORE : 0);
if (!more) {
break;
}
}
}
if (items.pollin(1)) {
while (true) {
message = backend.recv(0);
more = backend.hasReceiveMore();
frontend.send(message, more ? ZMQ.SNDMORE : 0);
if (!more) {
break;
}
}
}
}
}
}我对码头工人撰写文件做错了什么吗?我知道Docker撰写会自动创建一个网络..。
谢谢!
发布于 2021-03-31 05:28:19
结果,我并没有把码头和集装箱作为一个整体的基本概念内化。
问题
"tcp://0.0.0.0:5560" (从洛基/雷神到比弗罗斯特)。,为什么这是个问题?
这是一个问题,因为与在同一台计算机上启动所有3个spring引导应用程序不同(使用相同的IP),我在自己的坞容器中启动每个春季应用程序--这个容器有自己独特的IP。因此,我不能对洛基/托尔说“在这台电脑上( IP ),连接到比弗罗斯特”。
我是如何修复它的:
我更改了Bifrost的docker-组合文件,以包含一个网络别名:
image: ivorytoast3853/bifrost
container_name: bifrost-app
networks:
my-net:
aliases:
- queue所有这些都允许我说,“如果我给您"queue”的主机名,请连接到Bifrost应用程序所在的容器的IP地址。“
然后,我所要做的就是更改主机: Loki和Thor中的端口字符串,以反映以下内容:
responder.connect("tcp://queue:5560");
希望这能帮助任何遇到类似问题的人(或者在我的案例中缺乏理解)。
https://stackoverflow.com/questions/66881582
复制相似问题