springboot2.x对redis操作做了很大改进,spring-boot-starter-data-redis默认使用了lettuce。

Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例

单机版(分别使用jedis和lettuce):
1.pom.xml添加依赖

<!-- springboot web 依赖的jar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--使用jedis作为redis客户端时需导入,
        使用的lettuce时不需要-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>

2.application.yml 文件添加

server:
  port: 8110
spring:
  profiles:
    active: single
  application:
    name: service-redis

3.application-single.yml 此处为了区分单机和集群环境

#单机版redis
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    #默认0
    database: 0
    #密码
    password:
    jedis:
    	pool:
      	#连接池最大连接数,使用负值表示没有限制
      	max-active: 100
      	#连接池中的最大空闲连接
      	max-idle: 5
      	#连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
      	max-wait: 2000
      	#连接池中的最小空闲连接
      	min-idle: 1
    #连接超时时间(毫秒)
    timeout: 2000

如果使用lettuce,spring-boot-starter-data-redis已经依赖了lettuce核心包(也不需要jedis和common-pool2),只需修改

lettuce: #这里
 	pool:
   	#连接池最大连接数,使用负值表示没有限制
   	max-active: 100
   	#连接池中的最大空闲连接
   	max-idle: 5
   	#连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
   	max-wait: 2000
   	#连接池中的最小空闲连接
   	min-idle: 1
 #连接超时时间(毫秒)
 timeout: 2000

4.在需要使用redis的地方直接注入RedisTemplate

@RestController
public class UserController {
	
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/getUser")
    public String getUser(){
        return (String)redisTemplate.opsForValue().get("name");
    }

    @RequestMapping("/setUser")
    public String setUser(String name){
        redisTemplate.opsForValue().set("name",name);
        return "name:"+name;
    }
}

但是根据日志来看,程序依然使用了lettuce,暂时不清楚原因,有读者知道的,欢迎留言,感谢!!!
集群版:
1.pom.xml依赖同单机版一样
2.application.yml 文件将环境改为cluster集群

server:
  port: 8110
spring:
  profiles:
    active: cluster
  application:
    name: service-redis

3.application-cluster.yml 集群环境配置信息(分别使用jedis和lettuce)

spring:
  redis:
    #host:
    #port:
    #密码
    #password:
    jedis:
    	pool:
      	#连接池最大连接数,使用负值表示没有限制
     	 max-active: 100
     	 #连接池中的最大空闲连接
      	max-idle: 5
      	#连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
      	max-wait: 2000
      	#连接池中的最小空闲连接
      	min-idle: 1
    #连接超时时间(毫秒)
    timeout: 2000
    cluster:
      nodes: 192.168.31.11:6378,192.168.31.11:6379,192.168.31.18:6378,192.168.31.18:6379,192.168.31.25:6378,192.168.31.25:6379
      max-redirects: 3

使用lettuce,只需修改

lettuce: #这里
 	pool:
   	#连接池最大连接数,使用负值表示没有限制
   	max-active: 100
   	#连接池中的最大空闲连接
   	max-idle: 5
   	#连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
   	max-wait: 2000
   	#连接池中的最小空闲连接
   	min-idle: 1
 #连接超时时间(毫秒)
 timeout: 2000

4.在需要使用redis的地方直接注入RedisTemplate,同单机版4一样

项目目录结构

lettuce 链接不到redis lettuce连接redis集群_lettuce 链接不到redis

redistemplate之所以能直接使用是springboot自动化装配,已经为我们实例化了这个实例
到此结果。


如果有需要我们可以扩展redistemplate

/**
 * Created by lifeng on 2019/3/27.
 * 自定义redis的一些属性
 * 比如序列化和反序列化
 * 此类可以没有
 */
@Configuration
public class RedisConfig {

    /**  此处可能报错提示RedisConnectionFactory 类找不到,不影响运行*/
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}