- Jedis是同步的,不支持异步,Jedis客户端实例不是线程安全的,需要每个线程一个Jedis实例,所以一般通过连接池来使用Jedis
- Lettuce是基于Netty框架的事件驱动的Redis客户端,其方法调用是异步的,Lettuce的API也是线程安全的,所以多个线程可以操作单个Lettuce连接来完成各种操作,同时Lettuce也支持连接池
- SpringBoot2.0之前默认使用Jedis,而2.0之后默认就都是使用的Lettuce这个客户端连接Redis服务器
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- <version>2.9.0version>
- dependency>
建立redis.properties,配置如下
redis.node.maxTotal = 10
reids.node.host = xxxx
redis.node.port= 6379
- package com.buba.configuration;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringBootConfiguration;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.PropertySource;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- @SpringBootConfiguration
- @PropertySource(value = {"classpath:redis/redis.properties"})
- public class RedisConfiguration {
- @Value("${redis.node.maxTotal}")
- private Integer maxTotal;
- @Value("${redis.node.host}")
- private String host;
- @Value("${redis.node.port}")
- private Integer port;
-
- public JedisPoolConfig jedisPoolConfig(){
- JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
- jedisPoolConfig.setMaxTotal(maxTotal);
- return jedisPoolConfig;
- }
-
- @Bean //这个注解注入工厂的名称是方法名
- public JedisPool jedisPool(){
- JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
- return new JedisPool(jedisPoolConfig,host,port);
- }
- }
- @Autowired
- private JedisPool jedisPool
-
- Jedis resource = jedisPool. getResource();
- resource. set("test","springboot");
- String test = resource.get("test");
- System.outprintln(test);
- @SpringBootConfiguration
- public class RedisClusterConfiguration {
-
- // 读取redis.node
- @Value("${redis.cluster.nodes}")
- private String nodes;
-
- @Bean
- public JedisCluster jedisCluster(){
- Set
set = new HashSet<>(); - HostAndPort hp = null;
- String[] nodeStr = nodes.split(",");
- if(nodeStr!=null&&nodeStr.length>0){
- for(int i=0;i
- String[] hostPort = nodeStr[i].split(":");
- if(hostPort!=null&&hostPort.length>0){
- hp = new HostAndPort(hostPort[0],Integer.valueOf(hostPort[1]));
- set.add(hp);
- }
- }
- }
- JedisCluster jedisCluster = new JedisCluster(set);
- return jedisCluster;
- }
-
- }
测试集群版
- @Autowired
- private JedisCluster jedisCluster;
-
- jedisCluster.set("test","springbootcluster");
- String test = jedisCluster.get("test");
- System. out.println(test);
springBoot使用Lettuce
引入依赖
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
如果想要使用Jedis,需要排除Lettuce依赖,并引入Jedis以及Jedis依赖的commons-pool2
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- <exclusions>
- <exclusion>
- <groupId>io.lettucegroupId>
- <artifactId>lettuce-coreartifactId>
- exclusion>
- exclusions>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- dependency>
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- dependency>
-
配置lettuce
- # 连接池最大连接数(使用负值表示没有限制) 默认 8
- spring.redis.lettuce.pool.max-active=8
- # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
- spring.redis.lettuce.pool.max-wait=-1
- # 连接池中的最大空闲连接 默认 8
- spring.redis.lettuce.pool.max-idle=8
- # 连接池中的最小空闲连接 默认 0
- spring.redis.lettuce.pool.min-idle=0
测试使用
- @Autowired
- private StringRedisTemplate redisTemplate;
-
- String s = redisTemplate.opsForValue().get(key);
-
- System.out.println(s);
集群模式的lettuce使用StringRedisTemplate同样适配,内部做了封装,直接使用即可