栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot之redis使用(Lettuce版本)

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot之redis使用(Lettuce版本)

前言

前面三节我们讲解了springboot与关系型数据库交互,现在我们需要了解一下springboot,今天我们就需要学习了与nosql数据库交互,今天我们主要讲一下springboot如果操作redis。 目前java操作redis的客户端有jedis跟Lettuce。在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce。 因为我们的版本是springboot2.x系列,所以今天使用的是Lettuce。
关于jedis跟lettuce的区别:

  • Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。

  • Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接

  • Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

实现1、依赖

新建一个springboot工程,添加如下依赖。



        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.apache.commons
            commons-pool2
        
    

然后在application.yml配置一下redis服务器的地址

server:
  port: 8989
spring:
  redis:
    host: 127.0.0.1
    port: 6379    # 密码 没有则可以不填
    password: 123456    # 如果使用的jedis 则将lettuce改成jedis即可
    lettuce:
      pool:        # 最大活跃链接数 默认8
        max-active: 8        # 最大空闲连接数 默认8
        max-idle: 8        # 最小空闲连接数 默认0
        min-idle: 0
2、 redis配置

接下来我们需要配置redis的key跟value的序列化方式,默认使用的JdkSerializationRedisSerializer 这样的会导致我们通过redis desktop manager显示的我们key跟value的时候显示不是正常字符。 所以我们需要手动配置一下序列化方式 新建一个config包,在其下新建一个RedisConfig.java 具体代码如下

@Configuration@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisConfig {    
    @Bean
    RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);        //使用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.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();        return template;
    }

}

其中@Configuration 代表这个类是一个配置类,然后@AutoConfigureAfter(RedisAutoConfiguration.class) 是让我们这个配置类在内置的配置类之后在配置,这样就保证我们的配置类生效,并且不会被覆盖配置。其中需要注意的就是方法名一定要叫redisTemplate  因为@Bean注解是根据方法名配置这个bean的name的。

3、测试

我们需要测试在redis缓存对象的用例,所以我们需要新建一个实体类。
代码如下:

public class User implements Serializable {    private static final long serialVersionUID = 1222221L;    private Long id;    
    private String username;    
    private String password;    
    private Integer age;    
    private Integer sex;    public String getUsername() {        return username;
    }    public void setUsername(String username) {        this.username = username;
    }    public String getPassword() {        return password;
    }    public void setPassword(String password) {        this.password = password;
    }    public Integer getAge() {        return age;
    }    public void setAge(Integer age) {        this.age = age;
    }    public Integer getSex() {        return sex;
    }    public void setSex(Integer sex) {        this.sex = sex;
    }    public Long getId() {        return id;
    }    public void setId(Long id) {        this.id = id;
    }    @Override
    public String toString() {        return "User{" +                "id=" + id +                ", username='" + username + ''' +                ", password='" + password + ''' +                ", age=" + age +                ", sex=" + sex +                '}';
    }
}
我们在`Chapter6Application.java`测试一下

代码如下

@RunWith(SpringRunner.class)@SpringBootTestpublic class Chapter6ApplicationTests {    @Autowired
    private RedisTemplate redisTemplate;    @Test
    public void redisTest() {        // redis存储数据
        String key = "name";
        redisTemplate.opsForValue().set(key, "yukong");        // 获取数据
        String value = (String) redisTemplate.opsForValue().get(key);
        System.out.println("获取缓存中key为" + key + "的值为:" + value);

        User user = new User();
        user.setUsername("yukong");
        user.setSex(18);
        user.setId(1L);
        String userKey = "yukong";
        redisTemplate.opsForValue().set(userKey, user);
        User newUser = (User) redisTemplate.opsForValue().get(userKey);
        System.out.println("获取缓存中key为" + userKey + "的值为:" + newUser);

    }

}

首先我们引入springboot的测试环境,然后注入一个RedisTemplate对象,这里我解释一下为什么我前面让大家在配置redis的时候 放回RedisTemplate的方法的方法名一定要叫redisTemplate因为

  • 如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据

  • 如果查询的结果不止一个,那么@Autowired会根据名称来查找。

  • 如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false

如果我们没有把哪个bean命名为redisTemplate 而这里又这么注入,会导致我们使用的springboot内置的tempalte而不是我们配置的,导致我们的配置不生效,从而埋坑,所以最好的方法就是把方法命名为redisTemplate从而覆盖系统内置的。

接下来我们运行测试类。结果如下。


image.png

同时查看redis中缓存的结果。


image.png

image.png



作者:余空啊
链接:https://www.jianshu.com/p/feef1421ab0b


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号