目录
Redis实现的分布式锁还存在一下问题:

Redisson可以全部解决以上问题
1. Redisson获取的锁是可重入的锁
RLock lock = redissonClient.getLock("lock:order");
2. waitTime为重试时间,leaseTime为持有的超时时间
lock.tryLock(long waitTime, long leaseTime, TimeUnit unit)
3.不设置leaseTime,可以解决超时释放带来的并发安全隐患
4.解决主从一致性问题,采用连锁,多个节点都为主节点。同时获取全部节点的锁。

引入依赖
-
- <dependency>
- <groupId>org.redissongroupId>
- <artifactId>redissonartifactId>
- <version>3.17.5version>
- dependency>
添加配置
- @Configuration
- public class RedissonConfig {
-
- @Bean
- public RedissonClient redissonClient() {
- //配置
- Config config = new Config();
- //添加Redis地址,这里添加单点地址,也可以使用config.useClusterServers()添加集群地址
- config.useSingleServer().setAddress("redis://192.168.99.100:56379").setPassword("123321");
- //创建Redis对象
- return Redisson.create(config);
- }
- }
使用Redisson提供的分布式锁
- @Service
- public class DistributePayImpl implements SinglePay {
-
- @Autowired
- SimpleRedisLock simpleRedisLock;
-
- @Autowired
- RedissonClient redissonClient;
-
- public void buyOrder(Long userId) {
- buyOrder_RedissionLock(userId);
- }
-
- private void buyOrder_RedissionLock(Long userId){
- String lockKey = "lock:order:" + userId;
- //使用Redisson获取锁
- RLock lock = redissonClient.getLock(lockKey);
- //tryLock无参为默认值,只尝试一次,获取不到就不等了,持有锁的时间为30s,超过自动释放
- boolean isLock = lock.tryLock();
- if (!isLock) {
- //获取锁失败,返回错误或重试
- //"一人只能下一单"
- return;
- }
- try {
- SinglePay proxy = (SinglePay) AopContext.currentProxy();
- proxy.createOrder(userId);
- } finally {
- lock.unlock();
- }
- }
-
- @Transactional
- public String createOrder(Long userId) {
- //查询数据库order中userId是否存在记录,存在则不让购买
- int count = queryOrderCount(userId);
- if (count > 0) {
- //已买过购买失败
- return "fail";
- }
-
- boolean success = order_count(userId);
- if (!success) {
- return "库存不足";
- }
-
- saveOrder(userId);
- return "成功";
- // }
- }
-
- private void saveOrder(Long userId) {
- //模拟提交订单插入数据库
- //insert into order values(userId.....);
- }
-
- private boolean order_count(Long userId) {
- //修改库存
- //update order_count set count = count -1 where userId =? and count >0;
- return true;
- }
-
- private int queryOrderCount(Long userId) {
- //模拟查询数据库
- //select count(*) from order where userId = ?
- return 0;
- }
- }
redissonClient.getLock(key)获得锁为可冲入锁,在redis中存储的类型为hash类型
- @Slf4j
- @SpringBootTest
- class RedissonTests {
-
- @Autowired
- private RedissonClient redissonClient;
-
- private RLock lock;
-
- @BeforeEach
- void setUp() {
- lock = redissonClient.getLock("lock:order");
- }
-
- @Test
- void testString() {
- boolean isLock = lock.tryLock();
- if (!isLock) {
- log.error("获取锁失败.....1");
- return;
- }
- try {
- log.info("获取锁成功......1");
- method2();
- log.info("开始执行业务......1");
- } finally {
- log.info("准备释放锁......1");
- lock.unlock();
- }
- }
-
- private void method2() {
- boolean isLock = lock.tryLock();
- if (!isLock) {
- log.error("获取锁失败.....2");
- return;
- }
- try {
- log.info("获取锁成功.....2");
- log.info("开始执行业务......2");
- } finally {
- log.info("准备释放锁......2");
- lock.unlock();
- }
- }
- }


根据上图可以发现,同一个线程可以多次获取锁,value值会增加。key的UUID和线程一一对应

同时获取多个Redis节点的锁,只有全部获取到才算是获取锁成功,否则视为失败

- //连接多个Redis节点
- @Configuration
- public class RedissonConfig {
-
- @Bean
- public RedissonClient redissonClient() {
- //配置
- Config config = new Config();
- //添加Redis地址,这里添加单点地址,也可以使用config.useClusterServers()添加集群地址
- config.useSingleServer().setAddress("redis://192.168.99.100:56379").setPassword("123321");
- //创建Redis对象
- return Redisson.create(config);
- }
-
- @Bean
- public RedissonClient redissonClient2() {
- //配置
- Config config = new Config();
- //添加Redis地址,这里添加单点地址,也可以使用config.useClusterServers()添加集群地址
- config.useSingleServer().setAddress("redis://192.168.99.100:56378").setPassword("123321");
- //创建Redis对象
- return Redisson.create(config);
- }
-
- @Bean
- public RedissonClient redissonClient3() {
- //配置
- Config config = new Config();
- //添加Redis地址,这里添加单点地址,也可以使用config.useClusterServers()添加集群地址
- config.useSingleServer().setAddress("redis://192.168.99.100:56377").setPassword("123321");
- //创建Redis对象
- return Redisson.create(config);
- }
- }
-
- @Slf4j
- @SpringBootTest
- class RedissonTests {
-
- @Autowired
- private RedissonClient redissonClient;
- @Autowired
- private RedissonClient redissonClient2;
- @Autowired
- private RedissonClient redissonClient3;
- private RLock lock;
-
- @BeforeEach
- void setUp() {
- RLock lock1 = redissonClient.getLock("lock:order");
- RLock lock2 = redissonClient2.getLock("lock:order");
- RLock lock3 = redissonClient3.getLock("lock:order");
- //从多把锁中获取的最终的lock
- lock = redissonClient.getMultiLock(lock1,lock2,lock3);
- }
-
- @Test
- void testString() {
- boolean isLock = lock.tryLock();
- if (!isLock) {
- log.error("获取锁失败.....1");
- return;
- }
- try {
- log.info("获取锁成功......1");
- method2();
- log.info("开始执行业务......1");
- } finally {
- log.info("准备释放锁......1");
- lock.unlock();
- }
- }
-
- private void method2() {
- boolean isLock = lock.tryLock();
- if (!isLock) {
- log.error("获取锁失败.....2");
- return;
- }
- try {
- log.info("获取锁成功.....2");
- log.info("开始执行业务......2");
- } finally {
- log.info("准备释放锁......2");
- lock.unlock();
- }
- }
- }
RLock lock = redissonClient.getMultiLock(lock1,lock2,lock3);
根绝多把锁获取最总错做的RLock对象,当tryLock成功时,会在所有的Redis的节点中存在key =lock:order的记录

- Config config = new Config();
- //添加Redis地址,这里添加单点地址,也可以使用config.useClusterServers()添加集群地址
- config.useSingleServer().setAddress("redis://192.168.99.100:56379").setPassword("123321");
- //创建Redis对象
- Redisson redisson = Redisson.create(config);
- RBloomFilter
bloomFilter = redisson.getBloomFilter("product:bloom"); - //初始化布隆过滤器:预计元素1000000L,误判率为1%
- bloomFilter.tryInit(1000000L,0.01);
- bloomFilter.add("1");//添加数据
- //判断在布隆过滤器中是否存在
- System.out.println(bloomFilter.contains("1"));//输出true
- System.out.println(bloomFilter.contains("8888"));输出false