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"));
    }
相关文章
相关标签/搜索