怎么在SpringBoot2.0中使用复述,连接池

  介绍

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

SpringBoot2.0默认采用生菜客户端来连接复述,服务端的

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

复述:
  集群:
  节点:$ {redis.host.cluster}
  密码:$ {redis.password}
  生菜:
  shutdown-timeout: 100 #关闭超时时间
  池:
  max-active: 8 #连接池最大连接数(使用负值表示没有限制)
  最大空闲:8 #连接池中的最大空闲连接
  最大等待:30 #连接池最大阻塞等待时间(使用负值表示没有限制)
  min-idle: 0 #连接池中的最小空闲连接

怎么在SpringBoot2.0中使用复述,连接池”>同时,使用连接池,要依赖commons-pool2 <br/> </p> <pre class= & lt; dependency>   & lt; groupId> org.apache.commons   & lt; artifactId> commons-pool2   & lt;/dependency>

如果没有引入,会报错

怎么在SpringBoot2.0中使用复述,连接池

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

复述:
  集群:
  节点:$ {redis.host.cluster}
  密码:$ {redis.password}
  能:
  池:
  max-active: 8 #连接池最大连接数(使用负值表示没有限制)
  最大空闲:8 #连接池中的最大空闲连接
  最大等待:30 #连接池最大阻塞等待时间(使用负值表示没有限制)
  min-idle: 0 #连接池中的最小空闲连接

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

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

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

怎么在SpringBoot2.0中使用复述,连接池

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

<强>解决springboot2 RedisTemplate使用生菜连接池配置不生效的问题

springboot2复述,默认使用生菜、使用连接池根据网上的内容,进行如下配置:

 #连接池最大连接数使用负值表示没有限制
  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-cli的信息客户。

发现连接数并没有变多。

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

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

进口org.springframework.context.annotation.Bean;
  进口org.springframework.context.annotation.Configuration;
  进口org.springframework.data.redis.connection.RedisConnectionFactory;
  进口org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  进口org.springframework.data.redis.core.RedisTemplate;
  进口org.springframework.data.redis.core.StringRedisTemplate;
  进口javax.annotation.Resource;
  
  @ configuration
  公开课RedisConfig {
  @
  私人LettuceConnectionFactory lqlcfactory;
  @ bean
  公共RedisTemplate RedisTemplate (RedisConnectionFactory connectionFactory) {
  RedisTemplate模板=new RedisTemplate ();
  template.setConnectionFactory (connectionFactory);
  返回模板;
  }
  
  @ bean
  公共StringRedisTemplate StringRedisTemplate (RedisConnectionFactory工厂){
  StringRedisTemplate StringRedisTemplate=new StringRedisTemplate ();
  lqlcfactory.setShareNativeConnection(假);
  stringRedisTemplate.setConnectionFactory (lqlcfactory);
  返回stringRedisTemplate;
  }
  }

怎么在SpringBoot2.0中使用复述,连接池