重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringBoot中怎么利用Redisson实现分布式锁

这篇文章将为大家详细讲解有关SpringBoot中怎么利用redisson实现分布式锁,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

菏泽网站建设公司创新互联,菏泽网站设计制作,有大型网站制作公司丰富经验。已为菏泽上1000+提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的菏泽做网站的公司定做!

导入依赖

org.redissonredisson3.8.0

属性配置

application.properites 资源文件中添加单机&哨兵相关配置

server.port=3000# redisson lock 单机模式redisson.address=redis://127.0.0.1:6379redisson.password=#哨兵模式#redisson.master-name= master#redisson.password=#redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

注意:

这里如果不加 redis:// 前缀会报 URI 构建错误

Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0

更多的配置信息可以去官网查看

定义Lock的接口定义类

package com.tuhu.thirdsample.service;import org.redisson.api.RLock;import java.util.concurrent.TimeUnit;/** * @author chendesheng * @create 2019/10/12 10:48 */public interface DistributedLocker { RLock lock(String lockKey); RLock lock(String lockKey, int timeout); RLock lock(String lockKey, TimeUnit unit, int timeout); boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime); void unlock(String lockKey); void unlock(RLock lock);}

Lock接口实现类

package com.tuhu.thirdsample.service;import org.redisson.api.RLock;import org.redisson.api.RedissonClient;import java.util.concurrent.TimeUnit;/** * @author chendesheng * @create 2019/10/12 10:49 */public class RedissonDistributedLocker implements DistributedLocker{ private RedissonClient redissonClient; @Override public RLock lock(String lockKey) {  RLock lock = redissonClient.getLock(lockKey);  lock.lock();  return lock; } @Override public RLock lock(String lockKey, int leaseTime) {  RLock lock = redissonClient.getLock(lockKey);  lock.lock(leaseTime, TimeUnit.SECONDS);  return lock; } @Override public RLock lock(String lockKey, TimeUnit unit ,int timeout) {  RLock lock = redissonClient.getLock(lockKey);  lock.lock(timeout, unit);  return lock; } @Override public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {  RLock lock = redissonClient.getLock(lockKey);  try {   return lock.tryLock(waitTime, leaseTime, unit);  } catch (InterruptedException e) {   return false;  } } @Override public void unlock(String lockKey) {  RLock lock = redissonClient.getLock(lockKey);  lock.unlock(); } @Override public void unlock(RLock lock) {  lock.unlock(); } public void setRedissonClient(RedissonClient redissonClient) {  this.redissonClient = redissonClient; }}

redisson属性装配类

package com.tuhu.thirdsample.common;import lombok.Data;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;/** * @author chendesheng * @create 2019/10/11 20:04 */@Configuration@ConfigurationProperties(prefix = "redisson")@ConditionalOnProperty("redisson.password")@Datapublic class RedissonProperties { private int timeout = 3000; private String address; private String password; private int database = 0; private int connectionPoolSize = 64; private int connectionMinimumIdleSize=10; private int slaveConnectionPoolSize = 250; private int masterConnectionPoolSize = 250; private String[] sentinelAddresses; private String masterName;}

SpringBoot自动装配类

package com.tuhu.thirdsample.configuration;import com.tuhu.thirdsample.common.RedissonProperties;import com.tuhu.thirdsample.service.DistributedLocker;import com.tuhu.thirdsample.service.RedissonDistributedLocker;import com.tuhu.thirdsample.util.RedissonLockUtil;import org.apache.commons.lang3.StringUtils;import org.redisson.Redisson;import org.redisson.api.RedissonClient;import org.redisson.config.Config;import org.redisson.config.SentinelServersConfig;import org.redisson.config.SingleServerConfig;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author chendesheng * @create 2019/10/12 10:50 */@Configuration@ConditionalOnClass(Config.class)@EnableConfigurationProperties(RedissonProperties.class)public class RedissonAutoConfiguration { @Autowired private RedissonProperties redissonProperties; /**  * 哨兵模式自动装配  * @return  */ @Bean @ConditionalOnProperty(name="redisson.master-name") RedissonClient redissonSentinel() {  Config config = new Config();  SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redissonProperties.getSentinelAddresses())    .setMasterName(redissonProperties.getMasterName())    .setTimeout(redissonProperties.getTimeout())    .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())    .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize());  if(StringUtils.isNotBlank(redissonProperties.getPassword())) {   serverConfig.setPassword(redissonProperties.getPassword());  }  return Redisson.create(config); } /**  * 单机模式自动装配  * @return  */ @Bean @ConditionalOnProperty(name="redisson.address") RedissonClient redissonSingle() {  Config config = new Config();  SingleServerConfig serverConfig = config.useSingleServer()    .setAddress(redissonProperties.getAddress())    .setTimeout(redissonProperties.getTimeout())    .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())    .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());  if(StringUtils.isNotBlank(redissonProperties.getPassword())) {   serverConfig.setPassword(redissonProperties.getPassword());  }  return Redisson.create(config); } /**  * 装配locker类,并将实例注入到RedissLockUtil中  * @return  */ @Bean DistributedLocker distributedLocker(RedissonClient redissonClient) {  DistributedLocker locker = new RedissonDistributedLocker();  ((RedissonDistributedLocker) locker).setRedissonClient(redissonClient);  RedissonLockUtil.setLocker(locker);  return locker; }}

Lock帮助类

package com.tuhu.thirdsample.util;import com.tuhu.thirdsample.service.DistributedLocker;import org.redisson.api.RLock;import java.util.concurrent.TimeUnit;/** * @author chendesheng * @create 2019/10/12 10:54 */public class RedissonLockUtil { private static DistributedLocker redissLock; public static void setLocker(DistributedLocker locker) {  redissLock = locker; } /**  * 加锁  * @param lockKey  * @return  */ public static RLock lock(String lockKey) {  return redissLock.lock(lockKey); } /**  * 释放锁  * @param lockKey  */ public static void unlock(String lockKey) {  redissLock.unlock(lockKey); } /**  * 释放锁  * @param lock  */ public static void unlock(RLock lock) {  redissLock.unlock(lock); } /**  * 带超时的锁  * @param lockKey  * @param timeout 超时时间 单位:秒  */ public static RLock lock(String lockKey, int timeout) {  return redissLock.lock(lockKey, timeout); } /**  * 带超时的锁  * @param lockKey  * @param unit 时间单位  * @param timeout 超时时间  */ public static RLock lock(String lockKey, TimeUnit unit , int timeout) {  return redissLock.lock(lockKey, unit, timeout); } /**  * 尝试获取锁  * @param lockKey  * @param waitTime 最多等待时间  * @param leaseTime 上锁后自动释放锁时间  * @return  */ public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {  return redissLock.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime); } /**  * 尝试获取锁  * @param lockKey  * @param unit 时间单位  * @param waitTime 最多等待时间  * @param leaseTime 上锁后自动释放锁时间  * @return  */ public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {  return redissLock.tryLock(lockKey, unit, waitTime, leaseTime); }}

控制层

package com.tuhu.thirdsample.task;import com.tuhu.thirdsample.common.KeyConst;import lombok.extern.slf4j.Slf4j;import org.redisson.api.RLock;import org.redisson.api.RedissonClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.TimeUnit;/** * @author chendesheng * @create 2019/10/12 11:03 */@RestController@RequestMapping("/lock")@Slf4jpublic class LockController { @Autowired RedissonClient redissonClient;  @GetMapping("/task") public void task(){  log.info("task start");  RLock lock = redissonClient.getLock(KeyConst.REDIS_LOCK_KEY);  boolean getLock = false;  try {   if (getLock = lock.tryLock(0,5,TimeUnit.SECONDS)){    //执行业务逻辑    System.out.println("拿到锁干活");   }else {    log.info("Redisson分布式锁没有获得锁:{},ThreadName:{}",KeyConst.REDIS_LOCK_KEY,Thread.currentThread().getName());   }  } catch (InterruptedException e) {   log.error("Redisson 获取分布式锁异常,异常信息:{}",e);  }finally {   if (!getLock){    return;   }   //如果演示的话需要注释该代码;实际应该放开   //lock.unlock();   //log.info("Redisson分布式锁释放锁:{},ThreadName :{}", KeyConst.REDIS_LOCK_KEY, Thread.currentThread().getName());  } }}

RLock 继承自 java.util.concurrent.locks.Lock ,可以将其理解为一个重入锁,需要手动加锁和释放锁 。

来看它其中的一个方法:tryLock(long waitTime, long leaseTime, TimeUnit unit)

getLock = lock.tryLock(0,5,TimeUnit.SECONDS)

通过 tryLock() 的参数可以看出,在获取该锁时如果被其他线程先拿到锁就会进入等待,等待 waitTime 时间,如果还没用机会获取到锁就放弃,返回 false;若获得了锁,除非是调用 unlock 释放,那么会一直持有锁,直到超过 leaseTime 指定的时间。

关于SpringBoot中怎么利用Redisson实现分布式锁就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享题目:SpringBoot中怎么利用Redisson实现分布式锁
新闻来源:http://cqcxhl.cn/article/iijghi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP