常用:SpringBoot集成Redis
微服务写法及Redis用法:多服务情况下,Spring集成Redis写法:①Redis工具类,②RedisConfig配置类,③引导文件写在Common服务,其他服务需要Redis功能,需要先在自个服务的pom引入Common服务依赖和配置Redis配置后就可以直接Autowired Redis使用Redis功能了1.引入依赖(微服务redis 依赖写在 common 的 pom业务服务只引入 common 即可不用重复导入 redis 依赖)pom.xml添加Redis依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId /dependency或者使用快速面部添加Redis依赖:NoSQL---Spring Data Redis (ccessADriver)2.配置文件application.yml注:3.x和2.x版本的SpringBoot的Redis配置不一样,此处为3.x版本的## redis spring boot 3.x ## Spring: data: redis: host: localhost port: 6379 timeout: 60s # 连接空闲超过N(s秒、ms毫秒)后关闭0为禁用这里配置值和tcp-keepalive值一致 lettuce: # 默认使用 lettuce 连接池 pool: max-active: 8 # 允许最大连接数默认8负值表示没有限制 max-idle: 8 # 最大空闲连接数默认8 min-idle: 0 # 最小空闲连接数 max-wait: 5s # 连接用完时新的请求等待时间(s秒、ms毫秒)超过该时间抛出异常 JedisConnectionException(默认-1负值表示没有限制)3.封装Redis工具类(可直接复制,不需要每次都写)package com.bite.common.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.concurrent.TimeUnit; Slf4j //Component --单服务可用注解自动注入Spring,微服务需手动注入Spring public class Redis { private StringRedisTemplate redisTemplate; private static final String REDIS_SPLIT:; private static final String REDIS_DEFAULT_PREFIXdefault; public Redis(StringRedisTemplate redisTemplate) { this.redisTemplate redisTemplate; } /** * redis get操作 */ public String get(String key) { try{ return redisTemplate.opsForValue().get(key); }catch (Exception e){ log.error(redis get error,key:{},key); return null; } } /** * hasKey * true -存在 false -不存在 */ public boolean hasKey(String key) { try{ return keynull?false:redisTemplate.hasKey(key); }catch (Exception e){ log.error(redis hasKey error,key:{},key); return false; } } /** * set key */ public boolean set(String key,String value) { try { // 参数校验key不能为null、不能是空字符串、不能全空格 if (key null || key.isBlank()) { log.error(redis set errorkey不能为空); return false; } // value可以为空字符串业务允许空值存储只禁止null if (value null) { log.error(redis set errorvalue不能为null); return false; } redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { log.error(redis set error,key:{}, value:{},e:{}, key, value,e); return false; } } /** * set key,设置过期时间,单位为秒 */ public boolean set (String key, String value,long timeout){ try { // 参数校验key不能为null、不能是空字符串、不能全空格 if (key null || key.isBlank()) { log.error(redis set errorkey不能为空); return false; } // value可以为空字符串业务允许空值存储只禁止null if (value null) { log.error(redis set errorvalue不能为null); return false; } if (timeout 0) { redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { log.error(redis set error,key:{}, value:{},e:{}, key, value,e); return false; } } //封装key,统一前缀 public String buildKey(String prefix,String... args) { if(prefixnull){ prefixREDIS_DEFAULT_PREFIX; } StringBuilder keynew StringBuilder(); key.append(prefix); if(args!null){ for(String arg:args){ key.append(REDIS_SPLIT).append(arg); } } return key.toString(); } }4.Redis 的自动装配配置类(RedisConfig 负责把 Redis 工具实例化、注入进 Spring 容器解决无法直接 Autowired 的问题)package com.bite.common.config; import com.bite.common.utils.Redis; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.StringRedisTemplate; Configuration public class RedisConfig { //Conditional扩展,需要该服务对应的配置里符合这个条件才可以使用这个注入的Bean //只有配置文件中存在spring.data.redis.host配置项时 // 才会把 Redis 工具类注册为 Bean 放入 Spring 容器 // 没配置该属性就不创建这个 Bean防止未配置 Redis 时报错 //如blog服务配置没有redis相关配置,所以该服务就不创建这个bean,而user服务则有这个redis配置,则创建这个Bean ⭐ ConditionalOnProperty(prefix spring.data.redis,name host) Bean public Redis redis(StringRedisTemplate stringRedisTemplate) { return new Redis(stringRedisTemplate); } }手动注入 Spring 对象模板: Configuration public class XxxConfig { (ConditionalOnProperty条件注解) Bean public 你的类名 方法名(依赖对象 变量名) { return new 你的类名(依赖对象); } }5.创建引导文件:resources包-META-INF包-spring包-org.springframework.boot.autoconfigure.AutoConfiguration.imports文件在这个文件配置我们希望交给Spring管理的对象,也就是这里的RedisConfig,写其路径com.bite.common.config.RedisConfig单服务写法:只用到1.2.3步,且与微服务写法一致,区别就是在第三部加上Component 注解pom 引入 redis 依赖yml 配置 redis 连接Redis 工具类上加 Component 注解