在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、前言
2、分布式锁分布式锁,是一种思想,它的实现方式有很多,如基于数据库实现、基于缓存(Redis等)实现、基于Zookeeper实现等等。为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件
3、基于Redis实现分布式锁
3.1、RedisConfig.javapackage com.demo.configuration; 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 factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // key采用String的序列化方式 template.setKeySerializer(new StringRedisSerializer()); // value序列化方式采用jackson template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; } } 3.2、RedisLockController.javapackage com.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.scripting.support.ResourceScriptSource; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; @RestController @RequestMapping("/redis") public class RedisLockController { @Autowired private RedisTemplate<String, Object> redisTemplate; @RequestMapping(value = "/lock/{key}/{uid}/{expire}") public Long lock(@PathVariable("key") String key, @PathVariable("uid") String uid, @PathVariable("expire") Integer expire) { Long result = null; try { //调用lua脚本并执行 DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(); redisScript.setResultType(Long.class);//返回类型是Long //lua文件存放在resources目录下的redis文件夹内 redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("redis/redis_lock.lua"))); result = redisTemplate.execute(redisScript, Arrays.asList(key), uid, expire); System.out.println("lock==" + result); } catch (Exception e) { e.printStackTrace(); } return result; } @RequestMapping(value = "/unlock/{key}/{uid}") public Long unlock(@PathVariable("key") String key, @PathVariable("uid") String uid) { Long result = null; try { //调用lua脚本并执行 DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(); redisScript.setResultType(Long.class);//返回类型是Long //lua文件存放在resources目录下的redis文件夹内 redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("redis/redis_unlock.lua"))); result = redisTemplate.execute(redisScript, Arrays.asList(key), uid); System.out.println("unlock==" + result); } catch (Exception e) { e.printStackTrace(); } return result; } } 3.3、redis_lock.luaif redis.call('setnx',KEYS[1],ARGV[1]) == 1 then return redis.call('expire',KEYS[1],ARGV[2]) else return 0 end 3.4、redis_unlock.luaif redis.call("exists",KEYS[1]) == 0 then return 1 end if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end 4、测试效果
加锁:锁主人为thread12345 解锁:解锁人为thread123456 解锁:解锁人为thread12345 4.1、加锁,其他人解锁
4.2、加锁,自己解锁
5、总结
到此这篇关于基于Redis实现分布式锁的方法(lua脚本版)的文章就介绍到这了,更多相关Redis实现分布式锁内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论