redis与redisson实现分布锁的两种方式

本文介绍了如何使用Spring Boot和Redisson库在StockController中实现分布式锁,确保股票减少操作的原子性和并发控制。通过Redisson的RLock实现加锁解锁,避免竞态条件。
摘要由CSDN通过智能技术生成

redis实现方式

package cn.ping.redislock.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class StockController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/stock/reduceStock")
    public String reduceStock(){

        String stockKey = "stock_key";

        //生成每个线路不一样的uuid值,防止其他线程删除key(key超时redis删除key,其他线程加锁)
        String stock_id = UUID.randomUUID().toString();

        Integer stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
        //如果key不存在,则保存到redis返回true,返回不保存返回false
        Boolean ifAbsent = stringRedisTemplate.opsForValue().setIfAbsent(stockKey, stock_id, 10, TimeUnit.SECONDS);

        if (!ifAbsent){
            return "error";
        }
        try {
            if (stock > 0) {
                stock = stock - 1;
                stringRedisTemplate.opsForValue().set("stock", stock + "");
                System.out.println("扣减成功:库存剩余为:" + stock);
            } else {
                System.out.println("扣减失败:库存不足");
            }
        }finally {
            //如果不判断,执行到超时了,就删除其他线程加的锁
            if (stringRedisTemplate.opsForValue().get(stockKey).equals(stock_id)){
                //如果刚好到这里超时了??? 给锁的超时续期来解决
                stringRedisTemplate.delete(stockKey);
            }
        }
        return "end";
    }
}

redisson实现方式:

mvn:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.ping</groupId>
    <artifactId>redis-lock</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redis-lock</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.6.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bean注入:

@SpringBootApplication
public class RedisLockApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisLockApplication.class, args);
    }

    @Bean
    public Redisson redisson(){
        Config config = new Config();
        //使用单例redis
        config.useSingleServer().setAddress("http://localhost:6379").setDatabase(0);
        return (Redisson)Redisson.create(config);
    }
}

加锁解锁:

package cn.ping.redislock.controller;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class StockController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Redisson redisson;

    @GetMapping("/stock/reduceStock")
    public String reduceStock(){

        String stockKey = "stock_key";

        Integer stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
        //获取一个锁对象
        RLock lock = redisson.getLock(stockKey);
        try {
            lock.lock(10,TimeUnit.SECONDS);//加锁
            if (stock > 0) {
                stock = stock - 1;
                stringRedisTemplate.opsForValue().set("stock", stock + "");
                System.out.println("扣减成功:库存剩余为:" + stock);
            } else {
                System.out.println("扣减失败:库存不足");
            }
        }finally {
            lock.unlock();//解锁
           
        }
        return "end";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值