实现原理:

因为redis是单线程的,当多个线程同时访问redis时,是顺序的一个一个访问,故可以借助redis来实现分布式锁。

获得锁:

释放锁:

具体实现(codeing):

根据:SET resource_name my_random_value NX PX 30000

1、新建一个springboot项目(自己新建就行,这里给出pom.xml文件)

<?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.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xpf</groupId>
    <artifactId>distribute_lock</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>distribute_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-data-jpa</artifactId>
        </dependency>
        <!--连接redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- Slf4j -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!--Mybatis代码自动生成器-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId> mysql</groupId>
                        <artifactId> mysql-connector-java</artifactId>
                        <version>8.0.28</version>
                        <!--  <version> 5.1.39</version>  -->
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 自动生成的配置 -->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

2、application.properties (根据自己的redis配置,大部分应该没有设置密码,这里其实只要配置redis就行,mysql并没有用到)

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=Asia/Shanghai&useSSL=false

mybatis.mapper-locations=/com.xpf.distributelock.dao/*.xml

spring.redis.host=192.168.217.129
spring.redis.client-name=root
spring.redis.password=123456

3、RedisLockController控制类

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @auther xpf
 * @date 2022/6/9 9:00
 * @description 使用redis实现分布式锁
 */
@RestController
@Slf4j
@RequestMapping("redis")
public class RedisLockController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("redisLock")
    public String redisLock(){
        log.info("我进入了方法!");
        String key = "redisKey";
        String value = UUID.randomUUID().toString();

        RedisCallback<Boolean> redisCallback = connection -> {
            //设置NX
            RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent();
            //设置过期时间
            Expiration expiration = Expiration.seconds(30);
            //序列化key
            byte[] redisKey = redisTemplate.getKeySerializer().serialize(key);
            //序列化value
            byte[] redisValue = redisTemplate.getValueSerializer().serialize(value);

            //执行setnx操作
            Boolean result = connection.set(redisKey, redisValue, expiration, setOption);
            return result;
        };

        //获取分布式锁
        Boolean lock = (Boolean) redisTemplate.execute(redisCallback);
        if (lock){
            log.info("我进入了锁!!");
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                log.info(e.getMessage());
            }finally {
                String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n" +
                        "    return redis.call(\"del\",KEYS[1])\n" +
                        "else\n" +
                        "    return 0\n" +
                        "end";
                RedisScript<Boolean> redisScript = RedisScript.of(script, Boolean.class);
                List<String> keys = Arrays.asList(key);

                Boolean result = (Boolean) redisTemplate.execute(redisScript, keys, value);

                log.info("释放锁的结果:" + result);
            }
        }
        log.info("方法执行完毕!");
        return "方法执行完毕!";
    }
}

4、启动springboot项目,然后配置一个8081端口,在启动一个,模拟分布式

 5、先访问8080,http://127.0.0.1:8081/redis/redisLock

 立马(15s以内)在访问8081,http://127.0.0.1:8081/redis/redisLock

 先访问8080,进入方法,获得锁,睡眠15s后,释放锁,方法执行完毕

8081访问,进入方法,因为获取不到锁,进入不了if语块,直接打印日志。

说名接住redis实现分布式锁成功!!

代码优化

虽然上述已经实现了redis的分布式锁,但是有大片代码都在获取锁和释放锁,应该把这些代码抽取出来,和业务分离。优化代码如下:

import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.core.types.Expiration;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @auther xpf
 * @date 2022/6/10 11:15
 * @description 封装redis分布式锁
 */
public class RedisLock implements AutoCloseable {

    private String key;
    private String value;
    private int expiration;
    private RedisTemplate redisTemplate;

    public RedisLock(String key, int expiration, RedisTemplate redisTemplate) {
        this.key = key;
        this.value = UUID.randomUUID().toString();
        this.expiration = expiration;
        this.redisTemplate = redisTemplate;
    }

    public Boolean getLock(){
        RedisCallback<Boolean> redisCallback = connection -> {
            //设置NX
            RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent();
            //设置过期时间
            Expiration expiration1 = Expiration.seconds(expiration);
            //序列化key
            byte[] redisKey = redisTemplate.getKeySerializer().serialize(key);
            //序列化value
            byte[] redisValue = redisTemplate.getValueSerializer().serialize(value);

            //执行setnx操作
            Boolean result = connection.set(redisKey, redisValue, expiration1, setOption);
            return result;
        };

        //获取分布式锁
        Boolean lock = (Boolean) redisTemplate.execute(redisCallback);
        return lock;
    }

    public Boolean unLock(){
        String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n" +
                "    return redis.call(\"del\",KEYS[1])\n" +
                "else\n" +
                "    return 0\n" +
                "end";
        RedisScript<Boolean> redisScript = RedisScript.of(script, Boolean.class);
        List<String> keys = Arrays.asList(key);

        Boolean result = (Boolean) redisTemplate.execute(redisScript, keys, value);
        return result;
    }

    /**
     * jdk1.7新特性:实现 AutoCloseable ,就可以不用在finally里面写释放锁的代码了
     * @throws Exception
     */
    @Override
    public void close() throws Exception {
        unLock();
    }
}

上述案例优化后:

/**
 * @auther xpf
 * @date 2022/6/9 9:00
 * @description 使用redis实现分布式锁(配合封装类RedisLock)
 */
@RestController
@Slf4j
@RequestMapping("redis2")
public class RedisLockController2 {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("redisLock2")
    public String redisLock(){
        log.info("我进入了锁!!");
//        RedisLock redisLock = new RedisLock("redisKey", 30, redisTemplate);

        try(RedisLock redisLock = new RedisLock("redisKey", 30, redisTemplate)) {
            if (redisLock.getLock()) {
                log.info("我进入了锁!");
                Thread.sleep(15000);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }/*finally {
            Boolean result = redisLock.unLock();
            log.info("释放锁的结果:" + result);
        }*/
        
        log.info("方法执行完毕!");
        return "方法执行完毕!";

    }
}

优化完毕,是不是比之前清晰特别多,而且业务也清晰可见。(这里使用了jdk1.7新特性try(){},可自行了解下)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐