SpringBoot使用Redission实现分布式锁

    首先在pom.xml文件中添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.8.2</version>
    <optional>true</optional>
</dependency>            

    然后就是Redission的配置,没有什么特别的地方:

@Configuration
public class RedisConfig {
	@Bean(destroyMethod = "shutdown")
	RedissonClient redisson() throws IOException {
		Config config = new Config();
		config.useSingleServer().setAddress("redis://127.0.0.1:6379");
		return Redisson.create(config);
	}
}

    Redission的配置比较简单,配置一个RedissonClient就可以了,默认的配置支持redis的所有实现方式,集群,单节点,哨兵,副本等都可以进行配置,这里需要注意的是配置的线程模型,默认配置的线程是64个。

    Redission实现分布式锁示例代码如下所示:支持常用的读写锁,信号量,红锁,闭锁
 

@Repository
public class RedisDaoImpl {

	@Autowired
	private RedissonClient redissonClient;

	public String getString(String key) {
		RBucket<Object> result = this.redissonClient.getBucket(key);
		return result.get().toString();
	}

	public void setString(String key, Object value) {
		RBucket<Object> result = this.redissonClient.getBucket(key);
		if (!result.isExists()) {
			result.set(value, 5, TimeUnit.MINUTES);
		}
	}

	public boolean hasString(String key) {
		RBucket<Object> result = this.redissonClient.getBucket(key);
		if (result.isExists()) {
			return true;
		} else {
			return false;
		}
	}

	public long incr(String key, long delta) {
		return this.redissonClient.getAtomicLong(key).addAndGet(delta);
	}

	public void lock() {
		RCountDownLatch countDown = redissonClient.getCountDownLatch("test");
		countDown.trySetCount(1);
		try {
			countDown.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		RCountDownLatch latch = redissonClient.getCountDownLatch("testCountDownLatchName");
		latch.countDown();
		RReadWriteLock rwlock = redissonClient.getReadWriteLock("ReadWriteLockName");
		// 加读锁需要手动解锁
		rwlock.readLock().lock();
		rwlock.readLock().unlock();
		// 加写锁需要手动解锁
		rwlock.writeLock().lock();
		rwlock.writeLock().unlock();
		// 加读锁,上锁后1秒自动解锁
		rwlock.readLock().lock(1, TimeUnit.SECONDS);
		// 加写锁,上锁后1秒自动解锁
		rwlock.writeLock().lock(1, TimeUnit.SECONDS);
		try {
			// 尝试加锁,最多等待100秒,上锁以后5秒自动解锁
			boolean resRead = rwlock.readLock().tryLock(100, 5, TimeUnit.SECONDS);
			boolean resWrite = rwlock.writeLock().tryLock(100, 5, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// 红锁
		RLock redlock1 = redissonInstance1.getLock("redlock1");
		RLock redlock2 = redissonInstance2.getLock("redlock2");
		RLock redlock3 = redissonInstance3.getLock("redlock3");
		RedissonRedLock redissionRedlock = new RedissonRedLock(redlock1, redlock2, redlock3);
		// 同时加锁:redlock1 redlock2 redlock3
		// 注意:红锁在大部分节点上加锁成功就算成功。
		redissionRedlock.lock();
		redissionRedlock.unlock();

	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值