温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在SpringBoot2.0中使用Redis连接池

发布时间:2020-12-03 14:35:52 来源:亿速云 阅读:573 作者:Leah 栏目:开发技术

怎么在SpringBoot2.0中使用Redis连接池?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

SpringBoot2.0默认采用Lettuce客户端来连接Redis服务端的

默认是不使用连接池的,只有配置 redis.lettuce.pool下的属性的时候才可以使用到redis连接池

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  lettuce:
   shutdown-timeout: 100 # 关闭超时时间
   pool:
    max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
    max-idle: 8 # 连接池中的最大空闲连接
    max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    min-idle: 0 # 连接池中的最小空闲连接

怎么在SpringBoot2.0中使用Redis连接池同时,使用连接池,要依赖commons-pool2

   <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

如果没有引入,会报错

怎么在SpringBoot2.0中使用Redis连接池

同时如果你想使用jedis客户端,则需要配置

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  jedis:
   pool:
    max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
    max-idle: 8 # 连接池中的最大空闲连接
    max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    min-idle: 0 # 连接池中的最小空闲连接

当然你也可以不配置,走默认的连接池配置,但是有一点要注意

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <exclusions>
  <exclusion>
   <groupId>io.lettuce</groupId>
   <artifactId>lettuce-core</artifactId>
  </exclusion>
  </exclusions>
 </dependency>
 
 <dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
 </dependency>

依赖包的引用里,要去掉lettuce,并且加上jedis的依赖包,否则都是走的lettuce客户端

怎么在SpringBoot2.0中使用Redis连接池

同时jedis的客户端默认增加了pool的连接池依赖包,所以Jedis默认你配置与否都会有连接池,而lettuce则需要配置文件中配置一下

补充知识:解决springboot2 RedisTemplate使用lettuce连接池配置不生效的问题

springboot2 redis默认使用lettuce,使用连接池根据网上的内容,进行如下配置:

# 连接池最大连接数 使用负值表示没有限制
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0

但是启动后,多线程调用查询redis,通过redis-cli的info clients。

发现连接数并没有变多。

经过翻阅资料和源码发现,LettuceConnectionFactory类里面有个shareNativeConnection变量,默认为true。

说明共享本地连接,这样的话就不会创建多个连接了,连接池也就没用了。因此需要把这个值设为false。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
 
@Configuration
public class RedisConfig {
  @Resource
  private LettuceConnectionFactory lqlcfactory;
  @Bean
  public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(connectionFactory);
    return template;
  }
 
  @Bean
  public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    lqlcfactory.setShareNativeConnection(false);
    stringRedisTemplate.setConnectionFactory(lqlcfactory);
    return stringRedisTemplate;
  }
}

这样lettuce连接池就设置成功了。

为什么改这里就可以了呢?从LettuceConnectionFactory的源码分析

/*
 * (non-Javadoc)
 * @see org.springframework.data.redis.connection.RedisConnectionFactory#getConnection()
 */
 public RedisConnection getConnection() {
 
 if (isClusterAware()) {
  return getClusterConnection();
 }
 
 LettuceConnection connection;
 connection = doCreateLettuceConnection(getSharedConnection(), connectionProvider, getTimeout(), getDatabase());
 connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
 return connection;
 }
protected LettuceConnection doCreateLettuceConnection(
  @Nullable StatefulRedisConnection<byte[], byte[]> sharedConnection, LettuceConnectionProvider connectionProvider,
  long timeout, int database) {
 
 return new LettuceConnection(sharedConnection, connectionProvider, timeout, database);
 }

上面两个函数是获取connection连接,可以看到getSharedConnection()和shareNativeConnection配置有关了

@Nullable
 protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() {
 return shareNativeConnection &#63; (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
 }

如果是true,就是返回已经存在的连接,如果是false,返回null。那就只能创建新的连接了,如下

LettuceConnection(@Nullable StatefulConnection<byte[], byte[]> sharedConnection,
  LettuceConnectionProvider connectionProvider, long timeout, int defaultDbIndex) {
 
 Assert.notNull(connectionProvider, "LettuceConnectionProvider must not be null.");
 
 this.asyncSharedConn = sharedConnection;
 this.connectionProvider = connectionProvider;
 this.timeout = timeout;
 this.defaultDbIndex = defaultDbIndex;
 this.dbIndex = this.defaultDbIndex;
 }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI