RabbitMQ 實現實現基本通信及訂單處理


------------恢復內容開始------------

  一.基本使用 》》》 依據官方文檔

  (1)生產端

 

 

# python 操作我們rabbitMq 需要的模塊 pika
# pip install pika
import pika

# 建立和RabbitMQ的鏈接
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

# 通過句柄
channel = connection.channel()

# 聲明消息隊列
channel.queue_declare(queue = 'hello')

# body 一個內容體 往隊列中進行發消息
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')

print("[x]發送'Hello World!'")

# 關閉
connection.close()

  (2)消費端

# 取消息
import pika
# 建立和RabbitMQ的鏈接
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))


# 通過句柄
channel = connection.channel()

# 確認我們的queue的是存在的
channel.queue_declare(queue='hello')


# 申明回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

# 從Q 中取消息
channel.basic_consume(queue='hello',
                      auto_ack=True,
                      on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()  # 開始消費

  (3)執行

   窗口一 放信息

 

   窗口二  收消息

 

  二.redis的高級部分

   

  (1)linux 命令行實現安裝和啟動

  #下載

wget http://download.redis.io/releases/redis-5.0.7.tar.gz

 

  # 解壓 

 

tar -zxvf redis-5.0.7.tar.gz

  # 建立軟連接

  

ln -s redis-5.0.7 redis

  # 相關信息
  

cd redis
make&&make install
#在src目錄下可以看到
#redis-server--->redis服務器
#redis-cli---》redis命令行客戶端
#redis-benchmark---》redis性能測試工具
#redis-check-aof--->aof文件修復工具
#redis-check-dump---》rdb文件檢查工具
#redis-sentinel---》sentinel服務器,哨兵
#redis作者對windows維護不好,window自己有安裝包

  (2)三種命令行啟動方式

    1.最簡單的啟動

    

redis-server

    相關的參數命令

ps -ef|grep redis  #查看進程
netstat -antpl|grep redis #查看端口
redis-cli -h ip -p port ping #命令查看

  2.動態參數啟動

#動態參數啟動
redis-server --port 6380 #啟動,監聽6380端口

  3.vim  redis_6379.conf進行配置文件啟動

  (1)配置

daemonize no #是否以守護進程啟動 一般是改為yes 開啟進行守護
pidfile /var/run/redis.pid   #進程號的位置,刪除
port 6379    #端口號
dir "opt/soft/redis/data"  #工作目錄   》》》 事先要進行文件存放的目錄
logfile “6379.log” #日志位置  

  (2)啟動命令

  

# 一般啟動redis服務 redis-sever redis_6379.conf 啟動配置文件即可

# 因為環境沒有配置的原因
./src/redis-server redis_6379.conf

  # 查看進程號

#查看進程
ps -ef |grep redis-server |grep 6379
#查看日志
cd data 
cat 6379.log

  

  ####客戶端連接###

redis-cli -h 127.0.0.1 -p 6379
ping #返回PONG

`    

  (2)應用場景

### 1.4 Redis典型使用場景

緩存系統:

計數器:網站訪問量,轉發量,評論數

消息隊列:發布訂閱,阻塞隊列實現

排行榜:有序集合

社交網絡:很多特性跟社交網絡匹配,粉絲數,關注數

實時系統:垃圾郵件處理系統,布隆過濾器

  三. 一主多從

  查詢進行id

  

  ps -ef |grep redis-server

  配置:文件

  復制一份配置文件

  cp redis_6379.conf redis_6378.conf

daemonize yes
bind 0.0.0.0  # 127.0.0.1 只能連接本地 這樣既可以進行外部redis連接
pidfile /var/run/redis.pid
port 6378  # 端口號
dir "/root/data"  # 數據存儲文件
logfile "6378.log"  # 日志文件

     # 一主多重 切換redis 服務器

   cd redis

  ./src/redis-cli -p 6378

  (1)方式一:slave 執行主從命令

  

6378是從,6379是主

在6378上執行

slaveof 127.0.0.1 6379 #異步
slaveof no one #取消復制,不會把之前的數據清除

  ./src/redis-cli -p 6379 寫數據

 # 客戶端主是6379 負責寫數據  從客戶端6378負責讀取數據

(2)文件的配置方式二:

  # 在redis 服務端文件vim 進行配置

  

slaveof ip port #配置從節點ip和端口
slave-read-only yes #從節點只讀,因為可讀可寫,數據會亂

  配置  :從連接主

daemonize yes
bind 0.0.0.0
pidfile /var/run/redis.pid
port 6378
dir "/root/data"
logfile "6378.log"
slaveof 127.0.0.1 6379
slave-read-only yes

 (3)解決主從復制  萬一主掛了的問題  》》》 利用哨兵 高可用的方法進行 解決:起一個sentinel 進行 進行監聽 主服務器的情況 

  

port 26379
daemonize yes
dir /data
protected-mode no
bind 0.0.0.0
logfile "redis_sentinel.log"
sentinel monitor mymaster 10.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

  啟動哨兵

  redis-sentinel sentinel.conf

 

python鏈接

 

import redis
from redis.sentinel import Sentinel

# 連接哨兵服務器(主機名也可以用域名)
# 10.0.0.101:26379
sentinel = Sentinel([('10.0.0.101', 26379),
                     ('10.0.0.101', 26378),
                     ('10.0.0.101', 26377)
             ],
                    socket_timeout=5)


print(sentinel)
# 獲取主服務器地址
master = sentinel.discover_master('mymaster')
print(master)




# 獲取從服務器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)




# 獲取主服務器進行寫入
# master = sentinel.master_for('mymaster', socket_timeout=0.5)
# w_ret = master.set('foo', 'bar')
#
#
#
#
# slave = sentinel.slave_for('mymaster', socket_timeout=0.5)
# r_ret = slave.get('foo')
# print(r_ret)

 


  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM