- RabbitMQ簡(jiǎn)介
http://www.searchsoa.com.cn/showcontent_43337.htm
Fanout Exchange——不處理路由鍵还绘。你只需要簡(jiǎn)單的將隊(duì)列綁定到交換機(jī)上悴务。一個(gè)發(fā)送到交換機(jī)的消息都會(huì)被轉(zhuǎn)發(fā)到與該交換機(jī)綁定的所有隊(duì)列上摊崭。很像子網(wǎng)廣播笛粘,每臺(tái)子網(wǎng)內(nèi)的主機(jī)都獲得了一份復(fù)制的消息乔妈。Fanout交換機(jī)轉(zhuǎn)發(fā)消息是最快的外莲。
Direct Exchange——處理路由鍵队贱。需要將一個(gè)隊(duì)列綁定到交換機(jī)上欣除,要求該消息與一個(gè)特定的路由鍵完全匹配住拭。這是一個(gè)完整的匹配。如果一個(gè)隊(duì)列綁定到該交換機(jī)上要求路由鍵 “dog”历帚,則只有被標(biāo)記為“dog ”的消息才被轉(zhuǎn)發(fā)滔岳,不會(huì)轉(zhuǎn)發(fā)dog.puppy ,也不會(huì)轉(zhuǎn)發(fā)dog.guard 挽牢,只會(huì)轉(zhuǎn)發(fā)dog 谱煤。
Topic Exchange——將路由鍵和某模式進(jìn)行匹配。此時(shí)隊(duì)列需要綁定要一個(gè)模式上禽拔。符號(hào)“#”匹配一個(gè)或多個(gè)詞刘离,符號(hào)“*”匹配不多不少一個(gè)詞。因此“audit.#”能夠匹配到“audit.irs.corporate ”奏赘,但是“audit.* ” 只會(huì)匹配到“audit.irs ”
1.1 理
講解的很詳細(xì)
http://hwcrazy.com/34195c9068c811e38a44000d601c5586/be62fc2668c811e3adba000d601c5586/
RabbitMQ提供了四種Exchange:fanout,direct,topic,header
Direct Exchange
任何發(fā)送到Direct Exchange的消息都會(huì)被轉(zhuǎn)發(fā)到RouteKey中指定的Queue寥闪。
1.一般情況可以使用rabbitMQ自帶的Exchange:”"(該Exchange的名字為空字符串,下文稱其為default Exchange)磨淌。
2.這種模式下不需要將Exchange進(jìn)行任何綁定(binding)操作
3.消息傳遞時(shí)需要一個(gè)“RouteKey”疲憋,可以簡(jiǎn)單的理解為要發(fā)送到的隊(duì)列名字。
4.如果vhost中不存在RouteKey中指定的隊(duì)列名梁只,則該消息會(huì)被拋棄缚柳。
2.RabbitMQ
mac 安裝
http://my.oschina.net/u/998693/blog/547873
下載 然后 host添加,然后 運(yùn)行
2.1 啟動(dòng) web插件
沒有啟動(dòng) 要啟動(dòng)
./ rabbitmq-plugins enable rabbitmq_management
2.2 啟動(dòng) ./rabbitmq-server restart
2.2.1 賬戶設(shè)置
一下都執(zhí)行一遍
這個(gè) 并不是 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
權(quán)限
/Users/Downloads/rabbitmq_server-3.5.7/sbin/rabbitmqctl set_permissions -p "/" bayern "." "." ".*"
*mac 一些坑*
1.用戶login failed
添加用戶時(shí)要這樣操作
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
注:當(dāng)前用戶 test 只是在 vhos‘/’ 下
(1) 新增一個(gè)用戶
rabbitmqctl add_user Username Password
(2) 刪除一個(gè)用戶
rabbitmqctl delete_user Username
(3) 修改用戶的密碼
rabbitmqctl change_password Username Newpassword
(4) 查看當(dāng)前用戶列表
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 "." "." ".*"
關(guān)于login_failed的一些操作 (登陸賬戶設(shè)置)
http://www.cnblogs.com/mingaixin/p/4134920.html
還有**官方**更詳細(xì)的介紹
https://www.rabbitmq.com/man/rabbitmqctl.1.man.html
[此處輸入鏈接的描述][2]
2.4 進(jìn)入 http://127.0.0.1:15672/ web控制臺(tái)
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 端口
停止 已經(jīng)運(yùn)行的 rabbit
sudo rabbitmqctl stop
Unbutu
安裝
方法一
sudo apt-get install rabbitmq-server
sudo pip install pika
http://www.01happy.com/ubuntu-rabbitmq-and-python-practice/
今天再次整理一下搪锣,有些忘記了秋忙,查看原鏈接就行
安裝以及測(cè)試 實(shí)現(xiàn)簡(jiǎn)單的 收發(fā) hello 這個(gè)相對(duì) mac 感覺簡(jiǎn)單點(diǎn)啊,沒mac復(fù)雜
mac 也可通過安裝 pika 實(shí)現(xiàn)同類操作
進(jìn)入web管理
參考這個(gè)
http://blog.csdn.net/i_bruce/article/details/39555417 但是
通過 rabbitmq官網(wǎng)安裝 deb
使用 apt-get 安裝 erlang
這篇文章有些安裝還是比較麻煩的
有時(shí)候 plugin 一直無法激活 要在當(dāng)前 rabbitmq中去關(guān)閉 sudo rabbitmqctl stop
然后再enable --->啟動(dòng)
3.測(cè)試
3.1
http://yidao620c.iteye.com/blog/1947338
一些發(fā)送接收
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登錄**時(shí) python要無論是localhost還是 遠(yuǎn)程端口都是**5672**
**默認(rèn)連接**:
本地:
默認(rèn)賬號(hào) 是guest 密碼 guest
guest只能本地用
connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost'))
使用賬號(hào)
例如
賬號(hào) mactest 密碼 test
本地連接
connection =
pika.BlockingConnection(pika.URLParameters('amqp://mactest:test@localhost:5672/%2F'))
*遠(yuǎn)程*
connection =
pika.BlockingConnection(pika.URLParameters('amqp://mactest:test@192.168.1.134:5672/%2F'))
%2F表示是默認(rèn)的vhost(“/”),如有設(shè)置vhost 需要設(shè)置為相應(yīng)的參數(shù)
3.2.1
model--1
在沒有建立consumer 和publisher關(guān)系時(shí)容易出現(xiàn)混亂
1. 建立通訊時(shí)构舟。一個(gè)主機(jī)注冊(cè)localhost:5672 receive.py
另一個(gè)遠(yuǎn)程建立 192....:5672 send py
兩個(gè)可以實(shí)現(xiàn)通訊灰追,
但是這時(shí)候主機(jī)如果發(fā)起一個(gè) send.py 那么遠(yuǎn)程的 send.py就會(huì)無效。
沒有建立關(guān)系,或者指定vhost的時(shí)候就會(huì)出現(xiàn)這樣的問題弹澎。一個(gè)recevie只 能對(duì)應(yīng)一個(gè)send
所以可以 其一 在用戶下面為其設(shè)定 特有的vhost
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
這里的 是跟host “/”
modle--2
無法連接第二個(gè)賬號(hào)朴下,log現(xiàn)實(shí) vhost refused to user
添加遠(yuǎn)程訪問
http://blog.haohtml.com/archives/15249
仔細(xì)看一下。vhost的名字苦蒿。
vhost不要亂加. 最好從 web端添加 vhost
連接測(cè)試
很不錯(cuò)的
http://yidao620c.iteye.com/blog/1947338
3.2pika demo
3.2.1 consueme 就是 接受者
publish 發(fā)布者
遇到的問題
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 格式不要錯(cuò)
time.sleep(location.count('.'))
阻塞殴胧,直到location全部讀完。佩迟。
注意 路徑問題团滥。rabbit sshcommand 默認(rèn)的 路徑 是 /root
任何文件操作的 都要加 /**/.. 文件路徑
查看隊(duì)列 list
rabbitmqctl list_queues
更多用法及參數(shù),可以執(zhí)行如下命令查看
rabbitmqctl
ffmpeg http://www.tuicool.com/articles/Ivyie2e
python nohub
nohup python -u test.py > out.log &
本來想測(cè)試下nohup的用法报强,去執(zhí)行一個(gè)python腳本:nohup python test.py > out.log &
結(jié)果郁悶啊灸姊,怎么都查看不到輸出!
python的輸出又緩沖躺涝,導(dǎo)致out.log并不能夠馬上看到輸出厨钻。
-u參數(shù),使得python不啟用緩沖坚嗜。
nohup python -u rabbit_compress.py > rabbit.log &
disconnect 的問題
因?yàn)橛胢q做自動(dòng)化,執(zhí)行一個(gè)耗時(shí)任務(wù)诗充,執(zhí)行完畢才 接受下一個(gè)苍蔬,開始總是 會(huì)斷開,很糾結(jié)蝴蜓。后來使用
while (True):
所有邏輯
強(qiáng)制執(zhí)行碟绑。重開。算是解決耗時(shí)任務(wù)帶來的問題吧