优艾设计网

Spring Boot如何整合Redis?

优艾设计网 https://www.uibq.com 2023-02-05 14:15 出处:网络 作者:PS入门教程
程南 2021-08-14 08:20 Spring Boot是目前非常流行的Java Web开发框架,Redis是非关系型数据库的一种,以键值对的形式存储。Spring对Redis的支持是通过Spring Data Redis来实现的,给我们提供了RedisTemplate和
程南 2021-08-14 08:20

Spring Boot是目前非常流行的Java Web开发框架,Redis是非关系型数据库的一种,以键值对的形式存储。Spring对Redis的支持是通过Spring Data Redis来实现的,给我们提供了RedisTemplate和StringRedisTemplate两种模板来操作数据。Spring Boot框架也提供了对Redis的支持,下面我们来讲一下Spring Boot框架整合Redis的步骤。

工具/材料

IntelliJ IDEA

操作方法 01

Spring Boot整合Redis我们需要添加依赖的jar包,spring-boot-starter-data-redis中包含spring和redis相关的jar包,jedis作为redis的客户端也需要添加到工程中,Spring Boot的版本信息在父pom中已指定,子模块中的spring相关的jar包无需另外指定。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0-m1</version> </dependency>

02

Spring Boot会根据application.properties中的配置对Redis的属性进行自动配置,并注入到RedisProperties类中。在application.properties配置文件中这些属性都是以spring.redis为前缀的,值得注意的是在Spring Boot 1.5.x版本中默认的Redis客户端是jedis,因此在配置文件中无需指定,如下图所示。

03 优艾设计网_电脑技术

Spring Boot 1.5.x版本的整合配置网上可以搜索大量的文章,然而Spring Boot 2.x版本的整合资料却非常少,甚至提供的配置不能正常使用,因此本文主要讲解Spring Boot 2.x整合Redis以及Redis的使用情况。spring-boot 2.x版本有jedis和lettuce两种客户端,因此我们必须要去指定使用哪一种客户端,两个客户端的配置如下图所示,本文使用的是Jedis客户端连接池,具体的配置如下。# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=127.0.0.1# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=xylx1.t!@## 配置jedis连接池# 连接池最大连接数(使用负值表示没有限制)spring.redis.jedis.pool.max-active=8# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.jedis.pool.max-wait=-1ms# 连接池中的最大空闲连接spring.redis.jedis.pool.max-idle=8# 连接池中的最小空闲连接spring.redis.jedis.pool.min-idle=0# 连接超时时间(毫秒)spring.redis.timeout=5000ms由配置我们可以看到spring-boot 2.x版本时间设置需要加单位ms,因为参数的类型为Duration。另外spring.redis.timeout尽量不要配置0,否则可能会出现io.lettuce.core.RedisCommandTimeoutException: Command timed out超时错误。

04

配置文件编辑完成后,我们开始编写代码实现Redis数据的存储和读取。我们创建一个RedisUtil工具类,该类使用@Component注解表示交由Spring管理,StringRedisTemplate是Spring提供的,可以使用@Autowired注解直接注入,接下来便可以书写存和取的代码了。@Componentpublic class RedisUtil { @Autowired private StringRedisTemplate redisTemplate; /** * 存字符串 * @param key 缓存键 * @param value 缓存值 * @param expireTime 过期时间(s) */ public void setString(String key, String value, int expireTime){ ValueOperations<String, String> ops = redisTemplate.opsForValue(); if (expireTime != 0) { ops.set(key, value, expireTime, TimeUnit.SECONDS); } else { ops.set(key,value); } } /** * 取字符串 * @param key 缓存键 * @return 缓存值 */ public String getString(String key){ ValueOperations<String, String> ops = this.redisTemplate.opsForValue(); return ops.get(key); }

05

接下来我们编写Controller层代码去调用RedisUtil工具类,实现数据的存储和读取,代码比较简单可以参考下图。若想验证Redis是否可用,还需要编写启动类,如下图所示。

06

由上图可看到我们编写了一个post请求用于存储字符串,get请求用于取出字符串。启动类通过main方法启动应用,接下来我们使用postman去模拟浏览器调用post和get请求,由下图可以看到Redis存储的数据成功被取出。

07

接下来我们介绍Jedis,这是一个封装了Redis的客户端,在Spring Boot整合Redis的基础上,可以提供更简单的API操作。因此我们需要配置JedisPool的Bean,代码如下,其中@Configuration注解表明这是一个配置类,我们在该类中注入RedisProperties,并且使用@Bean注解指定JedisPool。@Configurationpublic class RedisConfiguration { @Autowired private RedisProperties properties; @Bean public JedisPool getJedisPool(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(properties.getJedis().getPool().getMaxIdle()); config.setMaxTotal(properties.getJedis().getPool().getMaxActive()); config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis()); JedisPool pool = new JedisPool(config,properties.getHost(), properties.getPort(),100, properties.getPassword(), properties.getDatabase()); return pool; }}

08

接下来我们编辑JedisUtil工具类,通过SpringBoot容器的@Component注解来自动创建,并且注入JedisPool,使用jedisPool.getResource()方法来获取Jedis,并最终实现操作redis数据库,其代码如下。@Componentpublic class JedisUtil { @Autowired JedisPool jedisPool; //获取key的value值 public String get(String key) { Jedis jedis = jedisPool.getResource(); String str = ""; try { str = jedis.get(key); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String str = ""; try { str = jedis.set(key, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; }}

09

JedisUtil工具类编写完成后,我们修改之前的RedisController,并注入JedisUtil,代码如下图所示。然后再用postman分别调用post和get接口,我们可以看到成功取到了新的key的value值。

特别提示

在Spring Boot整合Redis前本机需安装Redis,另外可以使用RedisDesktopManager这个Redis这个桌面管理工具查看Redis中的数据。


0

精彩评论

暂无评论...
验证码 换一张
取 消