简体   繁体   English

Docker 容器的 RabitMQ/Pika 消费者问题

[英]RabitMQ/Pika consumer problem with Docker Container

I'm trying to connect my very basic consumer container to the rabbitmq container.我正在尝试将我非常基本的消费者容器连接到 rabbitmq 容器。 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:下面是我的 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

When I run the containers, it shows in the console that rabbitmq is indeed accepting a connection:当我运行容器时,它在控制台中显示 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 '/'

But after that, nothing happens.但在那之后,什么也没有发生。 I never saw "abc" printed out in the console.我从未在控制台中看到“abc”打印出来。

I tried using the RabbitMQ GUI, saw that there's an idle queue named "hello" but publishing any message to the queue does nothing.我尝试使用 RabbitMQ GUI,看到有一个名为“hello”的空闲队列,但是将任何消息发布到队列中什么都不做。

I tried connecting a simple publisher and published a message using basic_publish, it worked.我尝试连接一个简单的发布者并使用 basic_publish 发布一条消息,它成功了。 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(?)更新:在 basic_consume() 之后添加 connection.close() 将显示 print("abc) 消息然后 exit(?)

Turns out I need ENV PYTHONUNBUFFERED=1 in my receiver's docker file for it to print out any print() statement.结果我需要在接收器的 docker 文件中使用ENV PYTHONUNBUFFERED=1才能打印出任何print()语句。 Weird that I don't need to do that with my publisher.奇怪的是我不需要和我的出版商这样做。 To avoid this problem, one can use logging instead of print为了避免这个问题,可以使用日志而不是打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM