python之路day11【RabbitMQ、Redis、Mysql】

來源:互聯網
上載者:User

標籤:

大綱

1.RabbitMQ

2.Redis

3.Mysql

 

 

1.RabbitMQ訊息佇列

 

1.1 RabbitMQ簡介

  AMQP,即Advanced Message Queuing Protocol,進階訊息佇列協議,是應用程式層協議的一個開放標準,為面向訊息的中介軟體設計。訊息中介軟體主要用於組件之間的解耦,訊息的寄件者無需知道訊息使用者的存在,反之亦然。
  AMQP的主要特徵是面向訊息、隊列、路由(包括點對點和發布/訂閱)、可靠性、安全。
  RabbitMQ是一個開源的AMQP實現,伺服器端用Erlang語言編寫,支援多種用戶端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支援AJAX。用於在分布式系統中儲存轉寄訊息,在易用性、擴充性、高可用性等方面表現不俗。
  下面將重點介紹RabbitMQ中的一些基礎概念,瞭解了這些概念,是使用好RabbitMQ的基礎。

 

 

1.2 安裝RabbitMQ和python的pika模組

1.2.1 安裝RabbitMQ

(1)安裝erlang平台(RabbitMQ的依賴平台)

1.安裝依賴檔案
yum install ncurses-devel

2.下載源檔案
wget http://www.erlang.org/download/ otp_src_19.1.tar.gz
若失敗,到地址:http://erlang.org/download/去手動下載

3.解壓源檔案壓縮包
tar -xvf otp_src_17.1.tar.gz
(tar 參數含義: bz2格式用j;gz格式用z;c是建立;x是解壓縮;v是詳細資料;f是指定檔案)

4.進入解壓後的目錄
cd otp_src_19.1

5.依次執行以下命令:
./configure -prefix=/opt/erlang 就會開始編譯安裝 會編譯到 /opt/erlang 下
make && make install

6.修改/etc/profile檔案,增加下面的環境變數:
cd /etc/
#set erlang environment
export PATH=$PATH:/opt/erlang/bin
source profile使得檔案生效(用export 查看path中是否有剛剛添加的環境變數)

7.安裝完成後執行erl看是否能開啟eshell,用’halt().’退出,注意:“.”是erlang的結束符

(2)安裝RabbitMQ

wget -c http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.0/rabbitmq-server-3.6.0-1.noarch.rpm

rpm -ivh --nodeps rabbitmq-server-3.6.0-1.noarch.rpm

1.2.2 安裝pika

pip install pika 或者easy_install pika

 

 

1.3 最簡單的發送/接收訊息佇列模型

producer:

 1 #! /usr/bin/env python3 2 # -*- coding:utf-8 -*- 3  4 import pika 5  6 connection = pika.BlockingConnection(pika.ConnectionParameters( 7     ‘localhost‘))   # 相當於建立一個socket串連 8 channel = connection.channel() 9 # 聲明queue10 channel.queue_declare(queue=‘hello‘)11 # RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.12 channel.basic_publish(exchange=‘‘,13                       routing_key=‘hello‘,14                       body=‘你好!‘.encode("utf-8"))15 print(" 發送 ‘你好!‘")16 connection.close()

consumer

 1 #! /usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 import pika 4  5 connection = pika.BlockingConnection(pika.ConnectionParameters( 6     ‘localhost‘)) 7 channel = connection.channel() 8  9 # You may ask why we declare the queue again ? we have already declared it in our previous code.10 # We could avoid that if we were sure that the queue already exists. For example if send.py program11 # was run before. But we‘re not yet sure which program to run first. In such cases it‘s a good12 # practice to repeat declaring the queue in both programs.13 channel.queue_declare(queue=‘hello‘)14 15 16 def callback(ch, method, properties, body):17     print(" 收到: %r" % body.decode("utf-8"))18 19 channel.basic_consume(callback,20                       queue=‘hello‘,21                       no_ack=True)22 print(‘ 等待。。。‘)23 channel.start_consuming()

注意代碼中的英文注釋,特別是為什麼又一次聲明queue。。。

 

 

1.4 輪詢原理

  1.3中如果依次運行兩個consumer,分別記consumer1、consumer2,那麼producer第一次發訊息是consumer1收到,第二次發是consumer2收到,第三次發又是consumer1收到......也就是說,rabbitMQ是依次把訊息發給consumer端。

 

 

1.5 訊息持久化

producer

 1 #! /usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 import pika 4  5 connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))  # 相當於建立一個socket串連 6 channel = connection.channel()  # 定義一個管道 7 # 聲明Queue 8 channel.queue_declare(queue="hello2",durable=True)   # durable=True 是把這個隊列持久化,如果rabbitMQ掛掉,隊列還在;如果 9                                                       # 隊列中的訊息沒有持久化,則訊息會丟失10 channel.basic_publish(exchange="",11                       routing_key="hello2",12                       body="Hi,how are you?",13                       properties=pika.BasicProperties(14                           delivery_mode=2,))      # properties=pika.BasicProperties(delivery_mode=2,) 這是隊列中的訊息持久化15 print("發送了一句話。。。")16 connection.close()

  上述代碼中,第8行只是隊列持久化,如果rabbitMQ掛掉,隊列還在;但如果隊列中的訊息沒有持久化,則訊息會丟失。

 

 

1.6 訊息公平分發

  如果Rabbit只管按順序把訊息發到各個消費者身上,不考慮消費者負載的話,很可能出現,一個機器配置不高的消費者那裡堆積了很多訊息處理不完,同時配置高的消費者卻一直很輕鬆。為解決此問題,可以在各個消費者端,配置perfetch=1,意思就是告訴RabbitMQ在我這個消費者當前訊息還沒處理完的時候就不要再給我發新訊息了。

 

1.7 訊息發布/訂閱(Publish/Subscribe)

1.8 有選擇地接收訊息(exchange type = direct)

 

python之路day11【RabbitMQ、Redis、Mysql】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.