一. RabbitMQ隊列
1
2
3
4
5
|
#消息中間件 -消息隊列
-
異步 提交的任務不需要實時得到結果或回應
python線程Q 實現了在同一個進程間不同線程間的交互
python線程Q 也可以實現進程間Q的通信
|
a. 安裝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#Centos7 安裝
#注意/etc/hosts文件 ip和主機名對應
wget https:
/
/
github.com
/
rabbitmq
/
rabbitmq
-
server
/
releases
/
download
/
rabbitmq_v3_6_10
/
rabbitmq
-
server
-
3.6
.
10
-
1.el7
.noarch.rpm
yum install epel
-
release
-
y
yum install rabbitmq
-
server
-
3.6
.
10
-
1.el7
.noarch.rpm
rabbitmq
-
plugins enable rabbitmq_management
cp
/
usr
/
share
/
doc
/
rabbitmq
-
server
-
3.6
.
10
/
rabbitmq.config.example
/
etc
/
rabbitmq
/
rabbitmq.config
systemctl restart rabbitmq
-
server
systemctl status rabbitmq
-
server
#創建用戶 授權
rabbitmqctl add_user alex alex3714
rabbitmqctl set_permissions
-
p
/
alex
".*"
".*"
".*"
|
啟動失敗:
[root@openstack ~]# systemctl start rabbitmq-server.service Job for rabbitmq-server.service failed. See ‘systemctl status rabbitmq-server.service’ and ‘journalctl -xn’ for details. [root@openstack ~]# systemctl status rabbitmq-server.service rabbitmq-server.service – RabbitMQ broker Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled) Active: failed (Result: exit-code) since 六 2016-04-30 23:19:21 CST; 8s ago Process: 3593 ExecStop=/usr/lib/rabbitmq/bin/rabbitmqctl stop (code=exited, status=2) Process: 3563 ExecStart=/usr/lib/rabbitmq/bin/rabbitmq-server (code=exited, status=1/FAILURE) Main PID: 3563 (code=exited, status=1/FAILURE) 4月 30 23:19:21 openstack rabbitmqctl[3593]: * epmd reports: node ‘rabbit’ not running at all 4月 30 23:19:21 openstack rabbitmqctl[3593]: no other nodes on openstack 4月 30 23:19:21 openstack rabbitmqctl[3593]: * suggestion: start the node 4月 30 23:19:21 openstack rabbitmqctl[3593]: current node details: 4月 30 23:19:21 openstack rabbitmqctl[3593]: – node name: rabbitmqctl3593@openstack 4月 30 23:19:21 openstack rabbitmqctl[3593]: – home dir: /var/lib/rabbitmq 4月 30 23:19:21 openstack rabbitmqctl[3593]: – cookie hash: DuHinHyRsf96Yx7NcAaAuQ== 4月 30 23:19:21 openstack systemd[1]: rabbitmq-server.service: control process exited, code=exited status=2 4月 30 23:19:21 openstack systemd[1]: Failed to start RabbitMQ broker. 4月 30 23:19:21 openstack systemd[1]: Unit rabbitmq-server.service entered failed state.
解決辦法:
firewall-cmd --permanent --add-port=5672/tcp firewall-cmd --reload setsebool -P nis_enabled 1
systemctl start rabbitmq-server再次啟動:
[root@oldboy_zny rabbitmq]# systemctl start rabbitmq-server
[root@oldboy_zny rabbitmq]# systemctl status rabbitmq-server
rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled)
Active: active (running) since 一 2018-03-05 10:33:48 CST; 8s ago
Process: 22014 ExecStop=/usr/sbin/rabbitmqctl stop (code=exited, status=0/SUCCESS)
Main PID: 22152 (beam)
Status: "Initialized"
CGroup: /system.slice/rabbitmq-server.service
├─22152 /usr/lib64/erlang/erts-5.10.4/bin/beam -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -- -root /usr/lib64/erlang...
├─22441 inet_gethost 4
└─22442 inet_gethost 4
3月 05 10:33:47 oldboy_zny rabbitmq-server[22152]: ## ##
3月 05 10:33:47 oldboy_zny rabbitmq-server[22152]: ########## Logs: /var/log/rabbitmq/rabbit@oldboy_zny.log
3月 05 10:33:47 oldboy_zny rabbitmq-server[22152]: ###### ## /var/log/rabbitmq/rabbit@oldboy_zny-sasl.log
3月 05 10:33:47 oldboy_zny rabbitmq-server[22152]: ##########
3月 05 10:33:47 oldboy_zny rabbitmq-server[22152]: Starting broker...
3月 05 10:33:48 oldboy_zny rabbitmq-server[22152]: systemd unit for activation check: "bin"
3月 05 10:33:48 oldboy_zny rabbitmq-server[22152]: 'systemctl' unavailable, falling back to sleep
3月 05 10:33:48 oldboy_zny systemd[1]: Started RabbitMQ broker.
3月 05 10:33:49 oldboy_zny python[22447]: SELinux is preventing /usr/bin/bash from getattr access on the file .
***** Plugin catchall (100. confidence) suggests **************************...
3月 05 10:33:53 oldboy_zny rabbitmq-server[22152]: completed with 6 plugins.
Hint: Some lines were ellipsized, use -l to show in full.
b. 創建用戶 授權

#遠程連接rabbitmq server的話,需要配置權限 #創建用戶 rabbitmqctl add_user alex alex3714 #同時還要配置權限,允許從外面訪問 rabbitmqctl set_permissions -p / alex ".*" ".*" ".*" set_permissions [-p vhost] {user} {conf} {write} {read} vhost The name of the virtual host to which to grant the user access, defaulting to /. user The name of the user to grant access to the specified virtual host. conf A regular expression matching resource names for which the user is granted configure permissions. write A regular expression matching resource names for which the user is granted write permissions. read A regular expression matching resource names for which the user is granted read permissions.
c. python rabbitMQ module 安裝
1
2
3
4
5
6
7
|
pip install pika
or
easy_install pika
or
源碼
https:
/
/
pypi.python.org
/
pypi
/
pika
|
二. 事例
參考博客:http://www.cnblogs.com/yuanchenqi/articles/8507109.html
1
2
3
4
|
注意: 一般申明隊列(如下代碼)只需要在服務端申明,但客戶端也可以申明,是防止如果服務端沒有啟動,客戶端先啟動后沒有隊列會報錯
此時服務端如果有相同代碼,會檢查,如果有相同隊列就不創建
channel.queue_declare(queue
=
'hello'
)
|
a. 服務端和客戶端一對一
1
2
3
4
5
|
#查看隊列
# rabbitmqctl list_queues
#客戶端再次申明隊列是因為客戶端要清楚去哪里取數據
channel.queue_declare(queue
=
'hello'
)
|

import pika credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.queue_declare(queue='hello') #通過通道生成一個隊列 channel.basic_publish(exchange='', routing_key='hello', #隊列 body='Hello World!') #內容 print(" [x] Sent 'Hello World!'") connection.close()

import pika credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print(ch) #上面channel = connection.channel()對象 print(method) #除了服務端本身的數據,還帶一些參數 print(properties) #屬性 print(body) #byte數據 channel.basic_consume(callback, queue='hello', no_ack=True) print(' Waiting for messages. To exit press CTRL+C') channel.start_consuming()
b. 消息持久化
1. 模擬客戶端中斷 觀察服務端隊列的數據會不會返回(不會)
1
2
3
4
|
#- 開啟一個服務端,兩個客戶端
#- 服務端向隊列中存放一個值,一客戶端從隊列中取到數據,在睡20秒期間中斷,表示出錯,它不會報告給服務端
#- 這時隊列中為零,另一客戶端也不會取到值
# no_ack=True 表示客戶端處理完了不需要向服務端確認消息
|

import pika credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.queue_declare(queue='hello') #通過通道生成一個隊列 channel.basic_publish(exchange='', routing_key='hello', #隊列 body='Hello World!') #內容 print(" [x] Sent 'Hello World!'") connection.close()

import pika import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print("received msg...start process",body) time.sleep(10) print("end process...") channel.basic_consume(callback, queue='hello', no_ack=True) print(' Waiting for messages. To exit press CTRL+C') channel.start_consuming()
2. 模擬客戶端中斷 觀察服務端隊列的數據會不會返回(會)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#1. 生產者端發消息時,加參數 消息持久化
properties
=
pika.BasicProperties(
delivery_mode
=
2
,
# make message persistent
),
#2. 消費者端,消息處理完畢時,發送確認包
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_consume(callback,
#取到消息后,調用callback 函數
queue
=
'task1'
,)
#no_ack=True) #消息處理后,不向rabbit-server確認消息已消費完畢
#- 開啟一個服務端,兩個客戶端
#- 服務端向隊列中存放一個值,一客戶端從隊列中取到數據,在睡20秒期間中斷,表示出錯,它會報給服務端,服務端隊列還有值
#- 這時啟動另一客戶端還可以取到值
|

import pika credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.queue_declare(queue='hello') #通過通道生成一個隊列 channel.basic_publish(exchange='', routing_key='hello', #隊列 properties=pika.BasicProperties( delivery_mode=2, # make message persistent ), body='Hello World!') #內容 print(" [x] Sent 'Hello World!'") connection.close()

import pika import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print("received msg...start process",body) time.sleep(10) print("end process...") ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume(callback, queue='hello', ) print(' Waiting for messages. To exit press CTRL+C') channel.start_consuming()
c. 隊列持久化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#隊列持久化
channel.queue_declare(queue
=
'hello'
,durable
=
True
)
systemctl restart rabbitmq
-
server
#重啟服務發現hello隊列還在,但是消息不在
rabbitmqctl list_queues
#hello
#隊列和消息持久化
channel.queue_declare(queue
=
'hello'
,durable
=
True
)
properties
=
pika.BasicProperties(
delivery_mode
=
2
,
# make message persistent
),
systemctl restart rabbitmq
-
server
#重啟服務發現隊列和消息都還在
rabbitmqctl list_queues
#hello 6
|

import pika credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('192.168.11.106',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.queue_declare(queue='hello',durable=True) #通過通道生成一個隊列 channel.basic_publish(exchange='', routing_key='hello', #隊列 properties=pika.BasicProperties( delivery_mode=2, # make message persistent ), body='Hello World!') #內容 print(" [x] Sent 'Hello World!'") connection.close()
d. fanout 廣播
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#服務端:
-
不需要申明隊列
#客戶端:
-
每個客戶端都需要申明一個隊列,自動設置隊列名稱,收聽廣播,當收聽完后queue刪除
-
把隊列綁定到exchange上
#注意:客戶端先打開,服務端再打開,客戶端會收到消息
#應用:
-
微博粉絲在線,博主發消息,粉絲可以收到
#如果服務端先啟動向exchange發消息,這時客戶端沒有啟動,沒有隊列保存數據(exchange不負責保存數據)
#這時數據會丟,隊列中沒有數據
#exchange只負責轉發
|

import pika import sys import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.exchange_declare(exchange='logs',type='fanout') message = ' '.join(sys.argv[1:]) or "info: Hello World!" channel.basic_publish(exchange='logs', routing_key='', body=message) print(" Send %r" % message) connection.close()

import pika import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() channel.exchange_declare(exchange='logs', type='fanout') queue_obj = channel.queue_declare(exclusive=True) #隨機創建一個隊列對象 exclusive=True會在使用此queue的消費者斷開后,自動將queue刪除 queue_name = queue_obj.method.queue #不指定queue名字,rabbit會隨機分配一個名字, channel.queue_bind(exchange='logs',queue=queue_name) #把queue綁定到exchange print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming()
e. direct 組播
1
2
3
4
5
6
7
8
9
10
|
#客戶端一:
-
python3 receive1.py info
#客戶端二:
-
python3 receive1.py error
#客戶端三:
-
python3 receive1.py warning
#客戶端四:
-
python3 receive1.py warning error info
#服務端:
-
python3 receive1.py warning
|

import pika import sys import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.exchange_declare(exchange='direct_logs',type='direct') severity = sys.argv[1] if len(sys.argv) > 1 else 'info' message = ' '.join(sys.argv[2:]) or 'Hello World!' channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message) print(" Send %r:%r" % (severity, message)) connection.close()

import pika import time import sys credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() channel.exchange_declare(exchange='direct_logs',type='direct') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue severities = sys.argv[1:] if not severities: sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0]) sys.exit(1) for severity in severities: channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming()
f. topic 規則傳播
1
2
3
4
5
6
7
8
9
|
#客戶端一:
-
python3 receive1.py
*
.django
#客戶端二:
-
python3 receive1.py mysql.error
#客戶端三:
-
python3 receive1.py mysql.
*
#服務端:
-
python3 receive1.py
#匹配相應的客戶端
|

import pika import time import sys credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() channel.exchange_declare(exchange='topic_logs',type='topic') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue binding_keys = sys.argv[1:] if not binding_keys: print(sys.argv[1:]) sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0]) sys.exit(1) for binding_key in binding_keys: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming()

import pika import sys import time credentials = pika.PlainCredentials("egon","egon123") #授權的賬號 密碼 connection = pika.BlockingConnection( pika.ConnectionParameters('172.16.42.128',credentials=credentials)) #建立socket channel = connection.channel() #創建rabbitmq協議通道 channel.exchange_declare(exchange='topic_logs',type='topic') routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info' message = ' '.join(sys.argv[2:]) or 'Hello World!' channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message) print(" [x] Sent %r:%r" % (routing_key, message))
以生產者消費者舉例四種模式:
生產者:
# ! /usr/bin/env python # -*- coding: utf-8 -*- # Author : 張寧陽 # date: 2018/3/6 # ######################### 簡答模式生產者 ######################### import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='ssssssssssssss') print(" 已經 發送 'ssssssssssssssssss'") connection.close() ######################基於exchang分發模式的生產者########################### import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.exchange_declare(exchange='logs', exchange_type='fanout') message = "info: Hello World!" channel.basic_publish(exchange='logs', routing_key='', body=message) print(" [x] Sent %r" % message) connection.close() ##########基於exchange關鍵字模式的生產者############## import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.exchange_declare(exchange='direct_logs', exchange_type='direct') message = "info: Hello World!" channel.basic_publish(exchange='direct_logs', routing_key='info',#關鍵字:info、error、warning body=message) print(" [x] Sent %r" % message) connection.close() ##########基於exchange模糊匹配模式的生產者############## import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') message = "info: Hello World!" #routing_key="模糊匹配字符" routing_keys=["old.boy.python ","old.boy"] for key in routing_keys: channel.basic_publish(exchange='topic_logs', routing_key=key, body=message) print(" [x] Sent %r" % message) connection.close()
消費者
# ! /usr/bin/env python # -*- coding: utf-8 -*- # Author : 張寧陽 # date: 2018/3/6 ##########################基於簡單模式的 消費者 ########################## import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print(" 已經 接受 %r" % body) channel.basic_consume(callback, queue='hello', no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming() ##############基於exchange分發模式消費者##################### import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='192.168.20.56',port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.exchange_declare(exchange='logs', exchange_type='fanout') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange='logs', queue=queue_name) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming() ##########################基於exchange的關鍵字模式的 消費者 ########################## import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.20.56', port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() # 生成交換機 channel.exchange_declare(exchange='direct_logs', exchange_type='direct') # 創建的隊列 result = channel.queue_declare(exclusive=True) # 創建的隊列的隨機名稱 queue_name = result.method.queue # 給隊列與direct_logs這個交換機綁定三個關鍵字 severities = ["info", "warning", "error"] for severity in severities: channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming() ##########################基於exchange的模糊匹配模式的 消費者 ########################## import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.20.56', port=5672, credentials=pika.credentials.PlainCredentials( username='alex', password='alex3714' ))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue bind_keys=["old.*","old.#"] for bind_key in bind_keys: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=bind_key) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming()
基於RabbitMQ的RPC
Callback queue 回調隊列
一個客戶端向服務器發送請求,服務器端處理請求后,將其處理結果保存在一個存儲體中。而客戶端為了獲得處理結果,那么客戶在向服務器發送請求時,同時發送一個回調隊列地址reply_to
。
Correlation id 關聯標識
一個客戶端可能會發送多個請求給服務器,當服務器處理完后,客戶端無法辨別在回調隊列中的響應具體和那個請求時對應的。為了處理這種情況,客戶端在發送每個請求時,同時會附帶一個獨有correlation_id
屬性,這樣客戶端在回調隊列中根據correlation_id
字段的值就可以分辨此響應屬於哪個請求。
客戶端發送請求:某個應用將請求信息交給客戶端,然后客戶端發送RPC請求,在發送RPC請求到RPC請求隊列時,客戶端至少發送帶有reply_to以及correlation_id兩個屬性的信息 服務器端工作流: 等待接受客戶端發來RPC請求,當請求出現的時候,服務器從RPC請求隊列中取出請求,然后處理后,將響應發送到reply_to指定的回調隊列中 客戶端接受處理結果: 客戶端等待回調隊列中出現響應,當響應出現時,它會根據響應中correlation_id字段的值,將其返回給對應的應用
服務器端
#!/usr/bin/env python import pika # 建立連接,服務器地址為localhost,可指定ip地址 connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) # 建立會話 channel = connection.channel() # 聲明RPC請求隊列 channel.queue_declare(queue='rpc_queue') # 數據處理方法 def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) # 對RPC請求隊列中的請求進行處理 def on_request(ch, method, props, body): n = int(body) print(" [.] fib(%s)" % n) # 調用數據處理方法 response = fib(n) # 將處理結果(響應)發送到回調隊列 ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id = \ props.correlation_id), body=str(response)) ch.basic_ack(delivery_tag = method.delivery_tag) # 負載均衡,同一時刻發送給該服務器的請求不超過一個 channel.basic_qos(prefetch_count=1) channel.basic_consume(on_request, queue='rpc_queue') print(" [x] Awaiting RPC requests") channel.start_consuming()
客戶端
#!/usr/bin/env python import pika import uuid class FibonacciRpcClient(object): def __init__(self): ”“” 客戶端啟動時,創建回調隊列,會開啟會話用於發送RPC請求以及接受響應 “”“ # 建立連接,指定服務器的ip地址 self.connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) # 建立一個會話,每個channel代表一個會話任務 self.channel = self.connection.channel() # 聲明回調隊列,再次聲明的原因是,服務器和客戶端可能先后開啟,該聲明是冪等的,多次聲明,但只生效一次 result = self.channel.queue_declare(exclusive=True) # 將次隊列指定為當前客戶端的回調隊列 self.callback_queue = result.method.queue # 客戶端訂閱回調隊列,當回調隊列中有響應時,調用`on_response`方法對響應進行處理; self.channel.basic_consume(self.on_response, no_ack=True, queue=self.callback_queue) # 對回調隊列中的響應進行處理的函數 def on_response(self, ch, method, props, body): if self.corr_id == props.correlation_id: self.response = body # 發出RPC請求 def call(self, n): # 初始化 response self.response = None #生成correlation_id self.corr_id = str(uuid.uuid4()) # 發送RPC請求內容到RPC請求隊列`rpc_queue`,同時發送的還有`reply_to`和`correlation_id` self.channel.basic_publish(exchange='', routing_key='rpc_queue', properties=pika.BasicProperties( reply_to = self.callback_queue, correlation_id = self.corr_id, ), body=str(n)) while self.response is None: self.connection.process_data_events() return int(self.response) # 建立客戶端 fibonacci_rpc = FibonacciRpcClient() # 發送RPC請求 print(" [x] Requesting fib(30)") response = fibonacci_rpc.call(30) print(" [.] Got %r" % response)