集群架构:
redis 版本:Redis 5.0.8
下载地址为:https://download.redis.io/releases/
安装步骤:
1、安装gcc环境 : 执行 yum install gcc-c++
2、进入文件夹编译
cd /usr/local/redis
make
3、进入src文件夹,执行make install进行Redis安装
make install
4、编写6份redis.conf配置文件,端口号分别为7001,7002,7003,7004,7005,7006,如下7001端口的配置文件为
- protected-mode yes
- port 7001
- tcp-backlog 511
- timeout 240
- tcp-keepalive 300
- daemonize yes
- #auth
- masterauth "kw8LkFpsTmY3"
- requirepass "kw8LkFpsTmY3"
- # cluster 新设置的notes配置文件会写到数据文件目录下面
- cluster-enabled yes
- cluster-config-file "nodes.conf"
- cluster-node-timeout 15000
- supervised no
- pidfile "/apply/redisdata/run/redis-7001.pid"
- loglevel notice
- logfile "/apply/redisdata/log/redis-7001.log"
- databases 16
- always-show-logo yes
- # 分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。就将数据同步到数据文件
- #save 900 1
- #save 300 10
- #save 60 10000
- stop-writes-on-bgsave-error no
- rdbcompression yes
- rdbchecksum yes
- #dbfilename "dump.rdb"
- dir "/apply/redisdata/data/7001"
- maxmemory 4g
- maxmemory-policy volatile-lru
- replica-serve-stale-data yes
- replica-read-only yes
- repl-diskless-sync no
- repl-diskless-sync-delay 5
- repl-disable-tcp-nodelay no
- replica-priority 100
- lazyfree-lazy-eviction no
- lazyfree-lazy-expire no
- lazyfree-lazy-server-del no
- replica-lazy-flush no
- appendonly yes
- appendfilename "appendonly.aof"
- appendfsync everysec
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 50
- auto-aof-rewrite-min-size 64mb
- aof-load-truncated yes
- # aof rdb持久化
- aof-use-rdb-preamble no
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 128
- latency-monitor-threshold 0
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
- list-max-ziplist-size -2
- list-compress-depth 0
- set-max-intset-entries 512
- zset-max-ziplist-entries 128
- zset-max-ziplist-value 64
- hll-sparse-max-bytes 3000
- activerehashing yes
- client-output-buffer-limit normal 0 0 0
- client-output-buffer-limit replica 256mb 64mb 60
- client-output-buffer-limit pubsub 128mb 32mb 120
- hz 10
- aof-rewrite-incremental-fsync yes
- # Generated by CONFIG REWRITE
- maxclients 4064
5、启动redis服务:
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7001/redis.conf
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7002/redis.conf
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7003/redis.conf
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7004/redis.conf
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7005/redis.conf
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7006/redis.conf
- [root@qhtx-bigdata-trino01 opt]#
- [root@qhtx-bigdata-trino01 opt]# ps -ef | grep redis
- root 2118 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7005 [cluster]
- root 2950 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7006 [cluster]
- root 3671 112804 0 19:02 pts/0 00:00:00 grep --color=auto redis
- root 125936 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7001 [cluster]
- root 126666 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7002 [cluster]
- root 129781 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7003 [cluster]
- root 130211 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7004 [cluster]
6、创建集群:
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
- [ERR] Node 127.0.0.1:7001 NOAUTH Authentication required.
- [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-cli -a kw8LkFpsTmY3 --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- >>> 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:7005 to 127.0.0.1:7001
- Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
- Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
- >>> Trying to optimize slaves allocation for anti-affinity
- [WARNING] Some slaves are in the same host as their master
- M: dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001
- slots:[0-5460] (5461 slots) master
- M: dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002
- slots:[5461-10922] (5462 slots) master
- M: f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003
- slots:[10923-16383] (5461 slots) master
- S: 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004
- replicates f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab
- S: 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005
- replicates dca82dca7308a25b0dd2d2636acf6febd00270ea
- S: 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006
- replicates dac0e579fe2d5294472110a325ff50dcf95514c8
- 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:7001)
- M: dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001
- slots:[0-5460] (5461 slots) master
- 1 additional replica(s)
- S: 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006
- slots: (0 slots) slave
- replicates dac0e579fe2d5294472110a325ff50dcf95514c8
- M: f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003
- slots:[10923-16383] (5461 slots) master
- 1 additional replica(s)
- S: 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005
- slots: (0 slots) slave
- replicates dca82dca7308a25b0dd2d2636acf6febd00270ea
- M: dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002
- slots:[5461-10922] (5462 slots) master
- 1 additional replica(s)
- S: 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004
- slots: (0 slots) slave
- replicates f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
可以通过查看node.conf(文件目录在redis.conf中的配置的dir下)查看集群状态
- [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
- 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006@17006 slave dac0e579fe2d5294472110a325ff50dcf95514c8 0 1667905437113 6 connected
- f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003@17003 master - 0 1667905436000 3 connected 10923-16383
- 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005@17005 slave dca82dca7308a25b0dd2d2636acf6febd00270ea 0 1667905437000 5 connected
- dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001@17001 myself,master - 0 1667905436000 1 connected 0-5460
- dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002@17002 master - 0 1667905438119 2 connected 5461-10922
- 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004@17004 slave f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 0 1667905439123 4 connected
- vars currentEpoch 6 lastVoteEpoch 0
- [root@qhtx-bigdata-trino01 7001]#
应用
引入redis pom配置:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-redis</artifactId>
- <version>1.4.7.RELEASE</version>
- </dependency>
编写工具类:
- package kafka;
-
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
-
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
-
- /**
- * zhuhao
- */
- @Component
- @Slf4j
- public class RedisConfig {
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- /**
- * 应用前缀key
- *
- * @param key
- * @return
- */
- public String getRedisKey(String key) {
-
- return key;
- }
- /**
- * 判断key是否存在
- * @param key
- * @return
- */
- public Boolean hasKey(String key){
- {
- try
- {
- return redisTemplate.hasKey(key);
- }
- catch (Exception e)
- {
- log.error(e.getMessage());
- return Boolean.FALSE;
- }
- }
- }
- /**
- * 判断key是否存在
- * @param key
- * @return
- */
- public Long getIncreaseSeq(String key) {
- Long currentValue = null;
- Boolean flag = hasKey(key);
- if(flag){
- String oldValue = String.valueOf(get(key));
- if(StringUtils.isNotEmpty(oldValue)){
- redisTemplate.opsForValue().set(key,Long.parseLong(oldValue)+1);
- currentValue = Long.valueOf(oldValue)+1;
- }
- }
- return currentValue;
- }
- /**
- * 放入String缓存,默认为1小时
- *
- * @param key 键
- * @param value 值
- *
- */
- public void set(String key, Object value) {
-
- try {
- redisTemplate.opsForValue().set(getRedisKey(key), value);
- } catch (Exception e) {
- log.info("放入String缓存异常,抛出系统错误提示........");
- log.error(e.getMessage() + e);
- }
- }
-
- public void setHash(String key, Map
value) { -
- try {
- redisTemplate.opsForHash().putAll(getRedisKey(key), value);
- } catch (Exception e) {
- log.info("放入String缓存异常,抛出系统错误提示........");
- log.error(e.getMessage() + e);
- }
- }
-
- /**
- * 放入String缓存并设置时间
- *
- * @param key 键
- * @param value 值
- * @param time 时间
- *
- */
- public void set(String key, Object value, Integer time) {
-
- try {
- redisTemplate.opsForValue().set(getRedisKey(key), value, time, TimeUnit.SECONDS);
- } catch (Exception e) {
- log.info("放入String缓存并设置时间异常,抛出系统错误提示........" + e);
- log.error(e.getMessage() + e);
- }
- }
-
- /**
- * 获取缓存
- *
- * @param key 键
- * @return Object
- */
- public Object get(String key) {
-
- Object object = null;
- try {
- object = redisTemplate.opsForValue().get(getRedisKey(key));
- } catch (Exception e) {
- log.info(String.format("获取key=%s, 异常,返回空.......", getRedisKey(key)));
- log.error(e.getMessage() + e);
- }
- return object;
- }
- public String get(String key, String defaultValue) {
- try {
- String value = (String) redisTemplate.opsForValue().get(key);
- return value != null ? value : defaultValue;
- } catch (Exception e) {
- log.error(e.getMessage());
- }
- return defaultValue;
- }
- /**
- * 重置key失效时间
- *
- * @param key 键
- * @param time 失效时间
- * @return
- */
- public void setExpire(String key, Long time) {
-
- try {
- if (time > 0) {
- redisTemplate.expire(getRedisKey(key), time, TimeUnit.SECONDS);
- }
- } catch (Exception e) {
- log.info(String.format("重置key=%s, 的失效时间为=%s, 失败.....", getRedisKey(key), time));
- log.error(e.getMessage() + e);
- }
- }
-
- /**
- * 删除key
- *
- * @author rqshao
- * @param key
- * @return
- */
- public void delete(String key) {
- redisTemplate.delete(getRedisKey(key));
- }
-
- }
添加配置信息:
- spring:
- redis:
- cluster:
- nodes: 10.226.21.94:7001,10.226.21.94:7002,10.226.21.94:7003,10.226.21.94:7004,10.226.21.94:7005,10.226.21.94:7006
- password: kw8LkFpsTmY3
启动类:
- @SpringBootApplication
- public class ConsumerApplication {
-
- public static void main(String[] args) throws InterruptedException {
- ApplicationContext applicationContext = SpringApplication.run(ConsumerApplication.class, args);
- RedisConfig redisConfig = applicationContext.getBean(RedisConfig.class);
- AtomicInteger ai = new AtomicInteger(0);
- while (true) {
- redisConfig.set("a", ai.getAndIncrement());
- System.out.println(redisConfig.get("a"));
- Thread.sleep(1000);
- }
- }
- }
当前集群状态:
- 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master - 0 1667955452640 3 connected 10923-16383
- e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 slave 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 0 1667955454648 5 connected
- f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667955453000 2 connected 5461-10922
- 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667955453643 6 connected
- 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 myself,slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667955451000 4 connected
- 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 master - 0 1667955453000 1 connected 0-5460
key为a,插入至 7003
当停掉7003之后:
- [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
- e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master - 0 1667955910000 7 connected 10923-16383
- 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667955911000 4 connected
- 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master,fail - 1667955896024 1667955894720 3 disconnected
- 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667955912000 6 connected
- f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667955912779 2 connected 5461-10922
- 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667955912000 1 connected 0-5460
此时key a 可以正常的插入,读取
当停掉7005之后:
- [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
- e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master,fail - 1667956044524 1667956043221 7 disconnected 10923-16383
- 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667956059000 4 connected
- 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master,fail - 1667955896024 1667955894720 3 disconnected
- 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667956061000 6 connected
- f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667956061296 2 connected 5461-10922
- 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667956060000 1 connected 0-5460
程序会出现异常
当重新启动7003之后:
- [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
- e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master,fail - 1667956044524 1667956043221 7 disconnected 10923-16383
- 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667956280138 4 connected
- 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 slave e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 0 1667956278000 7 connected
- 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667956278000 6 connected
- f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667956279134 2 connected 5461-10922
- 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667956279000 1 connected 0-5460
程序依然出现异常
重启7005:
- [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
- e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master - 0 1667958319053 7 connected 10923-16383
- 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667958317000 4 connected
- 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 slave e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 0 1667958317627 7 connected
- 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667958317000 6 connected
- f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667958319000 2 connected 5461-10922
- 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667958319000 1 connected 0-5460
则:
此时程序会正常执行: