老男孩Python全棧6期——redis
--------------------------
Linux 操做系統 默認的內存管理機制
RSS:
page cache:
anno page:
Linux操做系統以page進行分配內存,page大小默認4K
slab allocator 內存頁的劃分
buddy system :LRU 最近最少使用原則
-----------------------------
redis
在單用戶多併發讀寫,性能高
在多用戶少讀寫時,memcache更優
redis是一個單核的管理機制,生產中通常是,單機多實例的架構。
----------------------------node
redis配置文件:
vim /etc/redis.confpython
是否後臺運行:
daemonize no/yes
默認端口:
port 6379
AOF日誌開關是否打開:
appendonly no/yes
日誌文件位置
logfile /var/log/redis.log
RDB持久化數據文件:
dbfilename dump.rdbgit
daemonize yes
port 6379
dbfilename dump.rdb
dir "/application/data/6379"
logfile /var/log/redis.loggithub
cd /root
vim .bash_profile
PATH 行添加
:/application/redis-3.2.6/srcredis
配置生效
source .bash_profilevim
配置完成以後,能夠直接在命令行調用redis命令安全
例如:
redis-cli
shutdown
redis-server /etc/redis.confruby
---------------------------
安全控制
vim /etc/redis.conf bash
bind 10.0.0.200
requirepass root服務器
關閉redis
redis-cli
shutdown
啓動redis
redis-server /etc/redis.conf
鏈接redis:
設置完成以後須要換種打開方式:
redis-cli -h 10.0.0.200 -a root
-h <hostname>
-p <port>
-a <password>
redis-cli 默認狀況是如下的方式鏈接
redis-cli -h 127.0.0.1 -p 6379
因此須要加上-h -a 選項
--------------------------
在線變動配置:
獲取當前配置
CONFIG GET *
變動運行配置
CONFIG SET loglevel "notice"
例子:修改密碼
CONFIG SET requirepass "123"
CONFIG GET requirepass
在線修改的配置,下次登陸生效,可是重啓以後配置會丟失。
---------------------
import redis
r = redis.StrictRedis(host='10.0.0.200', port=6379, db=0)
-----------------------------
redis持久化功能
RDB:快照,只記錄一個時刻內存數據狀態
配置:
dbfilename dump.rdb
dir "/application/data/6379"
save 900 1
save 300 10
save 60 10000
配置分別表示:
• 900秒(15分鐘)內有1個更改
• 300秒(5分鐘)內有10個更改
• 60秒內有10000個更改
擴展選項:
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
AOF:只追加日誌文件,記錄了redis中全部的修改命令
配置:
appendonly yes
appendfsync everysec
---------------------------------------
數據類型
5中
發佈訂閱
SUBSCRIBE weibo 訂閱一個頻道
PSUBSCRIBE * 訂閱多個頻道
PSUBSCRIBE beijing.* 訂閱全部和北京相關的頻道
PUBLISH weibo hello
ZADD salary 2000 user1
ZADD salary 3000 user2
ZRANGE salary 0 -1 WITHSCORES
MULTI
ZINCRBY salary 1000 user1
ZINCRBY salary -1000 user2
EXEC
Info
Clinet list
Client kill ip:port
config get *
CONFIG RESETSTAT 重置統計
CONFIG GET/SET 動態修改
Dbsize
FLUSHALL 清空全部數據
select 1
FLUSHDB 清空當前庫
MONITOR 監控實時指令
SHUTDOWN 關閉服務器
save將當前數據保存
SLAVEOF host port 主從配置
SLAVEOF NO ONE
SYNC 主從同步
ROLE返回主從角色
CONFIG SET slowlog-log-slower-than 100
CONFIG SET slowlog-max-len 1000
CONFIG GET slow*
SLOWLOG GET
SLOWLOG RESET
------------------------------
主從複製
一、故障轉移 failover
二、分擔負載 blance
搭建主從
準備2個以上的redis實例
一、多配置文件(端口、數據路徑、日誌路徑、pid)
端口:
6380(主)
6381(從)
數據路徑(持久化文件存放位置):
/application/data/6380
/application/data/6381
日誌路徑:
/var/log/redis6380.log
/var/log/redis6381.log
pid文件
/application/data/6380/redis.pid
/application/data/6381/redis.pid
---------------------------
配置過程
一、建立目錄:
mkdir -p /application/data/6380
mkdir -p /application/data/6381
二、配置文件準備
vim /application/data/6380/redis.conf
port 6380
daemonize yes
pidfile /application/data/6380/redis.pid
logfile "/var/log/redis6380.log"
dbfilename dump.rdb
dir /application/data/6380
vim /application/data/6381/redis.conf
port 6381
daemonize yes
pidfile /application/data/6381/redis.pid
logfile "/var/log/redis6381.log"
dbfilename dump.rdb
dir /application/data/6381
三、啓動兩個實例
redis-server /application/data/6380/redis.conf
redis-server /application/data/6381/redis.conf
四、構建主從
redis-cli -p 6381
slaveof 127.0.0.1 6380
五、驗證主從
登陸主庫:
redis-cli -p 6380
set foo bar
登陸主庫:
redis-cli -p 6381
get
六、主從複製狀態查看。
role
info replication
七、主從複製,手工切換
slaveof no one
----------------------
傳統主從的缺陷
一、沒有自動監控機制
二、沒有自動切換的功能
三、對於應用不透明
sentinel 功能
一、自動監控redis全部節點狀態
二、發現主庫故障,自動選主切換
三、自動通知應用端
準備,1主2從結構比較合適。
添加6382節點:
mkdir -p /application/data/6382
vim /application/data/6382/redis.conf
port 6382
daemonize yes
pidfile /application/data/6382/redis.pid
logfile "/var/log/redis6382.log"
dbfilename dump.rdb
dir /application/data/6382
redis-server /application/data/6382/redis.conf
redis-cli -p 6382
slaveof 127.0.0.1 6380
sentinel配置
mkdir -p /application/data/26380
vim /application/data/26380/sentinel.conf
port 26380
dir "/application/data/26380"
sentinel monitor mymaster 127.0.0.1 6380 1
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
啓動
redis-sentinel /application/data/26380/sentinel.conf
+++++++++++++++++Python sentinel+++++++++++++++++
>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([('127.0.0.1', 26380)], socket_timeout=0.1)
>>> sentinel.discover_master('mymaster')
('127.0.0.1', 6379)
>>> sentinel.discover_slaves('mymaster')
[('127.0.0.1', 6380)]
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
>>> master.set('foo', 'bar')
>>> slave.get('foo')
'bar'
++++++++++++++++++++++++++++++++++
redis 分片cluser 部署
一、集羣組件安裝
EPEL源安裝ruby支持
yum install ruby rubygems -y
使用國內源
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
若是沒法使用,可使用aliyun
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources --remove http://rubygems.org/
gem sources -l
gem install redis -v 3.3.3
二、集羣節點準備
規劃:
端口:7000-7005
路徑:
/application/data/7000
/application/data/7001
/application/data/7002
/application/data/7003
/application/data/7004
/application/data/7005
日誌路徑:
/var/log/redis7000.log
/var/log/redis7001.log
/var/log/redis7002.log
/var/log/redis7003.log
/var/log/redis7004.log
/var/log/redis7005.log
pid文件
/application/data/7000/redis.pid
/application/data/7001/redis.pid
/application/data/7002/redis.pid
/application/data/7003/redis.pid
/application/data/7004/redis.pid
/application/data/7005/redis.pid
建立節點目錄:
mkdir -p /application/data/7000
mkdir -p /application/data/7001
mkdir -p /application/data/7002
mkdir -p /application/data/7003
mkdir -p /application/data/7004
mkdir -p /application/data/7005
配置文件添加:
-------------------------
vim /application/data/7000/redis.conf
port 7000
daemonize yes
pidfile /application/data/7000/redis.pid
logfile "/var/log/redis7000.log"
dbfilename dump.rdb
dir /application/data/7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /application/data/7001/redis.conf
port 7001
daemonize yes
pidfile /application/data/7001/redis.pid
logfile "/var/log/redis7001.log"
dbfilename dump.rdb
dir /application/data/7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /application/data/7002/redis.conf
port 7002
daemonize yes
pidfile /application/data/7002/redis.pid
logfile "/var/log/redis7002.log"
dbfilename dump.rdb
dir /application/data/7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /application/data/7003/redis.conf
port 7003
daemonize yes
pidfile /application/data/7003/redis.pid
logfile "/var/log/redis7003.log"
dbfilename dump.rdb
dir /application/data/7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /application/data/7004/redis.conf
port 7004
daemonize yes
pidfile /application/data/7004/redis.pid
logfile "/var/log/redis7004.log"
dbfilename dump.rdb
dir /application/data/7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /application/data/7005/redis.conf
port 7005
daemonize yes
pidfile /application/data/7005/redis.pid
logfile "/var/log/redis7005.log"
dbfilename dump.rdb
dir /application/data/7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
啓動全部節點
redis-server /application/data/7000/redis.conf
redis-server /application/data/7001/redis.conf
redis-server /application/data/7002/redis.conf
redis-server /application/data/7003/redis.conf
redis-server /application/data/7004/redis.conf
redis-server /application/data/7005/redis.conf
ps -ef |grep 700
-------------------------
三、集羣建立
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
---------------------
四、集羣狀態查看
redis-cli -p 7000 cluster nodes | grep master
+++++++++++++++Python 鏈接 redis cluster+++++++++++++++++(1) redis-py並無提供redis-cluster的支持,去github找了一下,有個叫redis-py-cluster的源碼,可是和redis-py不是一個做者,地址爲:https://github.com/Grokzen/redis-py-clusterwatch,star,fork還算能夠。(2) 安裝 Latest stable release from pypi$ pip install redis-py-cluster or from source$ python setup.py install(3) 使用>>> from rediscluster import StrictRedisCluster >>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}] >>> # Note: decode_responses must be set to True when used with python3 >>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) >>> rc.set("foo", "bar") True >>> print(rc.get("foo")) 'bar'++++++++++++++++++++++++++++++++