2014-03-06 79 views
0

我在linux OS环境中使用python和pika。 消息/主题当RabbitMQ未运行时,Receiver不断崩溃。 我想知道是否有一种方法可以在RabbitMQ不是因为RabbitMQ不会与消息/主题接收器位于同一虚拟机上时保持邮件/主题接收器运行。RabbitMQ消息/主题接收器在RabbitMQ未运行时崩溃

如果RabbitMQ由于某种原因崩溃,但消息/主题接收器应继续运行,则此封面。保存必须再次启动/重新启动消息/主题接收器。

回答

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 >        

并尽可能确保您兔服务器始终处于运行状态:

  • 您可以创建一个集群让你排队耐用,HA
  • 安装某种类型的监督(比如说monit或supervisord)并将其配置为检查兔子进程。例如:

    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