windows版本下的 redis 集群配置

windows下的redis配置

https://www.cnblogs.com/thirteen-zxh/p/9187875.html ( 集群后篇)
https://www.cnblogs.com/tommy-huang/p/6240083.html
下载并安装ruby,
下载ruby环境下Redis的驱动,考虑到兼容性,这里下载的是3.2.2版本
下载Redis官方提供的创建Redis集群的ruby脚本文件redis-trib.rb )

配置方法首先阅读以上,两篇文章即可!

java配置文件

#redis配置--------------------------------------》
#spring.redis.host=139.199.8.134
#spring.redis.port=9836
#spring.redis.password=/6VBW6+dvVorh4zEOQbRJg==
#spring.redis.host=127.0.0.1
#spring.redis.port=6379
spring.redis.password=
spring.redis.cluster.nodes=127.0.0.1:7000,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
#192.168.1.26:30001,192.168.1.26:30002,192.168.1.26:30003
spring.redis.cluster.max-redirects=5

#redis数据库的索引,默认为0
#spring.redis.database=10
spring.redis.timeout=5000ms
#最大空闲链接数
spring.redis.lettuce.pool.max-idle=30
#最小空闲连接数\u3000
spring.redis.lettuce.pool.min-idle=1
#连接池最大连接数,负数表示无最大连接数
spring.redis.lettuce.pool.max-active=100
#连接池最大阻塞等待时间,负数表示没有\u3000
spring.redis.lettuce.pool.max-wait=-1ms

java代码

@EnableConfigurationProperties
@Configuration
public class RedisConfiguration {

    public static final Logger logger = LoggerFactory.getLogger(RedisConfiguration.class);

    @Value("${spring.redis.password}")
    String password;

//集群配置
    @Bean
    public RedisClusterConfiguration redisClusterConfiguration(RedisProperties redisProperties) {
        RedisClusterConfiguration redisClusterConfiguration =
                new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
        try {
            String decipheringPass = new AESUtils().Decrypt(password, AESUtils.KEY);
            redisClusterConfiguration.setPassword(RedisPassword.of(decipheringPass));
        } catch (Exception e) {
            logger.error("[运行异常]{}",e);
        }
        redisClusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
        return redisClusterConfiguration;
    }


//集群连接工厂(安全的) ,里边写入了集群客户端
    @Bean
    @Primary
    public RedisConnectionFactory lettuceConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {

        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
//        .useSsl()
//        .and()
                .commandTimeout(Duration.ofSeconds(15))
                .shutdownTimeout(Duration.ZERO)
                .poolConfig(genericObjectPoolConfig() )
                .build();

//        cfg.setDatabase(database);
        return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
    }

    /**
     * GenericObjectPoolConfig 连接池配置
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
    public GenericObjectPoolConfig genericObjectPoolConfig() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setTestOnBorrow(true);
        return genericObjectPoolConfig;
    }

    /**
     * 解决redisTemplate 使用默认jdk的序列号导致redis key-value 编码异常问题
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //设置value的序列化规则和 key的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setEnableDefaultSerializer(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    @Qualifier
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                // sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisSerializationContext.SerializationPair<?> pair = RedisSerializationContext.SerializationPair
                .fromSerializer(jackson2JsonRedisSerializer);
        RedisCacheConfiguration cacheCfg = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair)
                .entryTtl(Duration.ofSeconds(600));
        // return RedisCacheManager.create(connectionFactory);
        return new RedisCacheManager(redisCacheWriter, cacheCfg);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值