Redis-API支持

一、准备

以python为例,上传相关软件包并解压:

-rw-r--r--  1 root root 16872064 Mar  6  2019 Python-3.6.1.tar.xz
-rw-r--r--  1 root root  1551468 Mar  6  2019 redis-3.2.12.tar.gz
-rw-r--r--  1 root root   124764 Mar  6  2019 redis-py-cluster-unstable.zip
-rw-r--r--  1 root root   111819 Mar  6  2019 redis-py-master.zip

1.1、安装 python3

tar xf Python-3.6.1.tar.xz 
cd Python-3.6.1/
./configure && make && make install

#如果出现以下报错,要安装依赖包:yum install zlib-devel -y
zipimport.ZipImportError: can't decompress data; zlib not available

#查看安装后的python版本
python3 -V					#redis集群至少需要2.8以上的python版本
Python 3.6.1

1.2、安装驱动包

下载地址:https://redis.io/clients

软件包名:redis-py-master.zip

安装过程:

unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install

1.3、安装redis-cluster客户端程序

unzip redis-py-cluster-unstable.zip
cd redis-py-cluster-unstable
python3 setup.py instal

二、redis单实例连接

[root@redis-master ~]# redis-server /data/6379/redis.conf 
[root@redis-master ~]# ps -ef|grep redis
root      73602      1  0 10:12 ?        00:00:00 redis-server 10.0.0.11:6379
root      73606  45343  0 10:12 pts/0    00:00:00 grep --color=auto redis
[root@redis-master ~]# python3
Python 3.6.1 (default, Oct  4 2019, 09:55:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis(host='10.0.0.11', port=6379, db=0,password='123456')                                          
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
b'bar'
>>> 

#--------------------------------------------------------------------------------
#连接不上
1)查看bindip
2)查看ip,端口,用户及密码是否有问题
[root@redis-master ~]# cat /data/6379/redis.conf 
daemonize yes
port 6379
logfile /data/redis/redis.log
dir /data/redis
dbfilename dump.rdb
bind 10.0.0.11  127.0.0.1
requirepass 123456
save 900 1
save 300 10
save 60 10000

三、sentinel集群连接

3.1、启动sentinel集群

[root@redis-master ~]# redis-server /data/6380/redis.conf
[root@redis-master ~]# redis-server /data/6381/redis.conf
[root@redis-master ~]# redis-server /data/6382/redis.conf 
[root@redis-master ~]# redis-sentinel /data/26380/sentinel.conf &
[root@redis-master ~]# ps -ef|grep redis
root      73877      1  0 10:17 ?        00:00:00 redis-server *:6380
root      73881      1  0 10:17 ?        00:00:00 redis-server *:6381
root      73885      1  0 10:17 ?        00:00:00 redis-server *:6382
root      73886  45343  0 10:17 pts/0    00:00:00 redis-sentinel *:26380 [sentinel]
root      73894  45343  0 10:17 pts/0    00:00:00 grep --color=auto redis

3.2、导入redis sentinel包

>>>from redis.sentinel import Sentinel

3.3、指定sentinel地址和端口号

>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1) 

3.4、测试,获取以下主库和从库的信息

>>> sentinel.discover_master('mymaster')
>>> sentinel.discover_slaves('mymaster')

3.5、操作步骤

[root@redis-master ~]# python3
Python 3.6.1 (default, Oct  4 2019, 09:55:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)
>>> sentinel.discover_master('mymaster')
('127.0.0.1', 6380)
>>> sentinel.discover_slaves('mymaster')
[('127.0.0.1', 6381), ('127.0.0.1', 6382)]

3.6、配置读写分离

#写节点
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1,password="123")

#读节点
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1,password="123")

#读写分离测试
>>> master.set('AAA', '123')
True
>>> slave.get('AAA')
b'123'

四、redis cluster集群连接

redis cluster的连接并操作(python2.7.2以上版本才支持redis cluster,我们选择的是3.6)

下载地址:https://github.com/Grokzen/redis-py-cluster

4.1、启动集群

[root@redis-master ~]# redis-server /data/7000/redis.conf 
[root@redis-master ~]# redis-server /data/7001/redis.conf 
[root@redis-master ~]# redis-server /data/7002/redis.conf 
[root@redis-master ~]# redis-server /data/7003/redis.conf 
[root@redis-master ~]# redis-server /data/7004/redis.conf 
[root@redis-master ~]# redis-server /data/7005/redis.conf 
[root@redis-master ~]# ps -ef|grep redis
root      74382      1  0 10:26 ?        00:00:00 redis-server *:7000 [cluster]
root      74386      1  0 10:26 ?        00:00:00 redis-server *:7001 [cluster]
root      74390      1  0 10:26 ?        00:00:00 redis-server *:7002 [cluster]
root      74394      1  0 10:26 ?        00:00:00 redis-server *:7003 [cluster]
root      74398      1  0 10:26 ?        00:00:00 redis-server *:7004 [cluster]
root      74406      1  0 10:26 ?        00:00:00 redis-server *:7005 [cluster]
root      74412  45343  0 10:26 pts/0    00:00:00 grep --color=auto redis

4.2、连接测试

[root@redis-master ~]# python3
Python 3.6.1 (default, Oct  4 2019, 09:55:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{"host":"127.0.0.1", "port":"7000"},{"host":"127.0.0.1", "port": "7001"},{"host":"127.0.0.1", "port": "7002"}]
### 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

五、redis相关概念

5.1、缓存穿透

访问一个不存在的key,缓存不起作用,请求会穿透到DB,流量大时DB会挂掉。

解决方案

采用布隆过滤器,使用一个足够大的bitmap,用于存储可能访问的key,不存在的key直接被过滤;访问key未在DB查询到值,也将空值写进缓存,但可以设置较短过期时间。

5.2、缓存雪崩

大量的key设置了相同的过期时间,导致在缓存在同一时刻全部失效,造成瞬时DB请求量大、压力骤增,引起雪崩。

解决方案:可以给缓存设置过期时间时加上一个随机值时间,使得每个key的过期时间分布开来,不会集中在同一时刻失效。

5.3、缓存击穿

一个存在的key,在缓存过期的一刻,同时有大量的请求,这些请求都会击穿到DB,造成瞬时DB请求量大、压力骤增。

解决方案:在访问key之前,采用SETNX(set if not exists)来设置另一个短期key来锁住当前key的访问,访问结束再删除该短期key。

原文地址:https://www.cnblogs.com/hujinzhong/p/11621534.html