【Spring Boot 3】【Redis】分布式锁

【Spring Boot 3】【Redis】分布式锁

背景

软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时间精力。因此本文旨在通过一篇文章即能还原出可工作的、甚至可用于生产的DEMO,期望初学者能尽快地迈过0到1的这一步骤,并在此基础上不断深化对相关知识的理解。

为达以上目的,本文会将开发环境、工程目录结构、开发步骤及源码尽量全面地展现出来,文字描述能简则简,能用代码注释的绝不在正文中再啰嗦一遍,正文仅对必要且关键的信息做重点描述。

介绍

本文介绍Spring Boot + Redis实现分布式锁。

开发环境

分类 名称 版本
操作系统 Windows Windows 11
JDK Oracle JDK 21.0.1
IDE IntelliJ IDEA 2023.2.4
构建工具 Apache Maven 3.9.3
缓存 Redis 7.2

开发步骤及源码

1> 创建Maven工程,添加依赖。

    <properties>
        <spring-boot.version>3.2.1</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2> 添加应用配置(src/main/resources/application.yml)。

spring:
  data:
    redis:
      # 连接地址
      host: 127.0.0.1
      # 端口
      port: 6379
      # Redis数据库索引,默认为 0
      database: 0
      # 用户名(可选)
      # username:
      # 密码(可选)
      # password:
      # 连接超时
      connect-timeout: 5000
      # 读超时
      timeout: 5000
      # Lettuce 客户端配置
      lettuce:
        # 连接池配置
        pool:
          # 最小空闲连接
          min-idle: 0
          # 最大空闲连接
          max-idle: 8
          # 最大活跃连接
          max-active: 8
          # 从连接池获取连接最大超时时间,小于等于 0 则表示不会超时
          max-wait: -1ms

3> 创建Redis配置类,自定义 org.springframework.data.redis.core.RedisTemplate Bean实例。

package com.jiyongliang.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        // 设置 key 的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置 value 的序列化方式
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 设置 hash 中 key 的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // 设置 hash 中 value 的序列化方式
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

4> 创建分布式锁服务类。

package com.jiyongliang.springboot.service;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Service
public class DistributedLockService {

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取锁
     *
     * @param key     锁的键
     * @param value   锁的值
     * @param timeout 超时时间
     * @return 是否成功获取锁
     */
    public boolean tryLock(String key, String value, long timeout) {
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS));
    }

    /**
     * 释放锁
     *
     * @param key   锁的键
     * @param value 锁的值
     * @return 是否成功释放锁
     */
    public boolean unlock(String key, String value) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        Object currentValue = ops.get(key);
        if (currentValue != null && Objects.equals(value, currentValue)) {
            Boolean result = redisTemplate.delete(key);
            return Boolean.TRUE.equals(result);
        }
        return false;
    }
}

5> 创建单元测试。

package com.jiyongliang.springboot.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

@SpringBootTest
class DistributedLockServiceTests {

    @Autowired
    DistributedLockService distributedLockService;

    @Test
    void test() throws InterruptedException {
        Assertions.assertThat(distributedLockService.tryLock("lock1", "A", 3)).isTrue();
        Assertions.assertThat(distributedLockService.tryLock("lock1", "A", 3)).isFalse();
        Assertions.assertThat(distributedLockService.unlock("lock1", "A")).isTrue();
        Assertions.assertThat(distributedLockService.tryLock("lock1", "A", 3)).isTrue();
        TimeUnit.SECONDS.sleep(3);
        Assertions.assertThat(distributedLockService.tryLock("lock1", "A", 3)).isTrue();
    }
}

6> 定义SpringBoot应用启动类。

package com.jiyongliang.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBoot3RedisDistributedLockApplication {

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

7> 单元测试结果

工程目录结构

总结

注意一定要添加 jackson-databind 依赖,否则会报错:threw exception with message: com/fasterxml/jackson/databind/jsontype/TypeResolverBuilder

相关推荐
2401_854391085 分钟前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
杨荧33 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
2401_8576100342 分钟前
SpringBoot实现:校园资料分享平台开发指南
服务器·spring boot·php
这孩子叫逆1 小时前
Spring Boot项目的创建与使用
java·spring boot·后端
Jay_fearless2 小时前
Redis SpringBoot项目学习
spring boot·redis
Wang's Blog2 小时前
Redis: 集群环境搭建,集群状态检查,分析主从日志,查看集群信息
数据库·redis
coderWangbuer2 小时前
基于springboot的高校招生系统(含源码+sql+视频导入教程+文档+PPT)
spring boot·后端·sql
Kenny.志2 小时前
2、Spring Boot 3.x 集成 Feign
java·spring boot·后端
sky丶Mamba3 小时前
Spring Boot中获取application.yml中属性的几种方式
java·spring boot·后端
千里码aicood4 小时前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统