从redis到分布式架构,通过Redis学AKF划分原则、CAP定理

基于SpringBoot AOP面向切面编程实现Redis分布式锁基于SpringBoot AOP面向切面编程实现Redis分布式锁基于SpringBoot AOP面向切面编程实现Redis分布式锁

锁定的目标是确保相互排斥其访问的资源。实际上,此资源通常是字符串。使用redis实现锁主要是将资源放入redis中并利用其原子性。当其他线程访问时,如果Redis中已经存在此资源,则不允许进行某些后续操作。

Spring Boot通过RedisTemplate使用Redis,在实际使用过程中,分布式锁可以在封装后在方法级别使用,这样使用起来就更方便了,无需到处获取和释放锁。

首先,定义一个注解:

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public@interfaceRedisLock {

//锁定的资源,redis的键

    String value() default "default";

    //锁定保持时间(以毫秒为单位)

    long keepMills() default 30000;

    //失败时执行的操作

    LockFailAction action() default LockFailAction.CONTINUE;

    //失败时执行的操作--枚举

    public enum LockFailAction{ 

        GIVEUP, 

        CONTINUE; 

    }

    //重试的间隔

    long sleepMills() default 200;

    //重试次数

    int retryTimes() default 5; 

}

具有分布式锁的Bean

@Configuration

@AutoConfigureAfter(RedisAutoConfiguration.class)

public class DistributedLockAutoConfiguration {

@Bean

@ConditionalOnBean(RedisTemplate.class)

public DistributedLock redisDistributedLock(RedisTemplate redisTemplate){

returnnewRedisDistributedLock(redisTemplate);

}

}

面向切面编程-定义切面

@Aspect

@Configuration

@ConditionalOnClass(DistributedLock.class)

@AutoConfigureAfter(DistributedLockAutoConfiguration.class)

publicclassDistributedLockAspectConfiguration{

privatefinalLogger logger = LoggerFactory.getLogger(DistributedLockAspectConfiguration.class);

@Autowired

privateDistributedLockdistributedLock;

@Pointcut("@annotation(com.itopener.lock.redis.spring.boot.autoconfigure.annotations.RedisLock)")

privatevoidlockPoint(){

}

@Around("lockPoint()")

publicObject around(ProceedingJoinPoint pjp) throws Throwable{

Method method = ((MethodSignature) pjp.getSignature()).getMethod();

RedisLock redisLock = method.getAnnotation(RedisLock.class);

Stringkey=redisLock.value();

if(StringUtils.isEmpty(key)){

Object\[\] args = pjp.getArgs();

key = Arrays.toString(args);

}

int retryTimes = redisLock.action().equals(LockFailAction.CONTINUE) ? redisLock.retryTimes() :0;

//获取分布式锁

        boolean lock = distributedLock.lock(key, redisLock.keepMills(), retryTimes, redisLock.sleepMills()); 

        if(!lock) { 

            logger.debug("get lock failed : " + key); 

            return null; 

        }

      //执行方法之后,释放分布式锁

        logger.debug("get lock success : " + key); 

        try { 

            return pjp.proceed();  //执行方法

        } catch (Exception e) { 

            logger.error("execute locked method occured an exception", e); 

        } finally { 

            boolean releaseResult = distributedLock.releaseLock(key);  //释放分布式锁

            logger.debug("release lock :" + key + (releaseResult ?" success" : "failed")); 

        } 

        return null; 

    } 

}

使用方法

进入该方法时,占用分布式锁,

方法执行完成时,释放分布式锁

使用同一个资源,如your-custom-service-redis-key的多个函数,抢占同一个锁。谁抢到谁先执行。

@RedisLock(value="your-custom-service-redis-key")

publicvoidserviceMethod(){

//正常写方法实现

}

下面小编给大家带来一份精美的学习资料,有源码,笔记和视频奥!

从redis到分布式架构,通过Redis学AKF划分原则、CAP定理!

许多小伙伴看到这,就疑问了,不都结束了,不要慌哈,咱们有高清录播和笔记。需要的小伙伴添加v:bjmsb07 来获取吧!免费哈!

你可能感兴趣的:(从redis到分布式架构,通过Redis学AKF划分原则、CAP定理)