网站底部关键词指向,邢台房产网,网站建设费用主要包括那几项,广东深广东深圳网站建设十二. Redis 集群操作配置(超详细配图#xff0c;配截图详细说明) 文章目录 十二. Redis 集群操作配置(超详细配图#xff0c;配截图详细说明)1. 为什么需要集群-高可用性2. 集群概述(及其搭建)3. Redis 集群的使用4. Redis 集群故障恢复5. Redis 集群的 Jedis 开发(使用Java…十二. Redis 集群操作配置(超详细配图配截图详细说明) 文章目录 十二. Redis 集群操作配置(超详细配图配截图详细说明)1. 为什么需要集群-高可用性2. 集群概述(及其搭建)3. Redis 集群的使用4. Redis 集群故障恢复5. Redis 集群的 Jedis 开发(使用Java程序连接 Redis 同时开启集群)6. Redis 集群的优缺点7. 补充8. 最后 1. 为什么需要集群-高可用性
为什么需要集群-高可用性
生产环境的实际需求和问题 容量不够redis 如何进行扩容。并发写操作redis 如何分摊。主从模式薪火相传模式主机宕机会导致 ip 地址发生变化应用程序中配置需要修改对应的主机地址端口等信息。 传统解决方案 代理主机来解决 上图解图 客户端请求先到代理服务器由代理服务器进行请求转发到对应的业务处理器为了高可用代理服务A服务B服务C服务都需要搭建主从结构(至少是一主一从 这样就需求搭建至少 8 台服务器)。这种方案的缺点是成本高维护困难如果是一主多从成本就会更高。 redis3.0 提供解决方案 无中心化集群配置 各个 Redis 服务仍然采用主从结构。各个 Redis 服务是连通的任何一台服务器都可以作为请求入口 。各个 Redis 服务器因为是连通的可以进行请求转发这种方式就无中心化 集群配置可以看到只需要 6 台服务器即可搞定。无中心化集群配置 还会根据 key 值计算 slot 把数据分散到不同的主机从而缓解单个主机的存取压力Redis 推荐使用无中心化集群配置。在实际生成环境各个 Redis 服务器应当部署到不同的机器(防止机器宕机主从复制失效)。
2. 集群概述(及其搭建)
Redis 集群实现了对 Redis 的水平扩容即启动 N 个 Redis 节点将整个数据库分布存储在这个 N 个节点中每个节点存储总数居的 1 / NRedis 集群通过分区(partition) 来提供一定程度的可用性(availability) 即使集群中有一部分节点失效或者无法进行通讯集群也可以继续处理命令请求。
Redis 集群搭建实操演示 redis.conf 配置修改
cluster-enabled yes 打开集群模式
cluster-config-file nodes-6379.conf 设定节点配置文件名
cluster-node-timeout 15000 设定节点失联时间超过该时间毫秒集群自动进行主 从切换vi /rainbowsea/redis6379.conf , 删除不必要的内容 增加 cluster 配置, 文件最后内容,如图
include /rainbowsea/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
masterauth rainbowsea
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000[rootlocalhost rainbowsea]# cp redis6379.conf redis6380.conf
[rootlocalhost rainbowsea]# cp redis6379.conf redis6381.conf
[rootlocalhost rainbowsea]# cp redis6379.conf redis6389.conf
[rootlocalhost rainbowsea]# cp redis6379.conf redis6390.conf
[rootlocalhost rainbowsea]# cp redis6379.conf redis6391.conf
[rootlocalhost rainbowsea]# 使用查找替换修改另外 5 个文件 换指令 :%s/6379/6380其它几个文件以此操作即可, 操作的时候,一定要小心, 最后建议再检查一下 所有的都要加上这个 masterauth rainbowsea 加上 Redis 的密码没有设置密码的则不用配置这个。
所有的都要加上这个 masterauth rainbowsea 加上 Redis 的密码没有设置密码的则不用配置这个。
所有的都要加上这个 masterauth rainbowsea 加上 Redis 的密码没有设置密码的则不用配置这个。
include /rainbowsea/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
masterauth rainbowsea
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000启动 6 个 Redis 服务
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6379.conf
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6380.conf
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6381.conf
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6389.conf
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6390.conf
[rootlocalhost rainbowsea]# redis-server /rainbowsea/redis6391.conf
[rootlocalhost rainbowsea]# ps -aux | grep redis 将六个节点合成一个集群 进入到该路径下后将六个节点合成一个集群的指令:
如下这个是 Redis 没有配置密码的指令
redis-cli --cluster create --cluster-replicas 1 192.168.76.147:6379 192.168.76.147:6380 192.168.76.147:6381 192.168.76.147:6389 192.168.76.147:6390 192.168.76.147:6391如下这个是 Redis 配置了密码的指令
redis-cli --cluster create -a rainbowsea --cluster-replicas 1 192.168.76.147:6379 192.168.76.147:6380 192.168.76.147:6381 192.168.76.147:6389 192.168.76.147:6390 192.168.76.147:6391注意事项和细节
组合之前确保所有(你要使用上的端口的) Redis服务器都是启动的同时在 root 目录下(我这里是 root 配置的) nodes-xxxx.conf 文件都生成正常。此时不可以用 127.0.0.1 需要使用真实的 IP地址(就是你连接 Linux 的地址Linux 当中使用ifconfig 指令查询到的地址)在真实生产环境 IP都是独立的。replicas 1 采用最简单的方式配置集群一台主机一台从机正好三组。搭建加群如果没有成功把 sentinel 进程关闭掉再试一下。分许主从对应关系。 分析主从对应关系如下 集群方式登录
指令: redis-cli -c -p 6379
指令: cluster nodes 命令查看集群信息, 主从的对应关系, 主要看这里我标注的颜色
[rootlocalhost src]# redis-cli -c -p 6379
127.0.0.1:6379 auth rainbowsea
127.0.0.1:6379 cluster nodes注意事项和细节
[rootlocalhost src]# redis-cli -c -p 6379一个集群至少要有三个主节点。选项 --cluster-replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。分配原则 尽量保证主服务器和从服务器各自运行在不同的 IP 地址(机器)防止机器故障导致主从机制失效高可用性得不到保障。
3. Redis 集群的使用
什么是 slots
Redis 集群启动后, 你会看到如下提示 一个 Redis 集群包含了 16384 个插槽(hash slot) 编号从 0-16383 Redis 中的每个键都属于这 16384 个插槽的其中一个。注意这里虽然只有 16384个插槽但是并不是只能插入 16384个键多个不同的键可以插入到同一个插槽的并不是一个插槽一个键的 。集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽其中 CRC16(key) 语句用于计算键 key 的 CRC16的校验和 集群中的每个节点负责处理一部分插槽。举个例子如果一个集群可以有主节点其中 节点 A 负责处理 0号 ~ 5460号 插槽节点 B 负责处理 5461号 ~ 10922号 插槽节点 C 负责处理 10923号 ~ 16383号 插槽 在集群中录入值
在 Redis 每次录入查询键值redis 都会计算出该 key 应该送往的插槽如果不是该客户端对应服务器的插槽redis 会告知前往的 Redis 实例地址和端口。Redis-cli 客户端提供了 -c 参数实现自动重定向。如 redis-cli -c -p 6379 登入后再录入查询键值对可以自动重定向 不在一个 slot 下的键值是不能使用 mget,mset 等多键操作。
192.168.76.147:6381 mset k1 v1 k2 v2 k3 v3可以通过{}来定义组的概念从而使 key 中{}内相同内容的键值对放到一个 slot 中去就解决了上面 mget 分布到不同 slot 而导致失败的原因。
192.168.76.147:6381 mest k1{order} v1 k2{order} v2 k3{order} v3注意你如果对键加上了{}组那么你想要获取到该值的时候也是要加上对应的{}组的才能获取到的。 查询集群中的值
指令: CLUSTER KEYSLOT key 返回 key 对应的 slot 值
192.168.76.147:6381 cluster keyslot k1192.168.76.147:6381 cluster keyslot k2{order}可以看到归属于{}同一组的Redis都是分配到了同一个 slot 插槽数值当中。 指令: CLUSTER COUNTKEYSINSLOT slot 返回 slot 有多少个 key
192.168.76.147:6381 cluster countkeysinslot 12706
(integer) 1
192.168.76.147:6381 cluster countkeysinslot 16025
(integer) 3指令: CLUSTER GETKEYSINSLOT slotcount 返回 count 个 slot 槽中的键
192.168.76.147:6381 cluster getkeysinslot 16025 1
1) k1{order}
192.168.76.147:6381 cluster getkeysinslot 16025 2
1) k1{order}
2) k2{order}
192.168.76.147:6381 cluster getkeysinslot 16025 3 4. Redis 集群故障恢复
如果主节点下线, 从节点会自动升为主节点(注意 15 秒超时, 再观察比较准确)
[rootlocalhost ~]# redis-cli -c -p 6380 这里我们将 6380 主机关闭了。 主节点恢复后主节点回来变成从机 如果所有某一段插槽的主从节点都宕掉了 Redis 服务是否还能继续要根据不同的配置而言。 如果某一段插槽的主从 都宕机了而在 redis.conf 配置文件当中 cluster-require-full-coverage 为 yes 那么整个集群都会被宕掉无法使用。如果某一段插槽的主从 都宕机了而在 redis.conf 配置文件当中 cluster-require-full-coverage 为 no 那么仅仅只是该段插槽的数据不能使用了也无法存储了其他插槽的数据还可以继续使用。redis.conf 文件当中的参数 cluster-require-full-coverage 5. Redis 集群的 Jedis 开发(使用Java程序连接 Redis 同时开启集群)
即使连接的不是主机集群会自动切换主机进行存储主机写从机读 。无中心化主从集群无论从哪台主机写的数据其他主机上都能读到数据。注意需要将 Redis 相关的端口都打开 否则会报错
配置防火墙将所有相关 Redis 的端口都打开。
[rootlocalhost src]# firewall-cmd --add-port6379/tcp --permanent
Warning: ALREADY_ENABLED: 6379:tcp
success
[rootlocalhost src]# firewall-cmd --add-port6380/tcp --permanent
success
[rootlocalhost src]# firewall-cmd --add-port6381/tcp --permanent
success
[rootlocalhost src]# firewall-cmd --add-port6389/tcp --permanent
success
[rootlocalhost src]# firewall-cmd --add-port6390/tcp --permanent
success
[rootlocalhost src]# firewall-cmd --add-port6391/tcp --permanent[rootlocalhost src]# firewall-cmd --reload[rootlocalhost src]# firewall-cmd --list-all在 pom.xml 当中引入 redis.clients 依赖。如下 !-- 引入 jedis 依赖--dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion3.2.0/version/dependency首先测试是否可以连接到 Redis 服务器。
package com.rainbowsea.jedis;import org.junit.Test;
import redis.clients.jedis.Jedis;public class JedisCluster_ {Testpublic void con() {// 使用 ip地址 redis的端口的构造器方法Jedis jedis new Jedis(192.168.76.147, 6379);// 如果Redis 配置了密码则需要进行身份校验jedis.auth(rainbowsea);String ping jedis.ping();System.out.println(连接成功 ping 返回的结果 ping);jedis.close(); // 关闭当前连接注意并没有关闭 Redis}} import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;import java.util.HashSet;
import java.util.Set;public class JedisCluster_ {public static void main(String[] args) {SetHostAndPort set new HashSet();set.add(new HostAndPort(192.168.76.147, 6379));JedisPoolConfig jedisPoolConfig new JedisPoolConfig();// 对连接池进行配置jedisPoolConfig.setMaxTotal(200);jedisPoolConfig.setMaxIdle(32);jedisPoolConfig.setMaxWaitMillis(60 * 1000); // 单位是毫秒jedisPoolConfig.setBlockWhenExhausted(true);jedisPoolConfig.setTestOnBorrow(true);JedisCluster jedisCluster new JedisCluster(set,5000,5000,5,rainbowsea,jedisPoolConfig );jedisCluster.set(address, bj);String address jedisCluster.get(address);System.out.println(address address);jedisCluster.close();}
}6. Redis 集群的优缺点
优点
实现扩容。分摊压力。无中心配置相对简单。
缺点
多键操作是不被支持的。多键的 Redis 事务是不被支持的。 lua 脚本不被支持由于集群方案出现较晚很多公司已经采用了其他的集群方案而其它方案想要迁移至 redis cluster 需要整体迁移而不是逐步过渡复杂度较大。
7. 补充
将 root 目录下的rdb、aof 文件都删除掉 [rootlocalhost ~]# rm -f dump*.rdb8. 最后 “在这个最后的篇章中我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底继续在其他的领域奋斗。感谢你们我们总会在某个时刻再次相遇。”
文章转载自: http://www.morning.kphyl.cn.gov.cn.kphyl.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn