Fanout Exchange——不處理路由鍵。你只需要簡單的將隊列綁定到交換機上。一個發送到交換機的消息都會被轉發到與該交換機綁定的所有隊列上。很像子網廣播,每臺子網內的主機都獲得了一份復制的消息。Fanout交換機轉發消息是最快的。
Direct Exchange——處理路由鍵。需要將一個隊列綁定到交換機上,要求該消息與一個特定的路由鍵完全匹配。這是一個完整的匹配。如果一個隊列綁定到該交換機上要求路由鍵 “dog”,則只有被標記為“dog ”的消息才被轉發,不會轉發dog.puppy ,也不會轉發dog.guard ,只會轉發dog 。
Topic Exchange——將路由鍵和某模式進行匹配。此時隊列需要綁定要一個模式上。符號“#”匹配一個或多個詞,符號“*”匹配不多不少一個詞。因此“audit.#”能夠匹配到“audit.irs.corporate ”,但是“audit.* ” 只會匹配到“audit.irs ”
1.1 理
講解的很詳細
http://hwcrazy.com/34195c9068c811e38a44000d601c5586/be62fc2668c811e3adba000d601c5586/
RabbitMQ提供了四種Exchange:fanout,direct,topic,header
Direct Exchange
任何發送到Direct Exchange的消息都會被轉發到RouteKey中指定的Queue。
1.一般情況可以使用rabbitMQ自帶的Exchange:”"(該Exchange的名字為空字符串,下文稱其為default Exchange)。
2.這種模式下不需要將Exchange進行任何綁定(binding)操作
3.消息傳遞時需要一個“RouteKey”,可以簡單的理解為要發送到的隊列名字。
4.如果vhost中不存在RouteKey中指定的隊列名,則該消息會被拋棄。
2.RabbitMQ
mac 安裝
http://my.oschina.net/u/998693/blog/547873
下載 然后 host添加,然后 運行
2.1 啟動 web插件
沒有啟動 要啟動
./ rabbitmq-plugins enable rabbitmq_management
2.2 啟動 ./rabbitmq-server restart
2.2.1 賬戶設置
一下都執行一遍
這個 并不是 admin 很有可能無法登陸
添加
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl add_user bayern 123456
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl set_admin bayern
刪除
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl delete_user guest
權限
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl set_permissions -p "/" bayern "." "." ".*"
*mac 一些坑*
1.用戶login failed
添加用戶時要這樣操作
rabbitmqctl add_user test test
rabbitmqctl set_user_tags test administrator
rabbitmqctl set_permissions -p devhost dev ".*" ".*" ".*"
http://stackoverflow.com/questions/22850546/cant-access-rabbitmq-web-management-interfac e-after-fresh-install
注:當前用戶 test 只是在 vhos‘/’ 下
(1) 新增一個用戶
rabbitmqctl add_user Username Password
(2) 刪除一個用戶
rabbitmqctl delete_user Username
(3) 修改用戶的密碼
rabbitmqctl change_password Username Newpassword
(4) 查看當前用戶列表
rabbitmqctl list_users
RabbitMQ Demo
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl
add_vhost RabbitMQ.Demo.host/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl
set_permissions -p "RabbitMQ.Demo.host" bayern "." "." ".*"
關于login_failed的一些操作 (登陸賬戶設置)
http://www.cnblogs.com/mingaixin/p/4134920.html
還有**官方**更詳細的介紹
https://www.rabbitmq.com/man/rabbitmqctl.1.man.html
[此處輸入鏈接的描述][2]
2.4 進入 http://127.0.0.1:15672/ web控制臺
pip 安裝 卸載等
http://www.xuebuyuan.com/593678.html
easy_install:
$ easy_install pip
rabbitmq-server 安裝方法
http://blog.csdn.net/i_bruce/article/details/39555417
sudo lsof -n -P| grep :5672 查看 5672 端口
停止 已經運行的 rabbit
sudo rabbitmqctl stop
Unbutu
安裝
方法一
sudo apt-get install rabbitmq-server
sudo pip install pika
http://www.01happy.com/ubuntu-rabbitmq-and-python-practice/
今天再次整理一下,有些忘記了,查看原鏈接就行
安裝以及測試 實現簡單的 收發 hello 這個相對 mac 感覺簡單點啊,沒mac復雜
mac 也可通過安裝 pika 實現同類操作
進入web管理
參考這個
http://blog.csdn.net/i_bruce/article/details/39555417 但是
通過 rabbitmq官網安裝 deb
使用 apt-get 安裝 erlang
這篇文章有些安裝還是比較麻煩的
有時候 plugin 一直無法激活 要在當前 rabbitmq中去關閉 sudo rabbitmqctl stop
然后再enable --->啟動
3.測試
3.1
http://yidao620c.iteye.com/blog/1947338
一些發送接收
pika
3.1.1
python pika的一些一些guide 參考 github docs--examples
https://github.com/pika/pika
http://pika.readthedocs.io/en/0.10.0/examples/using_urlparameters.html
%2f是'/ '的URL編碼
using_urlparameters 的連接參考
例如:
scheme://username:password@host:port/virtual_host?key=value&key=value
The default connection URL connects to the / virtual host as guest
using the guest password on localhost port 5672. Note the forwardslash
in the URL is encoded to %2F::amqp://guest:guest@localhost:5672/%2F
Connect to a host rabbit1 as the user www-data using the password
rabbit_pwd on the virtual host web_messages::amqp://www-data:rabbit_pwd@rabbit1/web_messages
Connecting via SSL is pretty easy too. To connect via SSL for the
previous example, simply change the scheme to amqps. If you do not
specify a port, Pika will use the default SSL port of 5671::amqps://www-data:rabbit_pwd@rabbit1/web_messages
If you're looking to tweak other parameters, such as enabling
heartbeats, simply add the key/value pair as a query string value. The
following builds upon the SSL connection, enabling heartbeats every 30
seconds::amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat=30
3.1.1.1
使用**pika登錄**時 python要無論是localhost還是 遠程端口都是**5672**
**默認連接**:
本地:
默認賬號 是guest 密碼 guest
guest只能本地用
connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost'))
使用賬號
例如
賬號 mactest 密碼 test
本地連接
connection =
pika.BlockingConnection(pika.URLParameters('amqp://mactest:test@localhost:5672/%2F'))
*遠程*
connection =
pika.BlockingConnection(pika.URLParameters('amqp://mactest:test@192.168.1.134:5672/%2F'))
%2F表示是默認的vhost(“/”),如有設置vhost 需要設置為相應的參數
3.2.1
model--1
在沒有建立consumer 和publisher關系時容易出現混亂
1. 建立通訊時。一個主機注冊localhost:5672 receive.py
另一個遠程建立 192....:5672 send py
兩個可以實現通訊,
但是這時候主機如果發起一個 send.py 那么遠程的 send.py就會無效。
沒有建立關系,或者指定vhost的時候就會出現這樣的問題。一個recevie只 能對應一個send
所以可以 其一 在用戶下面為其設定 特有的vhost
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
這里的 是跟host “/”
modle--2
無法連接第二個賬號,log現實 vhost refused to user
添加遠程訪問
http://blog.haohtml.com/archives/15249
仔細看一下。vhost的名字。
vhost不要亂加. 最好從 web端添加 vhost
連接測試
很不錯的
http://yidao620c.iteye.com/blog/1947338
3.2pika demo
3.2.1 consueme 就是 接受者
publish 發布者
遇到的問題
pip install ..
return
_setlocale(category, locale)
http://stackoverflow.com/questions/36394101/pip-install-locale-error-unsupported-locale-setting
queue過多
容易出問題需要重啟
讀取json
http://blog.chinaunix.net/uid-9525959-id-3074355.html
注意json 格式不要錯
time.sleep(location.count('.'))
阻塞,直到location全部讀完。。
注意 路徑問題。rabbit sshcommand 默認的 路徑 是 /root
任何文件操作的 都要加 /**/.. 文件路徑
查看隊列 list
rabbitmqctl list_queues
更多用法及參數,可以執行如下命令查看
rabbitmqctl
ffmpeg http://www.tuicool.com/articles/Ivyie2e
python nohub
nohup python -u test.py > out.log &
本來想測試下nohup的用法,去執行一個python腳本:nohup python test.py > out.log &
結果郁悶啊,怎么都查看不到輸出!
python的輸出又緩沖,導致out.log并不能夠馬上看到輸出。
-u參數,使得python不啟用緩沖。
nohup python -u rabbit_compress.py > rabbit.log &
disconnect 的問題
因為用mq做自動化,執行一個耗時任務,執行完畢才 接受下一個,開始總是 會斷開,很糾結。后來使用
while (True):
所有邏輯
強制執行。重開。算是解決耗時任務帶來的問題吧