美文网首页
Spring整合Lettuce Redis

Spring整合Lettuce Redis

作者: tenlee | 来源:发表于2019-12-04 20:19 被阅读0次

前言

以关键词【spring lettuce】搜索,大部分博文都是基于配置文件配置的,不太符合某些定制化需求。
所以本文提供两种配置方式。一种基于配置文件,一种基于Java Config。

  • Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
  • Lettuce是可伸缩的线程安全Redis客户端,用于同步,异步和反应式使用。 如果多个线程避免阻塞和事务操作(例如BLPOP和MULTI / EXEC),则可以共享一个连接。 Lettuce是基于netty的。 支持高级Redis功能,例如Sentinel,群集,管道,自动重新连接和Redis数据模型。
    https://www.zhihu.com/question/53124685

Java Config配置

增加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.2.1.RELEASE</version>
</dependency>
<!-- redis连接池需要-->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-pool2</artifactId>
  <version>2.7.0</version>
</dependency>

配置

单机redis配置

    @Bean
    public RedisConnectionFactory redisConnectionFactory(
            @Value("${spring.redis.host}") String host,
            @Value("${spring.redis.port}") Integer port,
            @Value("${spring.redis.pool.max-idle}") Integer maxIdle,
            @Value("${spring.redis.pool.max-wait}") Long maxWaitMillis,
            @Value("${spring.redis.pool.max-active}") Integer maxTotal,
            @Value("${spring.redis.pool.min-idle}") Integer minIdle) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        config.setMaxTotal(maxTotal);
        config.setMinIdle(minIdle);

        JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
                .usePooling()
                .poolConfig(config)
                .build();

        return new JedisConnectionFactory(new RedisStandaloneConfiguration(host, port), jedisClientConfiguration);
    }

哨兵模式redis配置

public RedisConnectionFactory redisConnectionFactory(
        @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}") Set<String> sentinelNodes,
        @Value("${spring.redis.sentinel.master}") String sentinelMaster,
        @Value("${spring.redis.pool.max-idle}") Integer maxIdle,
        @Value("${spring.redis.pool.max-wait}") Long maxWaitMillis,
        @Value("${spring.redis.pool.max-active}") Integer maxTotal,
        @Value("${spring.redis.pool.min-idle}") Integer minIdle) {
        GenericObjectPoolConfig<?> defaultPoolConfig = new GenericObjectPoolConfig<>();
        defaultPoolConfig.setMaxIdle(maxIdle);
        defaultPoolConfig.setMinIdle(minIdle);
        defaultPoolConfig.setMaxTotal(maxTotal);
        defaultPoolConfig.setMaxWaitMillis(maxWaitMillis);

        LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().poolConfig(defaultPoolConfig).build();

        RedisSentinelConfiguration configuration =
            new RedisSentinelConfiguration(sentinelMaster, sentinelNodes);

        return new LettuceConnectionFactory(configuration, clientConfig);
    }

参考源码:LettuceConnectionConfiguration:119

image.png

配置文件配置

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
##连接池最大连接数(使用负值表示没有限制) 默认8
spring.redis.lettuce.pool.max-active=8
## 连接池中的最大空闲连接 默认8
spring.redis.lettuce.pool.max-idle=8
## 连接池中的最小空闲连接 默认0
spring.redis.lettuce.pool.min-idle=0
## 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
# 连接超时时间(毫秒)
spring.redis.timeout=200

坑点

lettuce是基于netty的,所以有下面的坑点
https://github.com/lettuce-io/lettuce-core/wiki/Native-Transports
如果项目中依赖的netty低于4.0.26.Final,在linux环境下会导致无法运行。
建议项目中设置netty依赖版本大于等于4.1.11.Final

compile ('io.netty:netty-all:4.1.11.Final')

本人项目依赖了com.dianping.cat:cat,从而间接依赖了compile ('io.netty:netty-all:4.0.24.Final')
导致在linux环境运行一直报错,而macOS开发环境正常,因为macOS和windows环境都是不支持epoll的。

Caused by: java.lang.NoClassDefFoundError: io/netty/channel/unix/DomainSocketAddress
    at io.lettuce.core.EpollProvider.<clinit>(EpollProvider.java:65)
    at io.lettuce.core.Transports$NativeTransports.isSocketSupported(Transports.java:66)

相关文章

网友评论

      本文标题:Spring整合Lettuce Redis

      本文链接:https://www.haomeiwen.com/subject/dlzlgctx.html