springboot2.x采用lettuce整合redis集群

1 springboot2.x采用lettuce整合redis集群

1 概念说明

springboot1.x系列中使用的是jedis, springboot2.x使用的是Lettuce。
关于jedis跟lettuce的区别:

  • Lettuce 和 Jedis 都是Redis的client,都可以连接redis server。
  • Jedis在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接。
  • Lettuce的连接基于Netty,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,是线程安全的,这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

2 参考网址

1.1 pom依赖

   <!-- 集成redis采用lettuce方式需添加commons-pool依赖 -->
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>

1.2 配置文件(properties或yml)

properties配置

spring.redis.timeout=30000
spring.redis.lettuce.pool.max-active=256
spring.redis.lettuce.pool.max-idle=64
spring.redis.lettuce.pool.max-wait=30000
spring.redis.lettuce.pool.min-idle=32
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384

yml配置

spring:
  redis:
    timeout: 30000
    lettuce:
      pool:
        max-active: 256
        max-idle: 64
        max-wait: 30000
        min-idle: 32
    cluster:
      max-redirects: 3  # 获取失败 最大重定向次数
      nodes:
      - 127.0.0.1:6379
      - 127.0.0.1:6380
      - 127.0.0.1:6381
      - 127.0.0.1:6382
      - 127.0.0.1:6383
      - 127.0.0.1:6384

1.3 代码类

1.3.1 配置类

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

1.3.2 测试类

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.Set;

@SpringBootTest
class LettuceClusterTest {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("name", "hello world");
        String name = redisTemplate.opsForValue().get("name").toString();
        System.out.println(name);

        System.out.println("》》》》》》》》》》》获取配置信息");
        RedisSerializer<?> keySerializer = redisTemplate.getKeySerializer();
        System.out.println(keySerializer);
        LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
        Set<RedisNode> clusterNodes = connectionFactory.getClusterConfiguration().getClusterNodes();
        System.out.println(clusterNodes);
        GenericObjectPoolConfig poolConfig = ((LettucePoolingClientConfiguration) connectionFactory.getClientConfiguration()).getPoolConfig();
        System.out.println(poolConfig);
    }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值