美文网首页
rabbitMQ的简单安装使用

rabbitMQ的简单安装使用

作者: aq_wzj | 来源:发表于2019-10-15 18:04 被阅读0次

    1. 安装

    安装erl:apt-get install erlang-nox
    查看erl是否安装成功:erl
    安装源:wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
    更新源:apt-get update
    安装rabbitmq:apt-get install rabbitmq-server
    查看状态:systemctl status rabbitmq-server
    开启WebUI管理页面:rabbitmq-plugins enable rabbitmq_management
    重启:service rabbitmq-server restart
    查看用户列表:rabbitmqctl list_users
    添加【username】用户:rabbitmqctl add_user username password
    给【username】用户管理员权限:rabbitmqctl set_user_tags username administrator
    开机启动:systemctl enable  rabbitmq-server
    查看消息列队:rabbitmqctl list_queues
    

    2. 简单使用 参考https://www.cnblogs.com/shenh/p/10497244.html

    2.1 发送消息给队列(重启服务消息丢失)

    import pika
    
    credentials = pika.PlainCredentials('username', 'password')
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        '10.131.70.49', 5672, '/', credentials))
    channel = connection.channel()
    
    # 声明消息队列,消息将在这个队列传递
    channel.queue_declare(queue='balance')
    
    # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
    channel.basic_publish(exchange='',
                          routing_key='balance',
                          body='Hello World!',
                          )
    print(" [x] Sent 'Hello World!'")
    connection.close()
    

    2.2 发送消息给队列(持久化,重启服务消息还在)

    import pika
    
    credentials = pika.PlainCredentials('root', 'root')
    connection = pika.BlockingConnection(pika.ConnectionParameters(
         '10.131.70.49',5672,'/',credentials))
    channel = connection.channel()
    
    # 声明queue
    channel.queue_declare(queue='durable',durable=True)  # 多了durable=True
    
    # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
    channel.basic_publish(exchange='',
                          routing_key='durable',
                          body='Hello cheng!',
                          properties=pika.BasicProperties(
                              delivery_mode=2,  # make message persistent
                          )
                          )
    print(" [x] Sent 'Hello cheng!'")
    connection.close()
    

    2.3 取消息

    import pika
    
    credentials = pika.PlainCredentials('root', 'root')
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        '10.131.70.49', 5672, '/', credentials))
    channel = connection.channel()
    
    # 若取持久化的消息:
    # channel.queue_declare(queue='balance', durable=True)
    # 若取无持久化的消息:
    channel.queue_declare(queue='balance')
    
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
    
    
    channel.basic_consume('balance',
                          callback,
                          True
                           # False,在调用callback函数时,未收到确认标识,消息会重回队列。
                           # True,无论调用callback成功与否,消息都被消费掉
    ) 
     # no_ack 设置成 False,在调用callback函数时,未收到确认标识,消息会重回队列。
     # True,无论调用callback成功与否,消息都被消费掉
    
    
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
    
    

    相关文章

      网友评论

          本文标题:rabbitMQ的简单安装使用

          本文链接:https://www.haomeiwen.com/subject/mlzsmctx.html