当前位置:   article > 正文

SpringBoot整合redis集群实现读写分离(哨兵模式)_springboot redis 哨兵

springboot redis 哨兵

1 首先要在Linux虚拟机上安装redis

  1. # 安装包存放目录
  2. cd /usr/local/redis
  3. # 下载最新稳定版
  4. wget https://download.redis.io/releases/redis-6.2.6.tar.gz
  5. # 解压
  6. tar -zxvf redis-6.2.6.tar.gz
  7. # 进入解压后的目录
  8. cd /usr/local/redis/redis-6.2.6/
  9. # 编译
  10. make
  11. # 执行 "make install" 默认会安装到 /usr/local/bin,可通过PREFIX指定安装路径
  12. make install PREFIX=/usr/local/redis
  13. # 测试是否安装成功,执行下面命令
  14. /usr/local/redis/bin/redis-server
  • 下载安装好的文件

在这里插入图片描述

  • 测试启动,看是否能够正常运行

在这里插入图片描述

2 主从及哨兵配置

简要过程

三个redis-server服务都运行在我们虚拟机上,我这里的设置的IP地址都为192.168.159.100(根据自己的IP地址定义),端口分别为6380、6381、6382,以6380作为主节点,6381、6382作为从节点

1)主从复制(master&slave)
  1. 配置redis.conf运行文件

在这里插入图片描述

  • 先创建一个 redis-cluster 文件夹

在这里插入图片描述

  • 拷贝三份redis.conf文件到 redis-cluster 文件夹目录下

在这里插入图片描述

在这里插入图片描述

配置文件可在解压后的源码文件根目录中找到,这里以从节点 6381 配置文件为例,其余两个配置文件几乎一致。首先将配置文件redis.conf拷贝到/usr/local/redis/redis-cluster(redis-cluster文件夹需要手动创建),拷贝三份,然后进行下面的修改

  1. # (1)设置允许外部ip访问,需要注释掉bind配置,并关掉保护模式
  2. # bind 127.0.0.1 -::1
  3. protected-mode no
  4. # (2)修改端口号
  5. port 6381
  6. # (3)修改为以守护进程模式后台运行
  7. daemonize yes
  8. # (4)修改pid文件名,以守护进程运行的时候,会产生pid文件,默认位置为 /run/redis.pid
  9. # 因为这里在同一台机器上运行多个实例,所以需要指定
  10. pidfile /usr/local/redis/redis-cluster/redis_6381.pid
  11. # (5)修改日志文件位置
  12. logfile /usr/local/redis/redis-cluster/redis_6381.log
  13. # (6)修改rdb快照文件位置
  14. dir /usr/local/redis/redis-cluster
  15. dbfilename dump_6381.rdb
  16. # (7)修改主节点地址,在部分旧版本中是slaveof命令,主节点6380配置文件中不要加这一行
  17. replicaof 192.168.159.100 6380
  18. # (8)aof可按需要开启,这里我们使用.rdb文件的存储方式,以下不进行配置
  19. appendonly yes
  20. appendfilename appendonly_6381.aof

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在上面的配置中,63816382.conf一致,改一下其中的端口及地址就可以了,其中,6380作为主节点,没有第(7)步骤。建议三服务运行在不同的文件夹下,方便查看日志以及数据的输出。为了省去切换文件目录的时间,都放在一个文件夹下了

  1. 配置完成后,根据配置文件分别启动三个redis-server服务
  1. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-server ./redis-6380.conf
  2. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-server ./redis-6381.conf
  3. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-server ./redis-6382.conf

在这里插入图片描述

  • 启动成功后,产生的持久化、日志文件如下图

在这里插入图片描述

  1. 进入主节点,查看实例主从状况
  1. # 运行redis客户端 -p 代表进入的是那个端口
  2. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-cli -p 6380
  3. # 查看主从信息
  4. 127.0.0.1:6380> info replication
  5. # Replication
  6. role:master
  7. connected_slaves:2
  8. slave0:ip=192.168.159.100,port=6381,state=online,offset=490,lag=0
  9. slave1:ip=192.168.159.100,port=6382,state=online,offset=490,lag=0
  10. master_failover_state:no-failover
  11. master_replid:efa9c1b74416340cf0a5cc2b02272fedd6344570
  12. master_replid2:0000000000000000000000000000000000000000
  13. master_repl_offset:490
  14. second_repl_offset:-1
  15. repl_backlog_active:1
  16. repl_backlog_size:1048576
  17. repl_backlog_first_byte_offset:1
  18. repl_backlog_histlen:490

在这里插入图片描述

  • 测试主从复制

在这里插入图片描述

2)哨兵配置(Sentinel)

简要过程

需要启动三个redis-sentinel服务,分别运行于26380、26381、26382三个端口,同样也需要配置.conf文件运行服务

  1. 首先需要在/usr/local/redis/redis-6.2.6目录下,拷贝三份sentinel.conf文件到redis-cluster文件夹下
  1. # 拷贝文件sentinel.conf
  2. [root@vinjcent redis-6.2.6]# cp sentinel.conf ../redis-cluster/sentinel-26380.conf
  3. [root@vinjcent redis-6.2.6]# cp sentinel.conf ../redis-cluster/sentinel-26381.conf
  4. [root@vinjcent redis-6.2.6]# cp sentinel.conf ../redis-cluster/sentinel-26382.conf

在这里插入图片描述

  1. 修改所拷贝的配置文件,以sentinel-26380.conf为例,配置信息如下,其余两个配置文件基本上一致,改一下端口以及pidfile、logfile即可
  1. # 配置端口
  2. port 26380
  3. daemonize yes
  4. pidfile /usr/local/redis/redis-cluster/sentinel-26380.pid
  5. logfile /usr/local/redis/redis-cluster/sentinel-26380.log
  6. # 监控192.168.159.100:6380的主节点,实例取名为mymaster,当有两个哨兵认为该服务下线后,自动进行故障转移
  7. # 后面的数字1,代表主机宕机了后,slave投票看让谁接替成为主机,票数最多的,就会成为主机,默认是2
  8. sentinel monitor mymaster 192.168.159.100 6380 1
  9. # 设置主节点多长时间没有响应就代表挂了,默认是30s
  10. sentinel down-after-milliseconds mymaster 30000
  11. # 故障转移的时间上限,默认是三分钟
  12. sentinel failover-timeout mymaster 180000
  13. # 此配置值在发生故障时,最多可以有几个slave同时对新的master进行同步,这个数字越小完成故障处理的时间越短
  14. sentinel parallel-syncs mymaster 1

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. 分别启动三个哨兵服务
  1. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-sentinel sentinel-26380.conf
  2. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-sentinel sentinel-26381.conf
  3. [root@vinjcent redis-cluster]# /usr/local/redis/bin/redis-sentinel sentinel-26382.conf

在这里插入图片描述

在这里插入图片描述

  • 随意连接一个哨兵,查看哨兵监控信息
[root@vinjcent redis-cluster]# ../bin/redis-cli -p 26381

在这里插入图片描述

  • 查看6380节点的哨兵日志
[root@vinjcent redis-cluster]# tail -200f sentinel-26380.log
  • 1

在这里插入图片描述

  1. 测试哨兵模式
  • 关闭主节点6380,再看哨兵日志

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 从上面的日志文件中,我们可以看到哨兵投票选举master以及切换主节点的大概过程,这时候,主节点已经切换到6381节点了

  • 这时候再重新启动6380节点,也就是之前的主节点,这个节点会被哨兵自动加入到集群中作为从节点,sentinel会打印如下日志

在这里插入图片描述

在这里插入图片描述

可以看到6380这个节点由主节点转换成了从节点

在整合springboot之前,首先要开放redis集群的防火墙端口,不然连接不上我们的redis服务

  1. # 开放端口
  2. firewall-cmd --zone=public --add-port=6380/tcp --permanent
  3. firewall-cmd --zone=public --add-port=6381/tcp --permanent
  4. firewall-cmd --zone=public --add-port=6382/tcp --permanent
  5. firewall-cmd --zone=public --add-port=26380/tcp --permanent
  6. firewall-cmd --zone=public --add-port=26381/tcp --permanent
  7. firewall-cmd --zone=public --add-port=26382/tcp --permanent
  8. # 重启防火墙
  9. systemctl restart firewalld.service
  10. # 查看端口
  11. firewall-cmd --list-ports

3 springboot配置redis集群及读写分离

  1. 创建一个springboot项目

  2. 导入依赖

    • pom.xml
  1. <!--redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!--连接池依赖-->
  7. <dependency>
  8. <groupId>org.apache.commons</groupId>
  9. <artifactId>commons-pool2</artifactId>
  10. </dependency>
  11. <!--web-->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <!--lombok-->
  17. <dependency>
  18. <groupId>org.projectlombok</groupId>
  19. <artifactId>lombok</artifactId>
  20. <version>1.18.24</version>
  21. </dependency>
  1. 配置文件
    • application.yml
  1. server:
  2. port: 3035
  3. spring:
  4. redis:
  5. # redis哨兵配置
  6. sentinel:
  7. # 主节点名称
  8. master: mymaster
  9. nodes:
  10. - 192.168.159.100:26380
  11. - 192.168.159.100:26381
  12. - 192.168.159.100:26382
  13. # # 集群的部署方式
  14. # cluster:
  15. # nodes:
  16. # - 192.168.158.100:6380
  17. # - 192.168.158.100:6381
  18. # - 192.168.158.100:6382
  19. # # #最大重定向次数(由于集群中数据存储在多个节点,所以在访问数据时需要通过转发进行数据定位)
  20. # max-redirects: 2
  21. # lettuce:
  22. # pool:
  23. # max-idle: 10 # 连接池中的最大空闲连接
  24. # max-wait: 500 # 连接池最大阻塞等待时间(使用负值表示没有限制)
  25. # max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
  26. # min-idle: 0 # 连接池中的最小空闲连接
  27. # 服务应用名
  28. application:
  29. name: redis-cluster
  30. logging:
  31. pattern:
  32. console: '%date{yyyy-MM-dd HH:mm:ss.SSS} | %highlight(%5level) [%green(%16.16thread)] %clr(%-50.50logger{49}){cyan} %4line -| %highlight(%msg%n)'
  33. level:
  34. root: info
  35. io.lettuce.core: debug
  36. org.springframework.data.redis: debug
  1. 配置读写分离以及json序列化
  1. package com.vinjcent.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
  6. import io.lettuce.core.ReadFrom;
  7. import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.Primary;
  11. import org.springframework.data.redis.connection.RedisConnectionFactory;
  12. import org.springframework.data.redis.connection.RedisSentinelConfiguration;
  13. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  14. import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
  15. import org.springframework.data.redis.core.RedisTemplate;
  16. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  17. import org.springframework.data.redis.serializer.StringRedisSerializer;
  18. import java.text.SimpleDateFormat;
  19. import java.util.HashSet;
  20. @Configuration
  21. public class RedisConfiguration {
  22. /**
  23. *
  24. * 配置redis序列化json
  25. * @param redisConnectionFactory
  26. * @return
  27. */
  28. @Bean
  29. @Primary //若有相同类型的Bean时,优先使用此注解标注的Bean
  30. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  31. // 为了开发方便,一般直接使用<String, Object>
  32. RedisTemplate<String, Object> template = new RedisTemplate<>();
  33. template.setConnectionFactory(redisConnectionFactory);
  34. // 配置具体的序列化方式
  35. // JSON解析任意对象
  36. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  37. ObjectMapper om = new ObjectMapper();
  38. // 指定要序列化的域,field,getset,以及修饰符范围,ANY是都有包括private和public
  39. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  40. // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
  41. om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
  42. // 设置日期格式
  43. om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  44. jackson2JsonRedisSerializer.setObjectMapper(om);
  45. // String的序列化
  46. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  47. //key采用String的序列化
  48. template.setKeySerializer(stringRedisSerializer);
  49. //hash的key也采用String的序列化
  50. template.setHashKeySerializer(stringRedisSerializer);
  51. //value的序列化方式采用jackson
  52. template.setValueSerializer(jackson2JsonRedisSerializer);
  53. //hash的value序列化方式采用jackson
  54. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  55. //设置所有配置
  56. template.afterPropertiesSet();
  57. return template;
  58. }
  59. /**
  60. * 配置读写分离
  61. * @param redisProperties
  62. * @return
  63. */
  64. @Bean
  65. public RedisConnectionFactory lettuceConnectionFactory(RedisProperties redisProperties) {
  66. // 配置哨兵节点以及主节点
  67. RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(
  68. redisProperties.getSentinel().getMaster(), new HashSet<>(redisProperties.getSentinel().getNodes())
  69. );
  70. // 配置读写分离
  71. LettucePoolingClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
  72. // 读写分离,这里的ReadFrom是配置Redis的读取策略,是一个枚举,包括下面选择
  73. // MASTER 仅读取主节点
  74. // MASTER_PREFERRED 优先读取主节点,如果主节点不可用,则读取从节点
  75. // REPLICA_PREFERRED 优先读取从节点,如果从节点不可用,则读取主节点
  76. // REPLICA 仅读取从节点
  77. // NEAREST 从最近节点读取
  78. // ANY 从任意一个从节点读取
  79. .readFrom(ReadFrom.REPLICA_PREFERRED)
  80. .build();
  81. return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration);
  82. }
  83. }
  1. 编写一个测试的Bean
  1. package com.vinjcent.serivce;
  2. import com.vinjcent.utils.RedisUtils;
  3. import lombok.RequiredArgsConstructor;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.ApplicationArguments;
  7. import org.springframework.boot.ApplicationRunner;
  8. import org.springframework.stereotype.Component;
  9. import java.util.concurrent.TimeUnit;
  10. @SuppressWarnings("all")
  11. @Slf4j
  12. @RequiredArgsConstructor
  13. @Component
  14. public class RedisInit implements ApplicationRunner {
  15. @Autowired
  16. private RedisUtils redisUtils;
  17. @Override
  18. public void run(ApplicationArguments args) throws Exception {
  19. for (int i = 0; i < 300; i++) {
  20. try {
  21. redisUtils.set("k" + i, "v" + i);
  22. log.info("set value success: {}", i);
  23. Object val = redisUtils.get("k" + i);
  24. log.info("get value success: {}", val);
  25. TimeUnit.SECONDS.sleep(1);
  26. } catch (Exception e) {
  27. log.error("error: {}", e.getMessage());
  28. }
  29. }
  30. log.info("finished...");
  31. }
  32. }
  1. 封装redis工具类《RedisTemplate序列化&RedisUtils工具类
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.util.CollectionUtils;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.concurrent.TimeUnit;
  9. @SuppressWarnings("all")
  10. @Component
  11. public final class RedisUtils {
  12. // ......
  13. }
  1. 启动该工程

在这里插入图片描述

可以在控制台看到,写入是在6380端口的redis服务端,而读取是在6382端口的redis服务端

随便进入一个redis-cli客户端,可以看到主从复制实现成功!

在这里插入图片描述

  1. 测试哨兵模式

停止主节点6380的redis服务端,查看控制台

在这里插入图片描述

在这里插入图片描述

可以看到我们的主节点变成了6381端口的redis服务端,由从节点成为主节点,实现写的功能;而从节点是6382端口的服务端

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/808408
推荐阅读
相关标签
  

闽ICP备14008679号