springboot整合redis,推荐整合和使用案例(2021版)

您所在的位置:网站首页 redis存储空间快满了会自动整合吗 springboot整合redis,推荐整合和使用案例(2021版)

springboot整合redis,推荐整合和使用案例(2021版)

2024-07-11 23:03| 来源: 网络整理| 查看: 265

背景:手下新人在初次使用springboot整合redis,大部分人习惯从网上检索到一份配置,然后不知其所以然的复制粘贴到项目中,网上搜索到的配置良莠不齐但又万变不离其宗。由于springboot最大化地简化了整合redis需要的配置,在用户只需要在配置文件(application.*)中配置少量参数就可以使用官方默认提供的RedisTemplate和StringRedisTemplate来操作redis。由于官方提供的*RedisTemplate提供的功能有限,难以针对java的复杂数据类型进行序列化,且采用直连的方式以及没有对连接数进行限制等诸多因素在现代引用中制约较大,所以项目中一般需要提供一个RedisConfig类来针对redisTemplate做进一步配置。在此把自己之前项目中用到的一份基于redis的配置和封装类贴出来,以便抛砖引玉!

假定熟悉springboot的常见用法和配置; 假定你熟悉并掌握redis的六种数据结构和相关命令,否则难以理解redisTemplate API中方法和redis命令的对应关系 本篇博客以下内容,本人亲测可用,有需要的小伙伴直接复制粘贴即可。

下面开始编码:

1、新建一个springboot项目,pom.xml做如下依赖,注意jedis版本与spring-boot-starter-data-redis版本对应,一般来说2.1.x对应jedis 2.9.x,2.2.x对应jedis 3.x,切记,否则可能出现莫名其妙的错误。

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE thinking-in-spring-boot first-app-by-gui 0.0.1-SNAPSHOT first-app-by-gui Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis redis.clients jedis 3.2.0 org.projectlombok lombok org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

新建的项目结构如下:

2、在application.yml配置文件中新增关于redis配置,内容如下:

spring: redis: database: 0 host: 127.0.0.1 port: 6379 password: # 如果未单独配置默认为空即可 timeout: 1000 jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0

3、编写redis配置类,内容如下,在该类中完成Jedis池、Redis连接和RedisTemplate序列化三个配置完成springboot整合redis的进一步配置。其中RedisTemplate对key和value的序列化类,各人结合自己项目情况进行选择即可。

package com.dongnao.config; import com.dongnao.cache.IGlobalCache; import com.dongnao.cache.impl.AppRedisCacheManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @EnableCaching @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.database}") private Integer database; @Value("${spring.redis.port}") private Integer port; @Value("${spring.redis.password}") private String pwd; @Primary @


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3