Spring Data Redis 单节点和集群配置【排雷版】

1. 配置方式

单点和集群配置,参看下文
https://blog.csdn.net/MOTUI/article/details/52903397

2. 遭遇问题

2.1 标签飘红

解决方法 :
把标签中的代码重新粘一次,不行就去掉中文再来一次,再不行就手打

原因:好像是从个别博客复制来的格式有问题

2.2 RedisClusterConfiguration无法调用(红色)

解决方法:
提升pom里的 jedisspring-data-redis的版本

 <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.11.RELEASE</version>
</dependency>
2.3 maxRedirects这个属性注不进去

报错内容:
org.springframework.beans.NotWritablePropertyException: Invalid property ‘maxRedirects’ of bean class [org.springframework.data.redis.connection.RedisClusterConfiguration]: Bean property ‘maxRedirects’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

解决方法:
提升pom里spring相关包的版本

<dependency>
    <groupId>Spring的系列包</groupId>
    <artifactId>Spring的系列包</artifactId>
    <version>4.2.5.RELEASE以上</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Spring Boot集成Redis主从集群配置方法: 1.在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2.在application.properties文件中添加以下配置: ```properties # Redis节点配置 spring.redis.master.host=127.0.0.1 spring.redis.master.port=6379 spring.redis.master.password=123456 # Redis节点配置 spring.redis.slave.nodes=127.0.0.1:6380,127.0.0.1:6381 spring.redis.slave.password=123456 ``` 3.创建RedisConfig类,配置RedisTemplate和JedisConnectionFactory: ```java @Configuration public class RedisConfig { @Value("${spring.redis.master.host}") private String masterHost; @Value("${spring.redis.master.port}") private int masterPort; @Value("${spring.redis.master.password}") private String masterPassword; @Value("${spring.redis.slave.nodes}") private String slaveNodes; @Value("${spring.redis.slave.password}") private String slavePassword; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(jedisConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster") .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380) .sentinel("127.0.0.1", 26381) .setPassword(masterPassword); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig); jedisConnectionFactory.setUsePool(true); jedisConnectionFactory.setPoolConfig(jedisPoolConfig()); return jedisConnectionFactory; } @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(50); jedisPoolConfig.setMinIdle(20); jedisPoolConfig.setMaxWaitMillis(3000); return jedisPoolConfig; } } ``` 4.在需要使用Redis的类中注入RedisTemplate即可使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值