Spring Boot如何整合Redis

Spring Boot是一个非常流行的Java Web开发框架 , Redis是一个非关系数据库 , 以键值对的形式存储 。Spring通过Spring Data Redis支持Redis , 并为我们提供了RedisTemplate和StringRedisTemplate两个模板来操作数据 。Spring Boot框架也为Redis提供了支持 。让我们来谈谈将Redis与Spring Boot框架集成的步骤 。
【Spring Boot如何整合Redis】 工具/材料
IntelliJ IDEA
01
为了将redis与spring Boot集成 , 我们需要添加相关的jar包 。spring-boot-starter-data-redis包含与spring和redis相关的jar包 。jedis作为Redis的客户端 , 也需要添加到项目中 。Spring Boot的版本信息已经在父pom中指定 , 子模块中与Spring相关的jar包不需要单独指定 。dependency GrouP id org . spring framework . boot/GrouP id artifact id spring-boot-starter-data-redis/artifact id/dependency dependency GrouP id redis . clients/GrouP id artifact id jedis/artifact id 3 . 0 . 0版-m1 /version /dependency
02
根据application.properties中的配置 , Spring Boot将自动配置Redis的属性 , 并将它们注入到RedisProperties类中 。在application.properties配置文件中 , 这些属性的前缀是spring 。Redis值得注意的是 , 在Spring Boot 1.5.x版本中 , 默认的redis客户端是jedis , 因此无需在配置文件中指定 , 如下图所示 。
03
对于Spring Boot 1.5.x的集成配置 , 网上可以搜索到大量的文章 , 但是Spring Boot 2.x的集成信息很少 , 甚至提供的配置都无法正常使用 。因此 , 本文主要说明Redis在Spring Boot 2.x中的集成及其用法 。spring-boot 2 . x版本有两个客户端 , Jedis和莴苣 , 所以我们必须指定使用哪个客户端 。下图显示了两个客户端的配置 。本文使用的是jedis客户端连接池 , 具体配置如下 。# Redis数据库索引(默认为0)spring . Redis . database = 0 # Redis服务器地址spring . Redis . host = 127 . 0 . 0 . 1 # Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空) spring.redis. @ # #配置jedis连接池#连接池中的最大连接数(负值表示无限制)spring . Redis . jedis . pool . max-active = 8 #连接池的最大阻塞等待时间Idle connection spring . redis . jedis . pool . max-Idle = 8 #连接池中最小的空Idle connection spring . redis . jedis . pool . min-Idle = 0 #连接超时(毫秒)spring.redis.timeout=5000ms通过配置 , 我们可以看到spring-boot2 。此外 , 尽量不要为spring.redis.timeout配置0 , 否则可能会出现io . letteruce . core . rediscommandtimeoutexception:命令超时超时错误 。
04
编辑完配置文件后 , 我们开始编写代码来存储和读取Redis数据 。我们创建了一个RedisUtil工具类 , 由Spring用@Component的注解来管理 , StringRedisTemplate由Spring提供 , 可以直接用@Autowired的注解来注入 , 然后就可以编写保存和取数的代码了 。@Componentpublic类RedisUtil { @ Autowired private StringRedisTemplate;/* * *保存字符串* @param键缓存键* @param值缓存值* @ paramexpitimeexpirement time(s)*/public void setstring(String key , stringvalue , int expireTime){ value operations String , String ops = redistemplate . opsforvalue();如果(expireTime!= 0) { ops.set(键、值、到期时间、时间单位 。秒);} else { ops.set(key , value);}}/* * *取字符串* @ param key cache key * @ return cache value */public string get string(string key){ value operations string , string ops = this 。redis template . ops for value();返回ops.get(键);}
05
接下来 , 我们编写控制器层代码来调用RedisUtil工具类来存储和读取数据 。代码比较简单 , 请参考下图 。如果想要验证Redis是否可用 , 还需要编写一个启动类 , 如下图所示 。
06
从上图中可以看出 , 我们编写了一个存储字符串的post请求和一个检索字符串的get请求 。启动类通过主方法启动应用程序 。接下来 , 我们使用postman来模拟浏览器调用post并获取请求 。从下图中 , 我们可以看到存储在Redis中的数据已经被成功检索到 。
07
接下来我们介绍Jedis , 一个封装Redis的客户端 , 可以在Spring Boot中集成Redis的基础上提供更简单的API操作 。因此 , 我们需要配置JedisPool的Bean , 代码如下 , 其中@Configuration注释表示这是一个配置类 , 我们在这个类中注入RedisProperties , 并使用@Bean注释来指定JedisPool 。@Configurationpublic类RedisConfiguration { @ Autowired private re properties属性;@Bean公共JedisPool getJedisPool(){ JedisPoolConfig = new JedisPoolConfig();config . setmaxidle(properties . getjedis() 。getPool() 。getMaxIdle());config . setmaxtotal(properties . getjedis() 。getPool() 。getMaxActive());config . setmaxwaitmillis(properties . getjedis() 。getPool() 。getMaxWait() 。tomallis());JedisPool pool = new JedisPool(config , properties.getHost() , properties.getPort() , 100 , properties.getPassword() , properties . getdatabase());回流池;}}
08
接下来 , 我们编辑JedisUtil工具类 , 通过SpringBoot容器的@Component注解自动创建 , 注入JedisPool , 使用JedisPool.getResource()方法获取jedis , 最终实现redis数据库的操作 。代码如下 。@Componentpublic类Jedistil { @ Autowired Jedispool Jedispool;//获取公钥字符串get (string key)的值{ jedis jedis = jedisspool . getresource();string str = try { str = jedis . get(key);}最后{ try { jedis . close();} catch(异常e){ e . printstacktrace();} }返回字符串;}公共字符串集(String键 , String值){ Jedis Jedis = jedispool . getresource();string str = try { str = jedis . set(key , value);}最后{ try { jedis . close();} catch(异常e){ e . printstacktrace();} }返回字符串;}}
09
在编写了jedistil工具类之后 , 我们修改了之前的RedisController并注入了jedistil 。代码如下图所示 。然后 , 使用postman分别调用post和获取接口 , 可以看到新密钥的值被成功获取 。
特别提示
在Spring Boot中集成Redis之前 , 需要在本地安装Redis 。此外 , 您可以使用桌面管理工具RedisDesktopManager查看Redis中的数据 。

    推荐阅读