zoukankan      html  css  js  c++  java
  • RabbitMQ

    1 安装

     启动服务器 

     rabbitmq-server

    你也可以添加 -detached 属性来让它在后台运行(注意:只有一个破折号)

    sudo rabbitmq-server -detached

    永远不要用 kill 停止 RabbitMQ 服务器,而是应该用 rabbitmqctl 命令:

    sudo rabbitmqctl stop

     2 安装 控制台插件 web  页面详情

    rabbitmq-plugins enable  rabbitmq_management

    4 列出有哪些队列,有哪些消息在等待中

    rabbitmqctl list_queues

    简单的hello world

    1 生产者发送 hello world 消息队列中

    2 消费者获取消息并打印

    [root@jinkang-e2elog rabbitmq]# cat send.py
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    #创建一个名称为hello的队列
    channel.queue_declare(queue='hello')
    
    for i in range(10):
            channel.basic_publish(exchange='',
                          routing_key='hello',
                          body='Hello ' + str(i) + ' World')
    #recive.py
    
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    #创建一个名称为hello的队列
    channel.queue_declare(queue='hello')
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
    channel.basic_consume(on_message_callback=callback,queue='hello',auto_ack=True)
    channel.start_consuming()  #进程会一直执行

    默认来说,RabbitMQ会按顺序得把消息发送给每个消费者(consumer)。
    平均每个消费者都会收到同等数量得消息。这种发送消息得方式叫做——轮询(round-robin)

    以上面的 hello world demo 来说, 生产者 发送了 10个消息  range(10)

    我们在两个窗口 执行两个 recive.py ,构造两个 worker

     

    rabbitmq 会均匀的将消息发送给两个 woker。

  • 相关阅读:
    Docker 部署项目
    Python+Pywinauto+Lackey 实现PC端.exe 自动化测试
    03_Fiddler 导出jmx文件
    02_Postman 中文汉化版
    07_Linux系统(Centos)安装tomcat和部署Web项目
    05_oracel题集
    02_appium基本使用
    01_appium的安装
    02_Monkey使用
    01_Monkey安装
  • 原文地址:https://www.cnblogs.com/jkklearn/p/13772008.html
Copyright © 2011-2022 走看看