网站开发所需基础知识,杭州网站seo外包,淘宝客 wordpress主题,怎样用微信做购物网站【Spring Boot 3】【Redis】分布式锁 背景介绍开发环境开发步骤及源码工程目录结构总结 背景
软件开发是一门实践性科学#xff0c;对大多数人来说#xff0c;学习一种新技术不是一开始就去深究其原理#xff0c;而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经… 【Spring Boot 3】【Redis】分布式锁 背景介绍开发环境开发步骤及源码工程目录结构总结 背景
软件开发是一门实践性科学对大多数人来说学习一种新技术不是一开始就去深究其原理而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO这占用了我大量的时间精力。因此本文旨在通过一篇文章即能还原出可工作的、甚至可用于生产的DEMO期望初学者能尽快地迈过0到1的这一步骤并在此基础上不断深化对相关知识的理解。 为达以上目的本文会将开发环境、工程目录结构、开发步骤及源码尽量全面地展现出来文字描述能简则简能用代码注释的绝不在正文中再啰嗦一遍正文仅对必要且关键的信息做重点描述。
介绍
本文介绍Spring Boot Redis实现分布式锁。
开发环境
分类名称版本操作系统WindowsWindows 11JDKOracle JDK21.0.1IDEIntelliJ IDEA2023.2.4构建工具Apache Maven3.9.3缓存Redis7.2
开发步骤及源码
1 创建Maven工程添加依赖。 propertiesspring-boot.version3.2.1/spring-boot.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdversion${spring-boot.version}/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion${spring-boot.version}/versionscopetest/scope/dependency/dependencies2 添加应用配置src/main/resources/application.yml。
spring:data:redis:# 连接地址host: 127.0.0.1# 端口port: 6379# Redis数据库索引默认为 0database: 0# 用户名可选# username:# 密码可选# password:# 连接超时connect-timeout: 5000# 读超时timeout: 5000# Lettuce 客户端配置lettuce:# 连接池配置pool:# 最小空闲连接min-idle: 0# 最大空闲连接max-idle: 8# 最大活跃连接max-active: 8# 从连接池获取连接最大超时时间小于等于 0 则表示不会超时max-wait: -1ms3 创建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 {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateString, 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 RedisTemplateString, 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) {ValueOperationsString, 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 {AutowiredDistributedLockService distributedLockService;Testvoid 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。