Rabbitmq错误:[Errno 10054]远程主机强制关闭了现有连接

pbgvytdp  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(3)|浏览(613)

我在Python中使用Kombu来使用持久的RabbitMQ队列。
在Windows中只有一个使用者在使用队列。此使用者产生以下错误:

Traceback (most recent call last):
  File ".\consumer_windows.py", line 66, in <module>
    message.ack()
  File "C:\Users\Administrator\Anaconda2\lib\site-packages\kombu\message.py", line 88, in ack
    self.channel.basic_ack(self.delivery_tag)
  File "C:\Users\Administrator\Anaconda2\lib\site-packages\amqp\channel.py", line 1584, in basic_ack
    self._send_method((60, 80), args)
  File "C:\Users\Administrator\Anaconda2\lib\site-packages\amqp\abstract_channel.py", line 56, in _send_method
    self.channel_id, method_sig, args, content,
  File "C:\Users\Administrator\Anaconda2\lib\site-packages\amqp\method_framing.py", line 221, in write_method
    write_frame(1, channel, payload)
  File "C:\Users\Administrator\Anaconda2\lib\site-packages\amqp\transport.py", line 182, in write_frame
    frame_type, channel, size, payload, 0xce,
  File "C:\Users\Administrator\Anaconda2\lib\socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

队列中一次最多有500封邮件。每封邮件都很小,但它是一项任务,最多需要10分钟才能完成(尽管每封邮件通常需要不到5分钟的时间)。
我已尝试重新启动消费者、RabbitMQ服务器并删除队列,但错误仍然存在。
我看过this question,但是the answer是2010年的,我的rabbitmq.log有不同的条目:

=ERROR REPORT==== 24-Apr-2016::08:26:20 ===
closing AMQP connection <0.6716.384> (192.168.X.X:59602 -> 192.168.Y.X:5672):
{writer,send_failed,{error,timeout}}

rabbitmq-sasl.log中没有最近的事件。
为什么会发生此错误?如何防止此错误发生?

63lcw9qa

63lcw9qa1#

我还在寻找答案,同时我重新连接到我的兔子服务器:

while True:
    try:
​
        connection = pika.BlockingConnection(params)
        channel = connection.channel() # start a channel
        channel.queue_declare(queue=amqp_q, durable=True) # Declare a queue
        ...
​
    except pika.exceptions.ConnectionClosed:
        print('connection closed... and restarted')
jdg4fx2g

jdg4fx2g2#

我有同样的问题与MySQL服务器托管...我来了解,它发生了,如果我们打开连接很长一段时间或未经修改的很长一段时间..如果您的程序打开数据库或任何东西,直到整个程序运行使它在这样一种方式,它打开数据库写入一切,关闭和重复
我不知道rabbitmq到底是什么,但我认为你写的标题错误可能是因为这个原因

c7rzv4ha

c7rzv4ha3#

我遇到了同样的错误(使用纯PIKA库),并试图通过Amazon MQ连接到RabbitMQ代理。
正确设置ssl配置后,问题解决。
请在此处查看完整的博客文章:https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/amazon-mq-rabbitmq-pika.html

我使用的核心代码片段:

定义Pika客户端:

import ssl
import pika

class BasicPikaClient:

    def __init__(self, rabbitmq_broker_id, rabbitmq_user, rabbitmq_password, region):

        # SSL Context for TLS configuration of Amazon MQ for RabbitMQ
        ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        ssl_context.set_ciphers('ECDHE+AESGCM:!ECDSA')

        url = f"amqps://{rabbitmq_user}:{rabbitmq_password}@{rabbitmq_broker_id}.mq.{region}.amazonaws.com:5671"
        parameters = pika.URLParameters(url)
        parameters.ssl_options = pika.SSLOptions(context=ssl_context)

        self.connection = pika.BlockingConnection(parameters)
        self.channel = self.connection.channel()

生产商:

from basicClient import BasicPikaClient

class BasicMessageSender(BasicPikaClient):

    def declare_queue(self, queue_name, durable):
        print(f"Trying to declare queue({queue_name})...")
        self.channel.queue_declare(queue=queue_name, durable=durable)

    def send_message(self, exchange, routing_key, body):
        channel = self.connection.channel()
        channel.basic_publish(exchange=exchange,
                              routing_key=routing_key,
                              body=body)
        print(f"Sent message. Exchange: {exchange}, Routing Key: {routing_key}, Body: {body}")

    def close(self):
        self.channel.close()
        self.connection.close()

调用生成器:


# Initialize Basic Message Sender which creates a connection

# and channel for sending messages.

basic_message_sender = BasicMessageSender(
    credentials["broker_id"],
    credentials["username"],
    credentials['password'],
    credentials['region']
)

# Declare a queue

basic_message_sender.declare_queue("q_name", durable=True)

# Send a message to the queue.

basic_message_sender.send_message(exchange="", routing_key="q_name", body=b'Hello World 2!')

# Close connections.

basic_message_sender.close()

定义消费者:

class BasicMessageReceiver(BasicPikaClient):

    def get_message(self, queue):
        method_frame, header_frame, body = self.channel.basic_get(queue)
        if method_frame:
            print(method_frame, header_frame, body)
            self.channel.basic_ack(method_frame.delivery_tag)
            return method_frame, header_frame, body
        else:
            print('No message returned')

    def close(self):
        self.channel.close()
        self.connection.close()

呼叫消费者:


# Create Basic Message Receiver which creates a connection

# and channel for consuming messages.

basic_message_receiver = BasicMessageReceiver(
    credentials["broker_id"],
    credentials["username"],
    credentials['password'],
    credentials['region']
)

# Consume the message that was sent.

basic_message_receiver.get_message("q_name")

# Close connections.

basic_message_receiver.close()

我希望以上有帮助。谢谢

相关问题