以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
tar xf Python-3.6.1.tar.xz cd Python-3.6.1/./configure && make && make install#如果出现以下报错,要安装依赖包:yum install zlib-devel -yzipimport.ZipImportError: can‘t decompress data; zlib not available#查看安装后的python版本python3 -V #redis集群至少需要2.8以上的python版本Python 3.6.1
软件包名:redis-py-master.zip
安装过程:
unzip redis-py-master.zipcd redis-py-masterpython3 setup.py install
unzip redis-py-cluster-unstable.zipcd redis-py-cluster-unstablepython3 setup.py instal
[root@redis-master ~]# redis-server /data/6379/redis.conf [root@redis-master ~]# ps -ef|grep redisroot 73602 1 0 10:12 ? 00:00:00 redis-server 10.0.0.11:6379root 73606 45343 0 10:12 pts/0 00:00:00 grep --color=auto redis[root@redis-master ~]# python3Python 3.6.1 (default, Oct 4 2019, 09:55:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linuxType "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)查看bindip2)查看ip,端口,用户及密码是否有问题[root@redis-master ~]# cat /data/6379/redis.conf daemonize yesport 6379logfile /data/redis/redis.logdir /data/redisdbfilename dump.rdbbind 10.0.0.11 127.0.0.1requirepass 123456save 900 1save 300 10save 60 10000
[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 redisroot 73877 1 0 10:17 ? 00:00:00 redis-server *:6380root 73881 1 0 10:17 ? 00:00:00 redis-server *:6381root 73885 1 0 10:17 ? 00:00:00 redis-server *:6382root 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
>>>from redis.sentinel import Sentinel
>>> sentinel = Sentinel([(‘localhost‘, 26380)], socket_timeout=0.1)
>>> sentinel.discover_master(‘mymaster‘)>>> sentinel.discover_slaves(‘mymaster‘)
[root@redis-master ~]# python3Python 3.6.1 (default, Oct 4 2019, 09:55:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linuxType "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)]
#写节点>>> 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的连接并操作(python2.7.2以上版本才支持redis cluster,我们选择的是3.6)
下载地址:https://github.com/Grokzen/redis-py-cluster
[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 redisroot 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
[root@redis-master ~]# python3Python 3.6.1 (default, Oct 4 2019, 09:55:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linuxType "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
访问一个不存在的key,缓存不起作用,请求会穿透到DB,流量大时DB会挂掉。
解决方案:
采用布隆过滤器,使用一个足够大的bitmap,用于存储可能访问的key,不存在的key直接被过滤;访问key未在DB查询到值,也将空值写进缓存,但可以设置较短过期时间。
大量的key设置了相同的过期时间,导致在缓存在同一时刻全部失效,造成瞬时DB请求量大、压力骤增,引起雪崩。
解决方案:可以给缓存设置过期时间时加上一个随机值时间,使得每个key的过期时间分布开来,不会集中在同一时刻失效。
一个存在的key,在缓存过期的一刻,同时有大量的请求,这些请求都会击穿到DB,造成瞬时DB请求量大、压力骤增。
解决方案:在访问key之前,采用SETNX(set if not exists)来设置另一个短期key来锁住当前key的访问,访问结束再删除该短期key。