SpringBoot 2.x版本整合Redis集群

1,554 阅读1分钟

SpringBoot 2.x版本整合redis集群

启动 Redis集群 搭建方式

SpringBoot 1.x版本默认使用jedis 连接,2.x版本使用lettuce连接

导入依赖

     <!-- redis缓存 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>



yml

spring:
  redis:
    cluster:
      nodes:
        - 127.0.1.1:7001
        - 127.0.1.1:7002
        - 127.0.1.1:7003
        - 127.0.1.1:7004
        - 127.0.1.1:7005
        - 127.0.1.1:7006
      max-redirects: 3  # 获取失败 最大重定向次数
    pool:
      max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
      max-idle: 10    # 连接池中的最大空闲连接
      max-wait: -1   # 连接池最大阻塞等待时间(使用负值表示没有限制)
      min-idle:  5     # 连接池中的最小空闲连接
    timeout: 6000  # 连接超时时长(毫秒)

使用默认的连接池

使用jedis连接池


        <!--        使用jedis连接池-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

yml


spring:
  redis:
    password:    # 密码(默认为空)
    timeout: 6000ms  # 连接超时时长(毫秒)
    cluster:
      nodes:
        - 127.0.1.1:7001
        - 127.0.1.1:7002
        - 127.0.1.1:7003
        - 127.0.1.1:7004
        - 127.0.1.1:7005
        - 127.0.1.1:7006
    jedis:
      pool:
        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10      # 连接池中的最大空闲连接
        min-idle: 5       # 连接池中的最小空闲连接

测试

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public  void test1(){
        redisTemplate.opsForValue().set("k2","k123");
        System.out.println(redisTemplate.opsForValue().get("k"));
    }