框架学习第三篇:手把手带你在springboot项目中整合redis集群

    技术2026-01-11  11

    一、概述

    博客连载中,想要学习第二篇请点这里,本篇文章中我不会讲redis的体系结构或数据结构相关的内容,也不会带你搭建相关的redis cluster环境,针对的重点是应用(也就是你工作干活的部分),如果想深度了解redis的工作原理,请自行找资料进行学习,废话不多说(万能三部曲),肝就完了。

    二、springboot项目中整合redis cluster

    2.1 在pom文件中引入依赖

    <!--redis依赖配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

    2.2 在application.yml中添加配置(主要关注redis的配置)

    server: port: 8888 spring: datasource: url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: xsy123 redis: ##主要关注redis的配置 host: 192.168.56.155 #redis主机ip地址 database: 0 #连接指定数据库,默认为0,可以指定连接其其它数据库 port: 6380 #端口 password: #密码,生产环境已经要设置 jedis: pool: max-active: 8 # 连接池最大连接数 max-wait: -1ms # 最大阻塞等待市场,-1表示不限制 max-idle: 8 # 连接池中最大的空闲连接 min-idle: 0 # 连接池中最小的空闲连接 timeout: 3000ms # 连接池超时时间(毫秒) cluster: ##集群配置 nodes: 192.168.56.155:6380,192.168.56.155:6381,192.168.56.155:6382 redis: ##自己定义的key,value key: prefix: authCode: "portal:authCode" expire: authCode: 120 mybatis: mapper-locations: - classpath:mapper/*.xml - classpath*:com/**/mapper/*.xml

    2.3 StringRedisTemplate的使用 接口:

    package com.rzx.mall.mallmylearning.service; public interface RedisService { void set(String key,String value); String get(String key); boolean exipre(String key,Long expire); void remove(String key); Long increment(String key,long delta); }

    实现:

    package com.rzx.mall.mallmylearning.service.impl; import com.rzx.mall.mallmylearning.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisServiceImpl implements RedisService { @Autowired private StringRedisTemplate stringRedisTemplate; @Override public void set(String key, String value) { stringRedisTemplate.opsForValue().set(key,value); } @Override public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } @Override public boolean exipre(String key, Long expired) { return stringRedisTemplate.expire(key,expired, TimeUnit.SECONDS); } @Override public void remove(String key) { stringRedisTemplate.delete(key); } @Override public Long increment(String key, long delta) { return stringRedisTemplate.opsForValue().increment(key,delta); } }

    2.4 下面通过一个简单的注册例子来运用一下

    业务接口:

    package com.rzx.mall.mallmylearning.service; import com.rzx.mall.mallmylearning.common.api.CommonResult; public interface UmsMemberService { /** * @Description 1.生产验证码,并将验证法送MQ * @Author xieshiyao@1218.com.cn * @Date 2020/7/4 18:11 * @Param * @Return * @Exception * */ CommonResult generateAuthCode(String telephone); /** * @Description 2.验证验证码 * @Author xieshiyao@1218.com.cn * @Date 2020/7/4 18:12 * @Param * @Return * @Exception * */ CommonResult verifyAuthCode(String telephone,String authCode); }

    业务接口实现

    package com.rzx.mall.mallmylearning.service.impl; import com.rzx.mall.mallmylearning.common.api.CommonResult; import com.rzx.mall.mallmylearning.service.RedisService; import com.rzx.mall.mallmylearning.service.UmsMemberService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Random; @Service public class UmsMemberServiceImpl implements UmsMemberService { @Autowired private RedisService redisService; @Value("${redis.key.prefix.authCode}") private String REDIS_KEY_PREFIX_AUTH_CODE; @Value("${redis.key.expire.authCode}") private Long REDIS_KEY_EXPIRE_AUTH_CODE; @Override public CommonResult generateAuthCode(String telephone) { Random random = new Random(); StringBuffer authCodeBuffer = new StringBuffer(); for(int i=0;i<6;i++){ authCodeBuffer.append(random.nextInt(10)); } redisService.set(REDIS_KEY_PREFIX_AUTH_CODE+telephone,authCodeBuffer.toString()); redisService.exipre(REDIS_KEY_PREFIX_AUTH_CODE+telephone,REDIS_KEY_EXPIRE_AUTH_CODE); //在真实的线上环境,这里会将消息发到MQ中,另外一个服务会作为消费者消费MQ的消息,并调用第三方接口,将短信发送出去,我这里没这个环境就不实现这段逻辑了,相当于模拟一下 return CommonResult.success(authCodeBuffer.toString()+":发送成功"); } @Override public CommonResult verifyAuthCode(String telephone, String authCode) { String redisCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone); if(redisCode.equals(authCode)){ return CommonResult.success(null,"验证成功"); } return CommonResult.failed("验证码错误"); } }

    controller代码

    package com.rzx.mall.mallmylearning.service.impl; import com.rzx.mall.mallmylearning.common.api.CommonResult; import com.rzx.mall.mallmylearning.service.RedisService; import com.rzx.mall.mallmylearning.service.UmsMemberService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Random; @Service public class UmsMemberServiceImpl implements UmsMemberService { @Autowired private RedisService redisService; @Value("${redis.key.prefix.authCode}") private String REDIS_KEY_PREFIX_AUTH_CODE; @Value("${redis.key.expire.authCode}") private Long REDIS_KEY_EXPIRE_AUTH_CODE; @Override public CommonResult generateAuthCode(String telephone) { Random random = new Random(); StringBuffer authCodeBuffer = new StringBuffer(); for(int i=0;i<6;i++){ authCodeBuffer.append(random.nextInt(10)); } redisService.set(REDIS_KEY_PREFIX_AUTH_CODE+telephone,authCodeBuffer.toString()); redisService.exipre(REDIS_KEY_PREFIX_AUTH_CODE+telephone,REDIS_KEY_EXPIRE_AUTH_CODE); //在真实的线上环境,这里会将消息发到MQ中,另外一个服务会作为消费者消费MQ的消息,并调用第三方接口,将短信发送出去,我这里没这个环境就不实现这段逻辑了,相当于模拟一下 return CommonResult.success(authCodeBuffer.toString()+":发送成功"); } @Override public CommonResult verifyAuthCode(String telephone, String authCode) { String redisCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone); if(redisCode.equals(authCode)){ return CommonResult.success(null,"验证成功"); } return CommonResult.failed("验证码错误"); } }

    三、总结

    相信细心的小伙伴会发现,你上面为什么连接redis的时候没用密码,确实,线上的环境没用使用密码是大忌,各位在实际的上线环境一定要添加密码,我这里为了演示方便,就不搞那么复杂。另外线上环境建议大家都搭建集群吧,真的单机这玩意在这个年代确实有点不是很好的设计,如果你所在的公司是不在乎高可用环境,那自然无所谓,还是那句话线上的环境再怎么强调安全强调可用都不为过。

    Processed: 0.026, SQL: 9