redis分布式锁实现方案,redis分布式锁实现方式

  redis分布式锁实现方案,redis分布式锁实现方式

  

目录

1、业务场景引入2、基础环境准备2.1.准备库存数据库2.2.创建跳羚工程,pom.xml中导入依赖,请注意版本2.3 .应用程序.属性配置文件2.4 .跳靴启动类2.5.添加使用心得的配置类2.6.pojo层2.7 .映射器层2.8 .跳靴监听网启动事件,加载商品数据到使用心得中3、Redis实现分布式锁3.1 分布式锁的实现类3.2 分布式锁的业务代码4、分布式锁测试总结

 

  

1、业务场景引入

模拟一个电商系统,服务器分布式部署,系统中有一个用户下订单的接口,用户下订单之前需要获取分布式锁,然后去检查一下库存,确保库存足够了才会给用户下单,然后释放锁。

 

  由于系统有一定的并发,所以会预先将商品的库存保存在存储中,用户下单的时候会更新存储的库存。

  

2、基础环境准备

 

  

2.1.准备库存数据库

--t _商品的表结构- 如果存在" t _ goods ",则删除表;CREATE TABLE ` t _ goods `(` goods _ id ` int(11)NOT NULL AUTO _ INCREMENT,` goods _ name ` varchar(255)DEFAULT NULL,` goods_price` decimal(10,2) DEFAULT NULL,` goods_stock` int(11) DEFAULT NULL,` goods _ img ` varchar(255)DEFAULT NULL,PRIMARY KEY(` goods _ id `))ENGINE=InnoDB AUTO _ INCREMENT=4 DEFAULT CHARSET=utf8;- -t_goods的记录- 插入 t _商品值( 1 , iphone8 , 6999.00 , 5000 , img/iphone。jpg’);插入 t _商品值( 2 ,小米9 , 3000.00 , 5000 , img/荣耀。jpg’);插入 t _商品值( 3 ,华为p30 , 4000.00 , 5000 , img/华为。jpg’);

 

  

2.2.创建SpringBoot工程,pom.xml中导入依赖,请注意版本。

?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 http://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本groupIdcom.springlock.task/groupIdartifactIdspringlock.task/artifactId版本1.0-快照/版本!- 导入跳羚的父工程把系统中的版本号做了一些定义!-父groupIdorg.springframework.boot/groupId人工弹簧-启动-母公司/version2.1.3.RELEASE/version/parent人工弹簧依赖项!- 导入跳羚的网场景启动器网相关的包导入!-

 

  gt; <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--导入MyBatis的场景启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <!--SpringBoot单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--导入Lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--Spring Data Redis 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies> <build> <!--编译的时候同时也把包下面的xml同时编译进去--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build></project>

 

  

2.3.application.properties配置文件

# SpringBoot有默认的配置,我们可以覆盖默认的配置server.port=8888# 配置数据的连接信息spring.datasource.url=jdbc:mysql://127.0.0.1:3306/redislock?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.type=com.alibaba.druid.pool.DruidDataSource# reids配置spring.redis.jedis.pool.max-idle=10spring.redis.jedis.pool.min-idle=5spring.redis.jedis.pool.maxTotal=15spring.redis.hostName=192.168.3.28spring.redis.port=6379

 

  

2.4.SpringBoot启动类

/** * @author swadian * @date 2022/3/4 * @Version 1.0 * @describetion */@SpringBootApplicationpublic class SpringLockApplicationApp { public static void main(String[] args) { SpringApplication.run(SpringLockApplicationApp.class,args); }}

 

  

2.5.添加Redis的配置类

import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;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;@Slf4j@Configurationpublic class RedisConfig { /** * 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置 * @ConfigurationProperties:会将前缀相同的内容创建一个实体。 */ @Bean @ConfigurationProperties(prefix="spring.redis.jedis.pool") public JedisPoolConfig jedisPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); log.info("JedisPool默认参数-最大空闲数:{},最小空闲数:{},最大链接数:{}",config.getMaxIdle(),config.getMinIdle(),config.getMaxTotal()); return config; } /** * 2.创建JedisConnectionFactory:配置redis链接信息 */ @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){ log.info("redis初始化配置-最大空闲数:{},最小空闲数:{},最大链接数:{}",config.getMaxIdle(),config.getMinIdle(),config.getMaxTotal()); JedisConnectionFactory factory = new JedisConnectionFactory(); //关联链接池的配置对象 factory.setPoolConfig(config); return factory; } /** * 3.创建RedisTemplate:用于执行Redis操作的方法 */ @Bean public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); //关联 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); //指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); //值采用json序列化 template.setValueSerializer(jacksonSeial); //设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); return template; }}

 

  

2.6.pojo层

import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Goods { private String goods_id; private String goods_name; private Double goods_price; private Long goods_stock; private String goods_img;}

 

  

2.7.mapper层

import com.springlock.pojo.Goods;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;@Mapperpublic interface GoodsMapper { /** * 01-更新商品库存 * @param goods * @return */ @Update("update t_goods set goods_stock=#{goods_stock} where goods_id=#{goods_id}") Integer updateGoodsStock(Goods goods); /** * 02-加载商品信息 * @return */ @Select("select * from t_goods") List<Goods> findGoods(); /** * 03-根据ID查询 * @param goodsId * @return */ @Select("select * from t_goods where goods_id=#{goods_id}") Goods findGoodsById(String goodsId);}

 

  

2.8.SpringBoot监听Web启动事件,加载商品数据到Redis中

import com.springlock.mapper.GoodsMapper;import com.springlock.pojo.Goods;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationListener;import org.springframework.context.annotation.Configuration;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.data.redis.core.RedisTemplate;import javax.annotation.Resource;import java.util.List;/** * @author swadian * @date 2022/3/4 * @Version 1.0 * @describetion ApplicationListener监听器,项目启动时出发 */@Slf4j@Configurationpublic class ApplicationStartListener implements ApplicationListener<ContextRefreshedEvent> { @Resource GoodsMapper goodsMapper; @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("Web项目启动,ApplicationListener监听器触发..."); List<Goods> goodsList = goodsMapper.findGoods(); for (Goods goods : goodsList) { redisTemplate.boundHashOps("goods_info").put(goods.getGoods_id(), goods.getGoods_stock()); log.info("缓存商品详情:{}",goods); } }}

 

  

3、Redis实现分布式锁

 

  

3.1 分布式锁的实现类

import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.Transaction;import redis.clients.jedis.exceptions.JedisException;import java.util.List;import java.util.UUID;public class DistributedLock { //redis连接池 private static JedisPool jedisPool; static { JedisPoolConfig config = new JedisPoolConfig(); // 设置最大连接数 config.setMaxTotal(200); // 设置最大空闲数 config.setMaxIdle(8); // 设置最大等待时间 config.setMaxWaitMillis(1000 * 100); // 在borrow一个jedis实例时,是否需要验证,若为true,则所有jedis实例均是可用的 config.setTestOnBorrow(true); jedisPool = new JedisPool(config, "192.168.3.28", 6379, 3000); } /** * 加锁 * @param lockName 锁的key * @param acquireTimeout 获取锁的超时时间 * @param timeout 锁的超时时间 * @return 锁标识 * Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。 * 设置成功,返回 1 。 设置失败,返回 0 。 */ public String lockWithTimeout(String lockName, long acquireTimeout, long timeout) { Jedis conn = null; String retIdentifier = null; try { // 获取连接 conn = jedisPool.getResource(); // value值->随机生成一个String String identifier = UUID.randomUUID().toString(); // key值->即锁名 String lockKey = "lock:" + lockName; // 超时时间->上锁后超过此时间则自动释放锁 毫秒转成->秒 int lockExpire = (int) (timeout / 1000); // 获取锁的超时时间->超过这个时间则放弃获取锁 long end = System.currentTimeMillis() + acquireTimeout; while (System.currentTimeMillis() < end) { //在获取锁时间内 if (conn.setnx(lockKey, identifier) == 1) {//设置锁成功 conn.expire(lockKey, lockExpire); // 返回value值,用于释放锁时间确认 retIdentifier = identifier; return retIdentifier; } // ttl以秒为单位返回 key 的剩余过期时间,返回-1代表key没有设置超时时间,为key设置一个超时时间 if (conn.ttl(lockKey) == -1) { conn.expire(lockKey, lockExpire); } try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } catch (JedisException e) { e.printStackTrace(); } finally { if (conn != null) { conn.close(); } } return retIdentifier; } /** * 释放锁 * @param lockName 锁的key * @param identifier 释放锁的标识 * @return */ public boolean releaseLock(String lockName, String identifier) { Jedis conn = null; String lockKey = "lock:" + lockName; boolean retFlag = false; try { conn = jedisPool.getResource(); while (true) { // 监视lock,准备开始redis事务 conn.watch(lockKey); // 通过前面返回的value值判断是不是该锁,若是该锁,则删除,释放锁 if (identifier.equals(conn.get(lockKey))) { Transaction transaction = conn.multi();//开启redis事务 transaction.del(lockKey); List<Object> results = transaction.exec();//提交redis事务 if (results == null) {//提交失败 continue;//继续循环 } retFlag = true;//提交成功 } conn.unwatch();//解除监控 break; } } catch (JedisException e) { e.printStackTrace(); } finally { if (conn != null) { conn.close(); } } return retFlag; }}

 

  

3.2 分布式锁的业务代码

service业务逻辑层

 

  

public interface SkillService { Integer seckill(String goodsId,Long goodsStock);}

service业务逻辑层实现层

 

  

import com.springlock.lock.DistributedLock;import com.springlock.mapper.GoodsMapper;import com.springlock.pojo.Goods;import com.springlock.service.SkillService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Slf4j@Servicepublic class SkillServiceImpl implements SkillService { private final DistributedLock lock = new DistributedLock(); private final static String LOCK_NAME = "goods_stock_resource"; @Resource GoodsMapper goodsMapper; @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public Integer seckill(String goodsId, Long goodsQuantity) { // 加锁,返回锁的value值,供释放锁时候进行判断 String identifier = lock.lockWithTimeout(LOCK_NAME, 5000, 1000); Integer goods_stock = (Integer) redisTemplate.boundHashOps("goods_info").get(goodsId); if (goods_stock > 0 && goods_stock >= goodsQuantity) { //1.查询数据库对象 Goods goods = goodsMapper.findGoodsById(goodsId); //2.更新数据库中库存数量 goods.setGoods_stock(goods.getGoods_stock() - goodsQuantity); goodsMapper.updateGoodsStock(goods); //3.同步Redis中商品库存 redisTemplate.boundHashOps("goods_info").put(goods.getGoods_id(), goods.getGoods_stock()); log.info("商品Id:{},在Redis中剩余库存数量:{}", goodsId, goods.getGoods_stock()); //释放锁 lock.releaseLock(LOCK_NAME, identifier); return 1; } else { log.info("商品Id:{},库存不足!,库存数:{},购买量:{}", goodsId, goods_stock, goodsQuantity); //释放锁 lock.releaseLock(LOCK_NAME, identifier); return -1; } }}

controller层

 

  

import com.springlock.service.SkillService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@Scope("prototype") //prototype 多实例,singleton单实例public class SkillController { @Autowired SkillService skillService; @RequestMapping("/skill") public String skill() { Integer count = skillService.seckill("1", 1L); return count > 0 ? "下单成功" + count : "下单失败" + count; }}

 

  

4、分布式锁测试

把SpringBoot工程启动两台服务器,端口分别为8888、9999。启动8888端口后,修改配置文件端口为9999,启动另一个应用

 

  

 

  然后使用jmeter进行并发测试,开两个线程组,分别代表两台服务器下单,1秒钟起20个线程,循环25次,总共下单1000次。

  

 

  查看控制台输出:

  

 

  注意:该锁在并发量太高的情况下,会出现一部分失败率。手动写的程序,因为操作的非原子性,会存在并发问题。该锁的实现只是为了演示原理,并不适用于生产。

  

 

  jmeter聚合报告

  

 

  

 

  

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注盛行IT的更多内容!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • 关于redis数据库入门详细介绍图片,redis数据库的使用,关于Redis数据库入门详细介绍
  • redis队列操作命令,redis 循环队列
  • redis队列操作命令,redis 循环队列,redis实现简单队列
  • redis部署应用服务器上,redis如何启动服务器
  • redis部署应用服务器上,redis如何启动服务器,搭建Redis服务器步骤详细介绍
  • redis缓存穿透和击穿解决方案,redis缓存穿透,缓存雪崩解决
  • redis缓存穿透和击穿解决方案,redis缓存穿透,缓存雪崩解决,redis缓存穿透解决方法
  • Redis缓存,redis和缓存
  • Redis缓存,redis和缓存,Redis缓存详解
  • redis的配置,启动,操作和关闭方法有哪些,关闭redis的命令,Redis的配置、启动、操作和关闭方法
  • redis的主从配置方法详解图,Redis主从配置
  • redis的主从配置方法详解图,Redis主从配置,redis的主从配置方法详解
  • redis界面工具,mac安装redis可视化工具
  • redis界面工具,mac安装redis可视化工具,推荐几款 Redis 可视化工具(太厉害了)
  • redis正确使用的十个技巧是什么,redis正确使用的十个技巧有哪些
  • 留言与评论(共有 条评论)
       
    验证码: