in git Linux Python rabbit ~ read.
Install RabbitMQ and Send JSON data With Python on Ubuntu

Install RabbitMQ and Send JSON data With Python on Ubuntu

Introduction

RabbitMQ is one of the more popular message broker solutions, offered with an open-source license (Mozilla Public License v1.1) as an implementation of Advanced Message Queuing Protocol(AMQP). Developed using the Erlang language, it is actually relatively easy to use and get started. It was first published in early 2007.

In this article, I will show you about RabbitMQ, How to install RabbitMQ on Ubuntu 14.04 as Queue Messaging. And after that, I will write demo a simple producer to send json data to RabbitMQ, a simple consumer to receive json data from RabbitMQ.

Installing RabbitMQ

On Ubuntu 14.04, Install RabbitMQ is very easy. You just typing in terminal:

sudo apt-get update  
sudo apt-get install rabbitmq-server  

Managing RabbitMQ

To enable RabbitMQ Management Console, run the following:

sudo rabbitmq-plugins enable rabbitmq_management  

Add user to access:

sudo rabbitmqctl add_user admin admin  
sudo rabbitmqctl set_user_tags admin administrator  
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"  

After that, You can access to manager UI using your web browser:

http://your_ip:15672  

Rabbit_Login

Enter user and password that is created above to login and you will see:

Rabbit_Manager

Write Producer to send JSON data

I will write producer by Python and use pika to work with RabbitMQ.

The first, I will create virtualenv for my environment.

virtualenv -p /usr/bin/python2.7 env  
source env/bin/activate  
pip install pika  

Next, I will need to write config file.

RABBIT_HOST= 'localhost' QUEUE_TOPIC = 'topic_name'  

Next step, I write producer to send JSON data in producer.py file.

import pika  
import json  
import config as cfg 

connection = pika.BlockingConnection(pika.ConnectionParameters(host=cfg.RABBIT_HOST))  
channel = connection.channel() channel.queue_declare(queue=cfg.QUEUE_TOPIC) 

data = {  
    "id": 1,         
    "name": "My Name",         
    "description": "This is description about me"     
} 

message = json.dumps(data)  
channel.basic_publish(exchange='', routing_key=cfg.QUEUE_TOPIC, body=message) 

print(" [x] Sent data to RabbitMQ") 

connection.close()  

Next, I will write consumer to receive and print data in consumer.py file.

import pika  
import json  
import config as cfg 

connection = pika.BlockingConnection(pika.ConnectionParameters(host=cfg.RABBIT_HOST))  
channel = connection.channel() channel.queue_declare(queue=cfg.QUEUE_TOPIC) 

print(' [*] Waiting for messages. To exit press CTRL+C') 

def callback(ch, method, properties, body):  
    print("Method: {}".format(method))     
    print("Properties: {}".format(properties))     
    data = json.loads(body)     
    print("ID: {}".format(data['id']))     
    print("Name: {}".format(data['name']))      
    print('Description: {}'.format(data['description'])) 

channel.basic_consume(callback, queue=cfg.QUEUE_TOPIC,no_ack=True)  
channel.start_consuming()  

Finally, I will run it. I run producer and will see:

(env)[email protected]:~/working/source_code/rabbit-queue$ python producer.py
[x] Sent data to RabbitMQ 
(env)[email protected]:~/working/source_code/rabbit-queue$

After, With consumer:

(env)[email protected]:~/working/source_code/rabbit-queue$ python consumer.py  
[*] Waiting for messages. To exit press CTRL+C 
Method: <Basic.Deliver(['consumer_tag=ctag1.a3b15380056c4c55b95f316598ba51a5',  
'delivery_tag=1', 'exchange=', 'redelivered=False', 'routing_key=topic_name'])>  
Properties: <BasicProperties>  
ID: 1  
Name: My Name  
Description: This is description about me  

So, we have completed install RabbitMQ as queue, send and receive JSON data with it. Thank you for reading.

You can see full source code on:

https://github.com/cuongbangoc/rabbit-queue-python

comments powered by Disqus