分享三個實用python腳本--模擬操作rabbitmq隊列

概述

今天主要介紹三個python腳本,來模擬操作 rabbitmq,後面實驗需要~


一、安裝rabbitmq

Rabbitmq基於erlang,得先安裝erlang,erlang的下載地址http://www.erlang.org/downloads

1、安裝erlang

<code>yum install -y lrzsz wget gcc glibc-devel make ncurses-devel openssl-devel autoconf/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

<code>wget http://erlang.org/download/otp_src_21.2.tar.gz
tar -xvf otp_src_21.2.tar.gz
cd otp_src_21.2
./configure --prefix=/usr/local/erlang --with-ssl --enable-threads --enable-smp-support --enable-kernel-poll --enable-hipe --without-javac && make && make install

--設置環境變量
/usr/local/erlang/bin/erl
echo "export PATH=$PATH:/usr/local/erlang/bin" >> /etc/profile
source /etc/profile/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

2、安裝rabbitmq

<code>cd /usr/local/src/
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.14/rabbitmq-server-generic-unix-3.7.14.tar.xz
tar -xvf rabbitmq-server-generic-unix-3.7.14.tar.xz
mv rabbitmq_server-3.7.14 /usr/local/rabbitmq

echo "export PATH=$PATH:/usr/local/rabbitmq/sbin" >> /etc/profile
source /etc/profile/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

3、Rabbitmq服務器的管理

啟動:rabbitmq-server -detached (systemctl rabbitmq-server start)

關閉:rabbitmqctl stop

<code>狀態查看:rabbitmqctl status
啟用插件:rabbitmq-plugins enable rabbitmq_management
添加遠程登錄的用戶:rabbitmqctl add_user  hwb fswl@1234 /<code>

設置用戶為管理員:rabbitmqctl set_user_tags hwb administrator

設置遠程登錄(配置權限、寫權限、讀權限):rabbitmqctl set_permissions -p "/" hwb ".*" ".*" ".*"

分享三個實用python腳本--模擬操作rabbitmq隊列

分享三個實用python腳本--模擬操作rabbitmq隊列



二、安裝pika模塊(離線安裝)

--安裝setuptools

wget https://files.pythonhosted.org/packages/e0/02/2b14188e06ddf61e5b462e216b15d893e8472fca28b1b0c5d9272ad7e87c/setuptools-38.5.2.zip

unzip setuptools-38.5.2.zip

cd setuptools-38.5.2

python setup.py install

--安裝pip

wget https://files.pythonhosted.org/packages/69/81/52b68d0a4de760a2f1979b0931ba7889202f302072cc7a0d614211bc7579/pip-18.0.tar.gz

tar -xvf pip-18.0.tar.gz

cd pip-18.0

python setup.py build && python setup.py install

ln -s /usr/local/bin/pip /usr/bin/pip

--安裝pika

wget https://files.pythonhosted.org/packages/8c/6d/a526ad96ffb8aa0d3ab7e8660eb1c9fc964a02e7624112d70e4b63fb2bb7/pika-1.1.0.tar.gz

tar -xvf pika-1.1.0.tar.gz

cd pika-1.1.0

python setup.py install



三、安裝pika模塊(在線安裝)

yum install epel-release -y

yum install python-pip -y

pip install pika==0.12

分享三個實用python腳本--模擬操作rabbitmq隊列



四、python腳本操作rabbitmq

分享三個實用python腳本--模擬操作rabbitmq隊列

1、生產者程序(rabbitproducer.py)

<code># -*- coding: utf-8 -*-
import pika
credentials = pika.PlainCredentials('hwb', 'fswl@1234')
connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1',5672,'/',credentials))
channel = connection.channel()
queuename="hwb"
channel.queue_declare(queue=queuename)
channel.basic_publish(exchange='', routing_key=queuename, body='hwb body')
print("insert hwb body success")
connection.close()/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

此時在網頁版查看mq隊列情況:可以發現隊列中有一個未被消費的消息

分享三個實用python腳本--模擬操作rabbitmq隊列

2、每秒鐘插入一個觀察隊列情況(queues.py)

<code># -*- coding: utf-8 -*-
import pika
import time
credentials = pika.PlainCredentials('hwb', 'fswl@1234')
connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1',5672,'/',credentials))
channel = connection.channel()
queuename="hwb"
channel.queue_declare(queue=queuename)
i = 1
while True:
channel.basic_publish(exchange='', routing_key=queuename, body='hwb body {0}'.format( i ))
print("insert hwb body success")
time.sleep(1)

i = i + 1/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

此時在網頁版查看mq隊列情況:可以發現隊列中有12個未被消費的消息

分享三個實用python腳本--模擬操作rabbitmq隊列


3、消費者程序(rabbitconsumer.py)

<code># -*- coding: utf-8 -*-
import pika
credentials = pika.PlainCredentials('hwb', 'fswl@1234')
connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1',5672,'/',credentials))
channel = connection.channel()
queuename="hwb"
channel.queue_declare(queue=queuename)
def callback(ch, method, properties, body):
print("Received %r" % body)
channel.basic_consume(callback,queue=queuename,no_ack=True)
print('Consume waiting for messages. To exit press CTRL+C')
channel.start_consuming()/<code>
分享三個實用python腳本--模擬操作rabbitmq隊列

此時在網頁版查看mq隊列情況:

分享三個實用python腳本--模擬操作rabbitmq隊列



覺得有用的朋友多幫忙轉發哦!後面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注下~


分享三個實用python腳本--模擬操作rabbitmq隊列


分享到:


相關文章: