老男孩Python全栈6期——redis
--------------------------
Linux 操作系统 默认的内存管理机制
RSS:
page cache:
anno page:
Linux操作系统以page进行分配内存,page大小默认4K
slab allocator 内存页的划分
buddy system :LRU 最近最少使用原则
-----------------------------
redis
在单用户多并发读写,性能高
在多用户少读写时,memcache更优
redis是一个单核的管理机制,生产中一般是,单机多实例的架构。
----------------------------
redis配置文件:
vim /etc/redis.conf
是否后台运行:
daemonize no/yes
默认端口:
port 6379
AOF日志开关是否打开:
appendonly no/yes
日志文件位置
logfile /var/log/redis.log
RDB持久化数据文件:
dbfilename dump.rdb
daemonize yes
port 6379
dbfilename dump.rdb
dir "/application/data/6379"
logfile /var/log/redis.log
cd /root
vim .bash_profile
PATH 行添加
:/application/redis-3.2.6/src
配置生效
source .bash_profile
配置完成之后,可以直接在命令行调用redis命令
例如:
redis-cli
shutdown
redis-server /etc/redis.conf
---------------------------
安全控制
vim /etc/redis.conf
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
------------------------------
主从复制
1、故障转移 failover
2、分担负载 blance
搭建主从
准备2个以上的redis实例
1、多配置文件(端口、数据路径、日志路径、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
---------------------------
配置过程
1、创建目录:
mkdir -p /application/data/6380
mkdir -p /application/data/6381
2、配置文件准备
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
3、启动两个实例
redis-server /application/data/6380/redis.conf
redis-server /application/data/6381/redis.conf
4、构建主从
redis-cli -p 6381
slaveof 127.0.0.1 6380
5、验证主从
登录主库:
redis-cli -p 6380
set foo bar
登录主库:
redis-cli -p 6381
get
6、主从复制状态查看。
role
info replication
7、主从复制,手工切换
slaveof no one
----------------------
传统主从的缺陷
1、没有自动监控机制
2、没有自动切换的功能
3、对于应用不透明
sentinel 功能
1、自动监控redis所有节点状态
2、发现主库故障,自动选主切换
3、自动通知应用端
准备,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 部署
1、集群组件安装
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
2、集群节点准备
规划:
端口: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
-------------------------
3、集群创建
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
---------------------
4、集群状态查看
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-cluster
watch,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'
++++++++++++++++++++++++++++++++