官术网_书友最值得收藏!

RabbitMQ

RabbitMQ uses AMQP, by default, as the communication protocol, which makes this a very performative tool for the delivery of messages. RabbitMQ documentation is incredible and has native support for the languages most used in the market programming.

Among the many message brokers, RabbitMQ stands out because of its practicality of implementation, flexibility to be coupled with other tools, and its simple and intuitive API.

The following code shows how simple it is to create a system of Hello World in Python with RabbitMQ:

# import the tool communicate with RabbitMQ 
    import pika 
    # create the connection 
    connection = pika.BlockingConnection( 
      pika.ConnectionParameters(host='localhost')) 
    # get a channel from RabbitMQ 
    channel = connection.channel() 
    # declare the queue 
    channel.queue_declare(queue='hello') 
    # publish the message 
    channel.basic_publish(exchange='', 
                      routing_key='hello', 
                      body='Hello World!') 
    print(" [x] Sent 'Hello World!'") 
    # close the connection 
    connection.close() 

With the preceding example, we took the official RabbitMQ site, we are responsible for sending the message Hello World queue for the hello. The following is the code that gets the message:

    # import the tool communicate with RabbitMQ 
    import pika 
  
    # create the connection 
    connection = pika.BlockingConnection( 
    pika.ConnectionParameters(host='localhost')) 
 
    # get a channel from RabbitMQ 
    channel = connection.channel() 
 
    # declare the queue 
    channel.queue_declare(queue='hello') 
  
    # create a method where we'll work with the message received 
    def callback(ch, method, properties, body): 
      print(" [x] Received %r" % body) 
 
    # consume the message from the queue passing to the method 
created above
channel.basic_consume(callback, queue='hello', no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') # keep alive the consumer until interrupt the process channel.start_consuming()

The code is very simple and readable. Another feature of RabbitMQ is the practical tool for scalability. There are performance tests that indicate the ability of RabbitMQ in supporting 20,000 messages per second for each node.

主站蜘蛛池模板: 济源市| 呼图壁县| 彭泽县| 金昌市| 海晏县| 北海市| 理塘县| 张家界市| 长沙市| 河津市| 高平市| 大埔县| 勃利县| 宁河县| 堆龙德庆县| 微博| 华池县| 扶绥县| 霍州市| 石阡县| 宝山区| 曲水县| 吉安市| 楚雄市| 体育| 南召县| 四川省| 志丹县| 耒阳市| 稻城县| 双流县| 英山县| 盐源县| 德庆县| 丽江市| 台东市| 日喀则市| 平罗县| 弥渡县| 大埔县| 静安区|