SpringBoot 2.x版本整合redis集羣

SpringBoot 2.x版本整合redis集羣

啓動 Redis集羣 搭建方式 java

SpringBoot 1.x版本默認使用jedis 鏈接,2.x版本使用lettuce鏈接node

導入依賴

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

ymlredis

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>

ymlspring

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"));
    }
相關文章
相關標籤/搜索