• redis cluster伪集群搭建及应用


     集群架构:

     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端口的配置文件为

    1. protected-mode yes
    2. port 7001
    3. tcp-backlog 511
    4. timeout 240
    5. tcp-keepalive 300
    6. daemonize yes
    7. #auth
    8. masterauth "kw8LkFpsTmY3"
    9. requirepass "kw8LkFpsTmY3"
    10. # cluster 新设置的notes配置文件会写到数据文件目录下面
    11. cluster-enabled yes
    12. cluster-config-file "nodes.conf"
    13. cluster-node-timeout 15000
    14. supervised no
    15. pidfile "/apply/redisdata/run/redis-7001.pid"
    16. loglevel notice
    17. logfile "/apply/redisdata/log/redis-7001.log"
    18. databases 16
    19. always-show-logo yes
    20. # 分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。就将数据同步到数据文件
    21. #save 900 1
    22. #save 300 10
    23. #save 60 10000
    24. stop-writes-on-bgsave-error no
    25. rdbcompression yes
    26. rdbchecksum yes
    27. #dbfilename "dump.rdb"
    28. dir "/apply/redisdata/data/7001"
    29. maxmemory 4g
    30. maxmemory-policy volatile-lru
    31. replica-serve-stale-data yes
    32. replica-read-only yes
    33. repl-diskless-sync no
    34. repl-diskless-sync-delay 5
    35. repl-disable-tcp-nodelay no
    36. replica-priority 100
    37. lazyfree-lazy-eviction no
    38. lazyfree-lazy-expire no
    39. lazyfree-lazy-server-del no
    40. replica-lazy-flush no
    41. appendonly yes
    42. appendfilename "appendonly.aof"
    43. appendfsync everysec
    44. no-appendfsync-on-rewrite no
    45. auto-aof-rewrite-percentage 50
    46. auto-aof-rewrite-min-size 64mb
    47. aof-load-truncated yes
    48. # aof rdb持久化
    49. aof-use-rdb-preamble no
    50. lua-time-limit 5000
    51. slowlog-log-slower-than 10000
    52. slowlog-max-len 128
    53. latency-monitor-threshold 0
    54. notify-keyspace-events ""
    55. hash-max-ziplist-entries 512
    56. hash-max-ziplist-value 64
    57. list-max-ziplist-size -2
    58. list-compress-depth 0
    59. set-max-intset-entries 512
    60. zset-max-ziplist-entries 128
    61. zset-max-ziplist-value 64
    62. hll-sparse-max-bytes 3000
    63. activerehashing yes
    64. client-output-buffer-limit normal 0 0 0
    65. client-output-buffer-limit replica 256mb 64mb 60
    66. client-output-buffer-limit pubsub 128mb 32mb 120
    67. hz 10
    68. aof-rewrite-incremental-fsync yes
    69. # Generated by CONFIG REWRITE
    70. maxclients 4064

     5、启动redis服务:

    1. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7001/redis.conf
    2. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7002/redis.conf
    3. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7003/redis.conf
    4. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7004/redis.conf
    5. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7005/redis.conf
    6. [root@qhtx-bigdata-trino01 opt]# redis-5.0.8/src/redis-server redis-cluster-config/7006/redis.conf
    7. [root@qhtx-bigdata-trino01 opt]#
    8. [root@qhtx-bigdata-trino01 opt]# ps -ef | grep redis
    9. root 2118 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7005 [cluster]
    10. root 2950 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7006 [cluster]
    11. root 3671 112804 0 19:02 pts/0 00:00:00 grep --color=auto redis
    12. root 125936 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7001 [cluster]
    13. root 126666 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7002 [cluster]
    14. root 129781 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7003 [cluster]
    15. root 130211 1 0 19:02 ? 00:00:00 redis-5.0.8/src/redis-server *:7004 [cluster]

    6、创建集群:

    1. [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
    2. [ERR] Node 127.0.0.1:7001 NOAUTH Authentication required.
    3. [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
    4. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    5. >>> Performing hash slots allocation on 6 nodes...
    6. Master[0] -> Slots 0 - 5460
    7. Master[1] -> Slots 5461 - 10922
    8. Master[2] -> Slots 10923 - 16383
    9. Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
    10. Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
    11. Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
    12. >>> Trying to optimize slaves allocation for anti-affinity
    13. [WARNING] Some slaves are in the same host as their master
    14. M: dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001
    15. slots:[0-5460] (5461 slots) master
    16. M: dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002
    17. slots:[5461-10922] (5462 slots) master
    18. M: f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003
    19. slots:[10923-16383] (5461 slots) master
    20. S: 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004
    21. replicates f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab
    22. S: 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005
    23. replicates dca82dca7308a25b0dd2d2636acf6febd00270ea
    24. S: 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006
    25. replicates dac0e579fe2d5294472110a325ff50dcf95514c8
    26. Can I set the above configuration? (type 'yes' to accept): yes
    27. >>> Nodes configuration updated
    28. >>> Assign a different config epoch to each node
    29. >>> Sending CLUSTER MEET messages to join the cluster
    30. Waiting for the cluster to join
    31. .....
    32. >>> Performing Cluster Check (using node 127.0.0.1:7001)
    33. M: dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001
    34. slots:[0-5460] (5461 slots) master
    35. 1 additional replica(s)
    36. S: 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006
    37. slots: (0 slots) slave
    38. replicates dac0e579fe2d5294472110a325ff50dcf95514c8
    39. M: f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003
    40. slots:[10923-16383] (5461 slots) master
    41. 1 additional replica(s)
    42. S: 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005
    43. slots: (0 slots) slave
    44. replicates dca82dca7308a25b0dd2d2636acf6febd00270ea
    45. M: dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002
    46. slots:[5461-10922] (5462 slots) master
    47. 1 additional replica(s)
    48. S: 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004
    49. slots: (0 slots) slave
    50. replicates f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab
    51. [OK] All nodes agree about slots configuration.
    52. >>> Check for open slots...
    53. >>> Check slots coverage...
    54. [OK] All 16384 slots covered.

    可以通过查看node.conf(文件目录在redis.conf中的配置的dir下)查看集群状态

    1. [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
    2. 86dcc115c58afed5692a9beb93608b84b8bbc040 127.0.0.1:7006@17006 slave dac0e579fe2d5294472110a325ff50dcf95514c8 0 1667905437113 6 connected
    3. f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 127.0.0.1:7003@17003 master - 0 1667905436000 3 connected 10923-16383
    4. 3111819f1f650de2dc45e671dc0c8ec5e92a7c4d 127.0.0.1:7005@17005 slave dca82dca7308a25b0dd2d2636acf6febd00270ea 0 1667905437000 5 connected
    5. dca82dca7308a25b0dd2d2636acf6febd00270ea 127.0.0.1:7001@17001 myself,master - 0 1667905436000 1 connected 0-5460
    6. dac0e579fe2d5294472110a325ff50dcf95514c8 127.0.0.1:7002@17002 master - 0 1667905438119 2 connected 5461-10922
    7. 571ee9333b72ff8312bad92f8d281961e76cf6fa 127.0.0.1:7004@17004 slave f2f7fc17903b5495025ffdd8e6e2f58bfd4358ab 0 1667905439123 4 connected
    8. vars currentEpoch 6 lastVoteEpoch 0
    9. [root@qhtx-bigdata-trino01 7001]#

    应用

     引入redis pom配置:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-redis</artifactId>
    4. <version>1.4.7.RELEASE</version>
    5. </dependency>

    编写工具类: 

    1. package kafka;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.apache.commons.lang.StringUtils;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. import org.springframework.stereotype.Component;
    7. import java.util.Map;
    8. import java.util.concurrent.TimeUnit;
    9. /**
    10. * zhuhao
    11. */
    12. @Component
    13. @Slf4j
    14. public class RedisConfig {
    15. @Autowired
    16. private RedisTemplate redisTemplate;
    17. /**
    18. * 应用前缀key
    19. *
    20. * @param key
    21. * @return
    22. */
    23. public String getRedisKey(String key) {
    24. return key;
    25. }
    26. /**
    27. * 判断key是否存在
    28. * @param key
    29. * @return
    30. */
    31. public Boolean hasKey(String key){
    32. {
    33. try
    34. {
    35. return redisTemplate.hasKey(key);
    36. }
    37. catch (Exception e)
    38. {
    39. log.error(e.getMessage());
    40. return Boolean.FALSE;
    41. }
    42. }
    43. }
    44. /**
    45. * 判断key是否存在
    46. * @param key
    47. * @return
    48. */
    49. public Long getIncreaseSeq(String key) {
    50. Long currentValue = null;
    51. Boolean flag = hasKey(key);
    52. if(flag){
    53. String oldValue = String.valueOf(get(key));
    54. if(StringUtils.isNotEmpty(oldValue)){
    55. redisTemplate.opsForValue().set(key,Long.parseLong(oldValue)+1);
    56. currentValue = Long.valueOf(oldValue)+1;
    57. }
    58. }
    59. return currentValue;
    60. }
    61. /**
    62. * 放入String缓存,默认为1小时
    63. *
    64. * @param key 键
    65. * @param value 值
    66. *
    67. */
    68. public void set(String key, Object value) {
    69. try {
    70. redisTemplate.opsForValue().set(getRedisKey(key), value);
    71. } catch (Exception e) {
    72. log.info("放入String缓存异常,抛出系统错误提示........");
    73. log.error(e.getMessage() + e);
    74. }
    75. }
    76. public void setHash(String key, Map value) {
    77. try {
    78. redisTemplate.opsForHash().putAll(getRedisKey(key), value);
    79. } catch (Exception e) {
    80. log.info("放入String缓存异常,抛出系统错误提示........");
    81. log.error(e.getMessage() + e);
    82. }
    83. }
    84. /**
    85. * 放入String缓存并设置时间
    86. *
    87. * @param key 键
    88. * @param value 值
    89. * @param time 时间
    90. *
    91. */
    92. public void set(String key, Object value, Integer time) {
    93. try {
    94. redisTemplate.opsForValue().set(getRedisKey(key), value, time, TimeUnit.SECONDS);
    95. } catch (Exception e) {
    96. log.info("放入String缓存并设置时间异常,抛出系统错误提示........" + e);
    97. log.error(e.getMessage() + e);
    98. }
    99. }
    100. /**
    101. * 获取缓存
    102. *
    103. * @param key 键
    104. * @return Object
    105. */
    106. public Object get(String key) {
    107. Object object = null;
    108. try {
    109. object = redisTemplate.opsForValue().get(getRedisKey(key));
    110. } catch (Exception e) {
    111. log.info(String.format("获取key=%s, 异常,返回空.......", getRedisKey(key)));
    112. log.error(e.getMessage() + e);
    113. }
    114. return object;
    115. }
    116. public String get(String key, String defaultValue) {
    117. try {
    118. String value = (String) redisTemplate.opsForValue().get(key);
    119. return value != null ? value : defaultValue;
    120. } catch (Exception e) {
    121. log.error(e.getMessage());
    122. }
    123. return defaultValue;
    124. }
    125. /**
    126. * 重置key失效时间
    127. *
    128. * @param key 键
    129. * @param time 失效时间
    130. * @return
    131. */
    132. public void setExpire(String key, Long time) {
    133. try {
    134. if (time > 0) {
    135. redisTemplate.expire(getRedisKey(key), time, TimeUnit.SECONDS);
    136. }
    137. } catch (Exception e) {
    138. log.info(String.format("重置key=%s, 的失效时间为=%s, 失败.....", getRedisKey(key), time));
    139. log.error(e.getMessage() + e);
    140. }
    141. }
    142. /**
    143. * 删除key
    144. *
    145. * @author rqshao
    146. * @param key
    147. * @return
    148. */
    149. public void delete(String key) {
    150. redisTemplate.delete(getRedisKey(key));
    151. }
    152. }

    添加配置信息:

    1. spring:
    2. redis:
    3. cluster:
    4. 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
    5. password: kw8LkFpsTmY3

    启动类: 

    1. @SpringBootApplication
    2. public class ConsumerApplication {
    3. public static void main(String[] args) throws InterruptedException {
    4. ApplicationContext applicationContext = SpringApplication.run(ConsumerApplication.class, args);
    5. RedisConfig redisConfig = applicationContext.getBean(RedisConfig.class);
    6. AtomicInteger ai = new AtomicInteger(0);
    7. while (true) {
    8. redisConfig.set("a", ai.getAndIncrement());
    9. System.out.println(redisConfig.get("a"));
    10. Thread.sleep(1000);
    11. }
    12. }
    13. }

    当前集群状态:

    1. 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master - 0 1667955452640 3 connected 10923-16383
    2. e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 slave 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 0 1667955454648 5 connected
    3. f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667955453000 2 connected 5461-10922
    4. 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667955453643 6 connected
    5. 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 myself,slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667955451000 4 connected
    6. 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 master - 0 1667955453000 1 connected 0-5460

     key为a,插入至 7003

    当停掉7003之后:

    1. [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
    2. e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master - 0 1667955910000 7 connected 10923-16383
    3. 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667955911000 4 connected
    4. 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master,fail - 1667955896024 1667955894720 3 disconnected
    5. 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667955912000 6 connected
    6. f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667955912779 2 connected 5461-10922
    7. 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667955912000 1 connected 0-5460

     

     此时key  a 可以正常的插入,读取

     当停掉7005之后:

    1. [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
    2. e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master,fail - 1667956044524 1667956043221 7 disconnected 10923-16383
    3. 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667956059000 4 connected
    4. 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 master,fail - 1667955896024 1667955894720 3 disconnected
    5. 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667956061000 6 connected
    6. f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667956061296 2 connected 5461-10922
    7. 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667956060000 1 connected 0-5460

     程序会出现异常

    当重新启动7003之后:

    1. [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
    2. e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master,fail - 1667956044524 1667956043221 7 disconnected 10923-16383
    3. 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667956280138 4 connected
    4. 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 slave e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 0 1667956278000 7 connected
    5. 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667956278000 6 connected
    6. f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667956279134 2 connected 5461-10922
    7. 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667956279000 1 connected 0-5460

     程序依然出现异常

     重启7005:

    1. [root@qhtx-bigdata-trino01 7001]# cat nodes.conf
    2. e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 10.226.21.94:7005@17005 master - 0 1667958319053 7 connected 10923-16383
    3. 1da1ae064470785a2898023e1c5d9b789de595b0 10.226.21.94:7004@17004 slave f77869361142453f89d72b12a617f04d0bad3d29 0 1667958317000 4 connected
    4. 210c4d2f936d7b9b5a5bdc929761189e5236d7ba 10.226.21.94:7003@17003 slave e3bd7a72a3372b8cf7a2a2733a7da9deff84b461 0 1667958317627 7 connected
    5. 63b3abab4a130e68e6b7e71ee2efb63d5bc49f36 10.226.21.94:7006@17006 slave 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 0 1667958317000 6 connected
    6. f77869361142453f89d72b12a617f04d0bad3d29 10.226.21.94:7002@17002 master - 0 1667958319000 2 connected 5461-10922
    7. 4bbe235f6ea1b7ede53deb3f6ec0a5328c8f9ac9 10.226.21.94:7001@17001 myself,master - 0 1667958319000 1 connected 0-5460

    则:

     此时程序会正常执行:

     

  • 相关阅读:
    java面试题超详细讲解整理总结分析【java基础】
    【趣学算法】第一章读书笔记
    教你拥有一个自己的QQ机器人!0基础超详细保姆级教学!基于NoneBot2 Windows端搭建QQ机器人
    vue3+elementPlus el-select组件同时支持label和value模糊查询
    代码训练营第50天:leetcode198打家劫舍|leetcode213打家劫舍2|leetcode337打家劫舍3
    Unity 灯光组件Light
    自媒体视频剪辑素材到哪里找?
    git commit --amend 修改最近一次提交的 commit message
    Effective C++条款21:必须返回对象时,别妄想返回其reference
    java计算机毕业设计贵州农产品交易系统源码+mysql数据库+系统+lw文档+部署
  • 原文地址:https://blog.csdn.net/beiduofen2011/article/details/127756659