redis
关于spring-redis的API
spring-api
要学会看官方文档,在github上看readme都挺重要的
解决单节点数据容量问题-sharding分片
- 可以根据业务拆分
- 按照算法+取模拆分,有弊端如果加机器去模不好计算
- 使用random随机分配节点,适合做消息队列
- 使用一致性哈希算法比较可靠,也可以叫映射算法
千万别整到mysql,redis数据没了可以重新加载,作为缓存时数据时非必要的
reids 链接对server端得成本很高,是对server端造成额
解决方式:
类似于nginx反向代理,增加一个接入层
我们增加一个代理层,我们只需要关注代理层性能就可以了。
代理层里面有了逻辑实现,例如modula,random,kemata,这叫无状态的。这样减轻了客户端的代码。
如果请求压力太大,代理层hold不住怎么办?我们看图的下半部分这个模型。
在代理层前面加一个LVS,LVS做一个主备,主备之间通过keepalived,除了监控两个LVS的健康状态之外,也监控proxy的健康状态。
以上三种模式的弊端是不能做分布式数据库用。一致性哈希增删节点的时候会让一部分数据时间窗不可用。
新增节点会对算法带来挑战,比如rehash等,怎么解决这个问题?
分区
我们引入预分区的概念
分区是将你的数据分发到不同redis实例上的一个过程,每个redis实例只是你所有key的一个子集。文档第一部分将介绍分区概念,第二部分介绍分区的另外一种可选方案。
分区基本概念
有许多分区标准。假如我们有4个Redis实例R0, R1, R2, R3,有一批用户数据user:1, user:2, … ,那么有很多存储方案可以选择。从另一方面说,有很多different systems to map方案可以决定用户映射到哪个Redis实例。
一种最简单的方法就是范围分区,就是将不同范围的对象映射到不同Redis实例。比如说,用户ID从0到10000的都被存储到R0,用户ID从10001到20000被存储到R1,依此类推。
这是一种可行方案并且很多人已经在使用。但是这种方案也有缺点,你需要建一张表存储数据到redis实例的映射关系。这张表需要非常谨慎地维护并且需要为每一类对象建立映射关系,所以redis范围分区通常并不像你想象的那样运行,比另外一种分区方案效率要低很多。
另一种可选的范围分区方案是散列分区,这种方案要求更低,不需要key必须是object_name:的形式,如此简单:
使用散列函数 (如 crc32 )将键名称转换为一个数字。例:键foobar, 使用crc32(foobar)函数将产生散列值93024922。
对转换后的散列值进行取模,以产生一个0到3的数字,以便可以使这个key映射到4个Redis实例当中的一个。93024922 % 4 等于 2, 所以 foobar 会被存储到第2个Redis实例。 R2 注意: 对一个数字进行取模,在大多数编程语言中是使用运算符%
还有很多分区方法,上面只是给出了两个简单示例。有一种比较高级的散列分区方法叫一致性哈希,并且有一些客户端和代理(proxies)已经实现。
胡桃夹子:https://github.com/twitter/twemproxy
Twemproxy是一个专门为了这种nosql数据库设计的一款代理工具软件,这个工具软件最大的特征是可以实现数据的分片处理。所谓的分片指的是根据一定的算法将要保存的数据保存到不同的节点之中。
preids 是php链接redis得操作库,这里我就不说了奥
Twemproxy的安装
## 下载
get clone https://github.com/twitter/twemproxy.git
## 升级(出现失败情况下)
yum update nss
##执行autoreconf -fvi 必须用的两个工具
yam install automake libtool
## autoreconf
autoreconf -fvi
##
./configure --enable-debug=full
make
src/nutcracker -h
Predixy安装*
参考文章地址:https://github.com/joyieldInc/predixy/blob/master/README_CN.md
这个作者写的很好
redis-cluster
很早之前 redis 集群搭建是用 redis-trib 来启动使用的
./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
这个命令在这里用于创建一个新的集群, 选项–replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
如果你觉得没问题的话, 就可以输入 yes , redis-trib 就会将这份配置应用到集群当中
其实有一个更简单的傻瓜操作方式,但是不一定你不会学到相同数量的操作细节
进入该redis目录下
cd /redis-6.2.6/utils/create-cluster
# Settings
BIN_PATH="../../src/"
安装地址
CLUSTER_HOST=127.0.0.1
端口开始
PORT=30000
超时
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
数据存储地址
ADDITIONAL_OPTIONS=""
[root@admin create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
[root@admin create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: faa0b6f6def99893b138e6cda316edc546a4ebfc 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: edec4dce71f15a5d84d4706c9ba49679d1f1ba41 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 0c79e3e0c58a859c8fe71c5e3a2f93570a5f2e36 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: f4dd1c01ca8e3d2876845798fe5695feb4a9f011 127.0.0.1:30004
replicates edec4dce71f15a5d84d4706c9ba49679d1f1ba41
S: df4424e74437cf7e8ebdef45918fc5905d8b33ad 127.0.0.1:30005
replicates 0c79e3e0c58a859c8fe71c5e3a2f93570a5f2e36
S: 6e7f2d132d5021a6d636743784a3fc3aee842cbb 127.0.0.1:30006
replicates faa0b6f6def99893b138e6cda316edc546a4ebfc
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: faa0b6f6def99893b138e6cda316edc546a4ebfc 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: f4dd1c01ca8e3d2876845798fe5695feb4a9f011 127.0.0.1:30004
slots: (0 slots) slave
replicates edec4dce71f15a5d84d4706c9ba49679d1f1ba41
S: df4424e74437cf7e8ebdef45918fc5905d8b33ad 127.0.0.1:30005
slots: (0 slots) slave
replicates 0c79e3e0c58a859c8fe71c5e3a2f93570a5f2e36
M: edec4dce71f15a5d84d4706c9ba49679d1f1ba41 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 6e7f2d132d5021a6d636743784a3fc3aee842cbb 127.0.0.1:30006
slots: (0 slots) slave
replicates faa0b6f6def99893b138e6cda316edc546a4ebfc
M: 0c79e3e0c58a859c8fe71c5e3a2f93570a5f2e36 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@admin create-cluster]# redis-cli -p 30001
127.0.0.1:30001>
127.0.0.1:30001>
127.0.0.1:30001> set k1 a
(error) MOVED 12706 127.0.0.1:30003
[root@admin create-cluster]# redis-cli -c -p 30001
127.0.0.1:30001> set k1 a
-> Redirected to slot [12706] located at 127.0.0.1:30003
OK
127.0.0.1:30003> get k1
"a"
127.0.0.1:30003> watch k1 小技巧:可以用watch来做跳转
OK
127.0.0.1:30003> get k1
"a"
127.0.0.1:30003> watch k2
-> Redirected to slot [449] located at 127.0.0.1:30001
OK
127.0.0.1:30001> get k2
"qwer"
停止所有服务
[root@admin create-cluster]# ./create-cluster stop
清除所有
[root@admin create-cluster]# ./create-cluster clean
查看命令
[root@admin create-cluster]# redis-cli --cluster help
[root@admin create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
用 redis-cli --cluster create 创建分区
[root@admin create-cluster]# redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 00605d601aa2b79e064f66c9c0fe3f42c1df04ca 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: 633434f720419f6f457ee738ed0fdef9e68847af 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: ce216d46bdaad7c5858692d310ed6af74965df37 127.0.0.1:30004
replicates 00605d601aa2b79e064f66c9c0fe3f42c1df04ca
S: a9224fb0481ba32df9cce942eb8592803e765ddd 127.0.0.1:30005
replicates 633434f720419f6f457ee738ed0fdef9e68847af
S: fb9a3917f6297b885548613758bf02ce78d80809 127.0.0.1:30006
replicates 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 00605d601aa2b79e064f66c9c0fe3f42c1df04ca 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: ce216d46bdaad7c5858692d310ed6af74965df37 127.0.0.1:30004
slots: (0 slots) slave
replicates 00605d601aa2b79e064f66c9c0fe3f42c1df04ca
M: 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: fb9a3917f6297b885548613758bf02ce78d80809 127.0.0.1:30006
slots: (0 slots) slave
replicates 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987
S: a9224fb0481ba32df9cce942eb8592803e765ddd 127.0.0.1:30005
slots: (0 slots) slave
replicates 633434f720419f6f457ee738ed0fdef9e68847af
M: 633434f720419f6f457ee738ed0fdef9e68847af 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered
分区
这个时候是博主已经将 30001 2000个槽位分配给了30003,我下面将他还原
[root@admin create-cluster]# redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (00605d60...) -> 0 keys | 3461 slots | 1 slaves.
127.0.0.1:30003 (4ed2ba2b...) -> 0 keys | 7461 slots | 1 slaves.
127.0.0.1:30002 (633434f7...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@admin create-cluster]# redis-cli --cluster reshard 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 00605d601aa2b79e064f66c9c0fe3f42c1df04ca 127.0.0.1:30001
slots:[2000-5460] (3461 slots) master
1 additional replica(s)
S: ce216d46bdaad7c5858692d310ed6af74965df37 127.0.0.1:30004
slots: (0 slots) slave
replicates 00605d601aa2b79e064f66c9c0fe3f42c1df04ca
M: 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987 127.0.0.1:30003
slots:[0-1999],[10923-16383] (7461 slots) master
1 additional replica(s)
S: fb9a3917f6297b885548613758bf02ce78d80809 127.0.0.1:30006
slots: (0 slots) slave
replicates 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987
S: a9224fb0481ba32df9cce942eb8592803e765ddd 127.0.0.1:30005
slots: (0 slots) slave
replicates 633434f720419f6f457ee738ed0fdef9e68847af
M: 633434f720419f6f457ee738ed0fdef9e68847af 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 2000 分配两千
What is the receiving node ID? 00605d601aa2b79e064f66c9c0fe3f42c1df04ca (30001 ID)
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987 用哪个id去分配
Source node #2: done 不在分配了
Do you want to proceed with the proposed reshard plan (yes/no)? yes
重新查看分区
[root@admin create-cluster]# redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (00605d60...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30003 (4ed2ba2b...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (633434f7...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@admin create-cluster]# redis-cli --cluster check 127.0.0.1:30001
127.0.0.1:30001 (00605d60...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30003 (4ed2ba2b...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (633434f7...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 00605d601aa2b79e064f66c9c0fe3f42c1df04ca 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: ce216d46bdaad7c5858692d310ed6af74965df37 127.0.0.1:30004
slots: (0 slots) slave
replicates 00605d601aa2b79e064f66c9c0fe3f42c1df04ca
M: 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: fb9a3917f6297b885548613758bf02ce78d80809 127.0.0.1:30006
slots: (0 slots) slave
replicates 4ed2ba2b0d14d0e66abdf49ee33835db99b8d987
S: a9224fb0481ba32df9cce942eb8592803e765ddd 127.0.0.1:30005
slots: (0 slots) slave
replicates 633434f720419f6f457ee738ed0fdef9e68847af
M: 633434f720419f6f457ee738ed0fdef9e68847af 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.