您好,欢迎访问一九零五行业门户网

SpringBoot中如何使用Redis作为全局锁

一、模拟没有锁情况下的资源竞争public class commonconsumerservice { //库存个数 static int goodscount = 900; //卖出个数 static int salecount = 0; public static void main(string[] args) throws interruptedexception { for (int i = 0; i < 1000; i++) { new thread(() -> { try {thread.sleep(2);} catch (interruptedexception e) {} if (goodscount > 0) { goodscount--; system.out.println("剩余库存:" + goodscount + " 卖出个数" + ++salecount); } }).start(); } thread.sleep(3000); }}
运行一次,最后几行的输出结果如下,很明显出错了,剩余0个商品却只卖出了899个商品,很明显有商品被某个线程私吞了。
...
剩余库存:5 卖出个数893
剩余库存:5 卖出个数894
剩余库存:4 卖出个数895
剩余库存:2 卖出个数896
剩余库存:2 卖出个数897
剩余库存:1 卖出个数898
剩余库存:0 卖出个数899
二、使用redis加锁redis是单线程的,串行执行,那么接下来使用redis为资源进行加锁。
1.首先引入依赖
compile "org.springframework.boot:spring-boot-starter-data-redis"
2.引入redis加锁工具类
package com.kingboy.common.utils;import redis.clients.jedis.jedis;import java.util.collections;/** * @author kingboy--kingboyworld@163.com * @date 2017/12/29 下午1:57 * @desc redis工具. */public class redistool { private static final string lock_success = "ok"; private static final string set_if_not_exist = "nx"; private static final string set_with_expire_time = "px"; private static final long release_success = 1l; /** * 尝试获取分布式锁 * @param jedis redis客户端 * @param lockkey 锁 * @param requestid 请求标识 * @param expiretime 超期时间 * @return 是否获取成功 */ public static boolean trygetdistributedlock(jedis jedis, string lockkey, string requestid, int expiretime) { string result = jedis.set(lockkey, requestid, set_if_not_exist, set_with_expire_time, expiretime); if (lock_success.equals(result)) { return true; } return false; } /** * 释放分布式锁 * @param jedis redis客户端 * @param lockkey 锁 * @param requestid 请求标识 * @return 是否释放成功 */ public static boolean releasedistributedlock(jedis jedis, string lockkey, string requestid) { string script = "if redis.call('get', keys[1]) == argv[1] then return redis.call('del', keys[1]) else return 0 end"; object result = jedis.eval(script, collections.singletonlist(lockkey), collections.singletonlist(requestid)); if (release_success.equals(result)) { return true; } return false; }}
3.将上面没有锁的示例代码改编如下:
public class redislockconsumerservice { //库存个数 static int goodscount = 900; //卖出个数 static int salecount = 0; @sneakythrows public static void main(string[] args) { jedispool jedispool = new jedispool(new jedispoolconfig(), "192.168.0.130", 6379, 1000); for (int i = 0; i < 1000; i++) { new thread(() -> { try {thread.sleep(2);} catch (interruptedexception e) {} jedis jedis = jedispool.getresource(); boolean lock = false; while (!lock) { lock = redistool.trygetdistributedlock(jedis, "goodscount", thread.currentthread().getname(), 10); } if (lock) { if (goodscount > 0) { goodscount--; system.out.println("剩余库存:" + goodscount + " 卖出个数" + ++salecount); } } redistool.releasedistributedlock(jedis, "goodscount", thread.currentthread().getname()); jedis.close(); }).start(); } thread.sleep(3000); jedispool.close(); }}
执行几次程序输出结果如下,可以看到结果是有序,并且正确的。
...
剩余库存:6 卖出个数894
剩余库存:5 卖出个数895
剩余库存:4 卖出个数896
剩余库存:3 卖出个数897
剩余库存:2 卖出个数898
剩余库存:1 卖出个数899
剩余库存:0 卖出个数900
以上就是springboot中如何使用redis作为全局锁的详细内容。
其它类似信息

推荐信息