Springboot整合redis(一般人都能看懂的Lettuce版本)

去年学习的Redis,刚刚学习完就迫不及待的在实战中用了一下,走了很多坑不过幸好都填上了,需求的不断变化发现用不上Redis,一开始去掉了,后来想想加进来比较合适。这篇文章主要讲解Springboot如何整合开发Redis实现一个基本的案例。使用的是目前Springboot2.x的Lettuce版本。希望对你有帮助。

这里因为不是专门讲解Redis的,所以假定你已经学习了Redis,只是希望在SpringBoot2.x中使用。

废话不多说,直接按照步骤开始,以下的案例均在我自己的电脑上测试成功,如有问题可以联系我。

一、开发环境

名称 版本
Idea 2018专业版(已破解)
Maven 4.0.0
SpringBoot 2.2.2
Redis 3.2.1
RedisDesktopManager 0.8.8
jdk 1.8

版本的话其实差不不大就没问题,最主要的就是Springboot的版本,在这里说一下Jedis和Lettuce的区别在哪?

1、Jedis 是直连模式,在多个线程间共享一个 Jedis 实例时是线程不安全的,每个线程都去拿自己的 Jedis 实例,当连接数量增多时,物理连接成本就较高了。

2、Lettuce的连接是基于Netty的,连接实例可以在多个线程间共享,如果你不知道Netty也没事,大致意思就是一个多线程的应用可以使用同一个连接实例,而不用担心并发线程的数量。通过异步的方式可以让我们更好的利用系统资源。

既然有这么大的好处,干脆就用了这个,跟上时代的变化。下面新建一个SpringBootRedis项目,开始整合。

二、整合

步骤一:添加依赖

<!-- spring boot redis 缓存引入 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool 缓存连接池 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

步骤二:application.properties配置文件

# Redis数据库索引(默认为0)
spring.redis.database=0  
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379  
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制) 默认 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

当然如果你的属性文件是yml的,把格式调整一下就OK了。

步骤三:新建config包,创建RedisConfig类

默认情况下RedisTemplate模板只能支持字符串,我们自定义一个RedisTemplate,设置序列化器,这样我们可以很方便的操作实例对象。

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable> 
    		redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

步骤四:新建bean包,创建User类

public class User implements Serializable {
    private Integer id;
    private String userName;
    private String userSex;
    public User() { }
    public User(Integer id, String userName, String userSex) {
        this.id = id;
        this.userName = userName;
        this.userSex = userSex;
    }
    //getter和setter方法
    //toString方法
}

步骤五:测试插入一个User

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootredisApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testObj() throws Exception {
        User user=new User(1, "java的架构师技术栈", "man");
        ValueOperations<String, User> operations=redisTemplate.opsForValue();
        operations.set("fdd2", user);
        boolean exists=redisTemplate.hasKey("fdd2");
        System.out.println("redis是否存在相应的key"+exists);
        User getUser = (User)redisTemplate.opsForValue().get("fdd2");
        System.out.println("从redis数据库获取的user:"+getUser.toString());
    }
}

在上面这个例子中我们使用redisTemplate调用了opsForValue会得到一个ValueOperations操作。这个是专门操作String类型的数据,所以里面的键值对中键为String,而值是我们的User。当然redisTemplate还为我们提供了下面几个。

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

具体使用的时候,你可以根据自己的数据类型选择相应的方法即可,网上有各种RedisUtil工具类

喜欢的可以关注我的公众号:java的架构师技术栈,获取各种文章和教程资源

发布了256 篇原创文章 · 获赞 75 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/SDDDLLL/article/details/103703627