SpringBoot中使用Redis作为全局锁示例过程

网友投稿 263 2022-08-18


SpringBoot中使用Redis作为全局锁示例过程

目录一、模拟没有锁情况下的资源竞争二、使用redis加锁

微服务的项目中,一个服务我们启动多份,在不同的进程中。这些服务是无状态的,而由数据存储容器(mysql/redis/es)进行状态数据的持久化。这就会导致资源竞争,出现多线程的问题。

一、模拟没有锁情况下的资源竞争

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.http://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--;

SXIpsIFCystem.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作为全局锁示例过程的详细内容,更多关于SpringBoot Redis全局锁的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java日志体系梳理分析示例
下一篇:java项目构建Gradle的使用教程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~