springboot连接redis集群

1、引入pom文件

  
           org.springframework.boot
           spring-boot-starter-data-redis
           2.2.5.RELEASE
    

2、 配置集群地址

spring.application.name=redis-cluster
server.port=8080
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,127.0.0.1:7006,127.0.0.1:7007
spring.redis.cluster.max-redirects=3
# 连接超时时长ms
spring.redis.timeout=10000
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)ms
spring.redis.jedis.pool.max-wait=2000
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=100
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
spring.redis.database=0

3、配置redisTemplate

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfiguration {

    @Bean
    public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

4、测试正常,正常赋值取值

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/test")
    public Object test() {
        Object author = redisTemplate.opsForValue().get("k1");
        System.out.println(redisTemplate.opsForValue().get("k2"));
        return author;
    }
}
image.png

image.png

你可能感兴趣的:(springboot连接redis集群)