windows版本下的 redis 集群配置

 2023-09-15 阅读 24 评论 0

摘要: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官方提供的创

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;//集群配置@Beanpublic 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@Primarypublic 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*/@Beanpublic 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@Qualifierpublic KeyGenerator wiselyKeyGenerator() {return new KeyGenerator() {@Overridepublic 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();}};}@Beanpublic 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);}}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/59809.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息