docker容器的RabitMQ/Pika消费者问题
我正在尝试将我非常基本的消费容器连接到RabbitMQ容器。我以前设法使它正常工作,但是现在它拒绝工作。 这是下面的消费者代码
def on_message(channel, method_frame, header_frame, body):
print(method_frame.delivery_tag)
print(body)
print()
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
print("Trying to connect 1")
credentials = pika.PlainCredentials(username="admin", password="pass")
print("Trying to connect 2")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq', credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_qos(prefetch_count=0)
print("abc")
channel.basic_consume('hello', on_message)
try:
channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()
是我的docker-compose文件:
services:
rabbitmq:
container_name: rabbitmq
image: "rabbitmq:3.6-management-alpine"
hostname: "rabbitmq-host"
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: "admin"
RABBITMQ_DEFAULT_PASS: "pass"
networks:
- rabbitnetwork
clairvoyance:
container_name: clairvoyance
image: clairvoyance
restart: always
build:
context: .
dockerfile: docker/clairvoyance/dockerfile
depends_on:
- rabbitmq
ports:
- '8081:8081'
networks:
- rabbitnetwork
networks:
rabbitnetwork:
driver: bridge
当我运行容器时,它在控制台中显示RabbitMQ确实正在接受连接:
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | started TCP Listener on [::]:5672
rabbitmq |
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | accepting AMQP connection <0.401.0> (192.168.80.3:33138 -> 192.168.80.2:5672)
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | connection <0.401.0> (192.168.80.3:33138 -> 192.168.80.2:5672): user 'admin' authenticated and granted access to vhost '/'
但是在那之后,什么也不会发生。我从未在控制台中看到“ ABC”。
我尝试使用RabbitMQ GUI,发现有一个名为“ Hello”的空闲队列,但向队列发布任何消息都没有任何作用。
我尝试连接一个简单的发布者,并使用basic_publish发布了一条消息,它起作用了。唯一的问题是消费者。
非常感谢任何帮助,谢谢!
更新:basic_consume()立即添加connect.close()将显示打印(“ abc)消息,然后退出(?)
I'm trying to connect my very basic consumer container to the rabbitmq container. I managed to get it working before but now it just refuses to work.
Here is the consumer code
def on_message(channel, method_frame, header_frame, body):
print(method_frame.delivery_tag)
print(body)
print()
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
print("Trying to connect 1")
credentials = pika.PlainCredentials(username="admin", password="pass")
print("Trying to connect 2")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq', credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_qos(prefetch_count=0)
print("abc")
channel.basic_consume('hello', on_message)
try:
channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()
Below is my docker-compose file:
services:
rabbitmq:
container_name: rabbitmq
image: "rabbitmq:3.6-management-alpine"
hostname: "rabbitmq-host"
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: "admin"
RABBITMQ_DEFAULT_PASS: "pass"
networks:
- rabbitnetwork
clairvoyance:
container_name: clairvoyance
image: clairvoyance
restart: always
build:
context: .
dockerfile: docker/clairvoyance/dockerfile
depends_on:
- rabbitmq
ports:
- '8081:8081'
networks:
- rabbitnetwork
networks:
rabbitnetwork:
driver: bridge
When I run the containers, it shows in the console that rabbitmq is indeed accepting a connection:
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | started TCP Listener on [::]:5672
rabbitmq |
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | accepting AMQP connection <0.401.0> (192.168.80.3:33138 -> 192.168.80.2:5672)
rabbitmq | =INFO REPORT==== 1-Jun-2022::21:04:37 ===
rabbitmq | connection <0.401.0> (192.168.80.3:33138 -> 192.168.80.2:5672): user 'admin' authenticated and granted access to vhost '/'
But after that, nothing happens. I never saw "abc" printed out in the console.
I tried using the RabbitMQ GUI, saw that there's an idle queue named "hello" but publishing any message to the queue does nothing.
I tried connecting a simple publisher and published a message using basic_publish, it worked. The only problem is the consumer.
Any help is greatly appreciated, thank you!
Update: Adding connection.close() right after basic_consume() will display the print("abc) message then exit(?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我需要
env Pythonunbuffered = 1
在我的docker文件中以打印出任何print()
语句。很奇怪,我不需要与发布者一起做。为了避免此问题,可以使用记录而不是打印
Turns out I need
ENV PYTHONUNBUFFERED=1
in my receiver's docker file for it to print out anyprint()
statement. Weird that I don't need to do that with my publisher.To avoid this problem, one can use logging instead of print