使用redis实现分布式锁
2022/7/5 2:22:31
本文主要是介绍使用redis实现分布式锁,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、使用StringRedisTemplate实现分布式锁
package com.example.baidu.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; import java.util.concurrent.TimeUnit; @RestController public class RedisController { @Autowired private RedisTemplate<String, String> redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/redis") public void sendMessage(@RequestParam String message) { String clientId = UUID.randomUUID().toString(); Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(message, clientId, 10, TimeUnit.SECONDS); if (!result) { System.out.println("获取锁失败!"); } else { try { Thread.sleep(100000); } catch (InterruptedException e) { ; e.printStackTrace(); } finally { if (clientId.equals(stringRedisTemplate.opsForValue().get(message))) { stringRedisTemplate.delete(message); } } } } }
二、使用Redisson实现分布式锁
package com.example.baidu.redis; import org.redisson.Redisson; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.concurrent.TimeUnit; @Controller public class RedissonController { @GetMapping("/redisson") @ResponseBody public int redisson() { testThreadLock(); return 0; } @Autowired private RedissonClient redissonClient; @Autowired private Redisson redisson; public void lock(String key, String num) { //获取锁 RLock lock = redisson.getLock(key); boolean locked = false; try { //设置锁 locked = lock.tryLock(10, TimeUnit.SECONDS); //locked = lock.lock(); if (locked) { //开始写业务 System.out.println(num + "锁住了。。。"); System.out.println(num + "模拟业务耗时开始。。"); Thread.sleep(10); System.out.println(num + "模拟业务耗时结束。。。"); } else { System.out.println(num + "没锁住。。。"); } } catch (Exception e) { e.printStackTrace(); } finally { if (locked) { System.out.println(num + "释放锁"); System.out.println(); lock.unlock(); } } } public void testThreadLock() { String key = "threadLock"; for (int i = 1; i < 100; i++) { new Thread() { @Override public void run() { lock("test", this.getName()); } }.start(); } } }
这篇关于使用redis实现分布式锁的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-21redisinsight-v2
- 2024-02-26Typed property App\Api\Mapper\GamePropsConfigMapper::$apiRedis must not be accessed before initia-icode9专业技术文章分享
- 2024-02-21redisson getlock
- 2024-02-20redis config
- 2024-02-20redis leaderboard
- 2024-01-23缓存选型:Redis or MemCache
- 2024-01-22面试官:Redis持久化能关吗?怎么关?
- 2024-01-21Redis压测工具redis-benchmark-icode9专业技术文章分享
- 2024-01-19这才是你应该了解的Redis数据结构!
- 2024-01-18Redis系列:使用 Redis Module 扩展功能