RabbitMQ未运行时消息/主题接收器崩溃

0 投票
1 回答
638 浏览
提问于 2025-04-17 21:07

我在Linux操作系统上使用Python和Pika。每当RabbitMQ没有运行时,消息/主题接收器就会崩溃。我想知道有没有办法让消息/主题接收器在RabbitMQ没有运行的情况下继续工作,因为RabbitMQ和消息/主题接收器不会在同一台虚拟机上。

这也包括了如果RabbitMQ因为某种原因崩溃,消息/主题接收器应该继续运行。这样就不用每次都重新启动消息/主题接收器了。

1 个回答

1

根据我的理解,你提到的“消息/主题接收者”其实就是消费者。你需要负责设计一个应用程序,让它在尝试连接到没有运行的RabbitMQ时能够捕捉到异常。

举个例子:

creds = pika.PlainCredentials(**creds)                             
params = pika.ConnectionParameters(credentials=creds,                   
                                   **conn_params)                  
try:                                                                    
    connection = pika.BlockingConnection(params)                   
    LOG.info("Connection to Rabbit was established")                    
    return connection                                              
except (ProbableAuthenticationError, AuthenticationError):              
    LOG.error("Authentication Failed", exc_info=True)                   
except ProbableAccessDeniedError:                                       
    LOG.error("The Virtual Host configured wrong!", exc_info=True)         
except ChannelClosed:                                                   
    LOG.error("ChannelClosed error", exc_info=True)                     
except AMQPConnectionError:                                             
    LOG.error("RabbitMQ server is down or Host Unreachable")            
    LOG.error("Connection attempt timed out!")                          
    LOG.error("Trying to re-connect to RabbitMQ...")                    
    time.sleep(reconnection_interval)  
    # <here goes your reconnection logic >                             

至于确保你的Rabbit服务器始终运行:

  • 你可以创建一个集群,让你的队列保持持久化和高可用性。
  • 安装一些监控工具(比如monit或supervisord),并配置它来检查Rabbit的进程。例如:

    check process rabbitmq with pidfile /var/run/rabbitmq/pid                       
      start program = "/etc/init.d/rabbitmq-server stop"                            
      stop program = "/etc/init.d/rabbitmq-server start"                            
      if 3 restarts within 5 cycles then alert 
    

撰写回答