> 文档中心 > Redis哨兵java实现

Redis哨兵java实现

文章目录

  • 前言
    • 一、先改好RedisConfig
    • 二、修改nacos中Redis的配置
    • 三、其他
        • 1.踩到的坑如下:
        • 1.下期将讲解:ngnix实现sentinel的负载均衡
  • 总结

前言


一、先改好RedisConfig

前提是已经建好Redis的springboot项目,按网上的操作就行。我的是在这基础上操作

@Configuration@EnableConfigurationProperties(RedisProperties.class)public class RedisConfig {    private final RedisProperties properties;    public RedisConfig(RedisProperties properties){ this.properties = properties;    }    @Bean    public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(getConnectionFactory()); // 用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); // key Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); // 指定要序列化的域(field,get,set),访问修饰符(public,private,protected) objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //value redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate;    }    @Bean    public RedisConnectionFactory getConnectionFactory() { //哨兵模式 RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(); configuration.setMaster(properties.getSentinel().getMaster()); configuration.setPassword(properties.getPassword()); configuration.setDatabase(properties.getDatabase()); List<String> nodes = properties.getSentinel().getNodes(); nodes.forEach(node -> {     String[] str = node.split(":");     RedisNode redisServer = new RedisServer(str[0], Integer.parseInt(str[1]));     configuration.sentinel(redisServer); }); LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, getPool()); //使用前先校验连接,这个最好是要配置:不然会带来connection reset by peer factory.setValidateConnection(true); return factory;    }    @Bean    public LettuceClientConfiguration getPool() { GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); //redis客户端配置:超时时间默认 LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder  builder = LettucePoolingClientConfiguration.builder().  commandTimeout(Duration.ofMillis(60000)); //连接池配置 RedisProperties.Pool pool = properties.getLettuce().getPool(); genericObjectPoolConfig.setMaxIdle(pool.getMaxIdle()); genericObjectPoolConfig.setMinIdle(pool.getMinIdle()); genericObjectPoolConfig.setMaxTotal(pool.getMaxActive()); genericObjectPoolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis()); builder.shutdownTimeout(Duration.ofMillis(4000)); builder.poolConfig(genericObjectPoolConfig); return builder.build();    }

二、修改nacos中Redis的配置

redis:  sentinel:    master: master    nodes: 192.168.2.237:6776,192.168.2.37:6777,192.168.2.37:6778  password: 123456  timeout: 1000  database: 3  lettuce:    pool:      max-active: 8      max-wait: 1000      max-idle: 8      min-idle: 0

三、其他

1.踩到的坑如下:

java一直无法连接sentinel,关闭防火墙也不行
解决方法:因为之前redis配置的防火墙打开,开放端口,所以这里也不能关闭防火墙,而是要开放sentinel的几个端口。

1.下期将讲解:ngnix实现sentinel的负载均衡


总结

*随心所往,看见未来。Follow your heart,see night!*
**欢迎点赞、关注、留言,一起学习、交流!**