栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 自定义注解+AOP+Redis 实现分布式锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 自定义注解+AOP+Redis 实现分布式锁

文章目录
    • 前言
    • 注解类
    • redis加锁解锁操作类
    • 切面类
    • 配置类
    • dto传输对象
    • 启动类
    • 配置文件
    • 测试类

前言

分布式环境下多个不同线程需要对共享资源进行同步,那么用Java的锁机制就无法实现了,这个时候就必须借助分布式锁来解决分布式环境下共享资源的同步问题。

aop 通过设置切面,当切面设置的目标类的方法被调用时,aop 框架会拦截此次调用,源码中 pointCut 类里有两个核心属性,即 ClassFilter 类过滤器与MethodMatcher 方法匹配器,aop基于其两个核心来进行拦截,拦截之后aop机制会通过jdk或cglib生成动态代理对象,调用增强类的增强方法进行功能织入。

@AspectJ 是一种将切面声明为带有注解的常规 Java 类的样式。 @AspectJ 样式是AspectJ project作为 AspectJ 5 版本的一部分引入的。 Spring 使用 AspectJ 提供的用于切入点解析和匹配的库来解释与 AspectJ 5 相同的 注解。但是,AOP 运行时仍然是纯 Spring AOP,并且不依赖于 AspectJ 编译器或编织器。

注解类

所要加锁的方法加的注解

package com.test.redislock.annotation;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedLockApi {


    
    String prefix();

    
    String value() default "lock_value";

    
    long timeout() default 10;

    
    TimeUnit timeUnit() default TimeUnit.MINUTES;

    
    boolean immediatelyUnLock() default true;

    
    long waitLockSecondTime() default 0;

    
     long maxRetryTimes () default 0;

    
    boolean retry() default false;
}

BusinessId 注解对于一个任务比如task1 有唯一标识id 加这个使锁粒度最小化

package com.test.redislock.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface BusinessId {
}


redis加锁解锁操作类

接口

package com.test.redislock.service;


import lombok.NonNull;


public interface RedisLockService {
    
    void lock(@NonNull RedisLockDto lock) throws RedisLockException;

    
    void unlock(@NonNull RedisLockDto lock);

    
    boolean extend(@NonNull RedisLockDto lock);
}


实现类

package com.test.redislock.service;


import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;


@AllArgsConstructor
@Slf4j
public class RedisLockServiceImpl implements RedisLockService {

    
    private final RedisTemplate redisTemplate;

    
    private final int maxRetryTimes;

    
    private final long delayTime;

    @Override
    public void lock(@NonNull RedisLockDto lock) throws RedisLockException {
        if (lock.getRetry()) {
            lockWithRetry(lock);
        } else {
            lockWithoutRetry(lock);
        }
    }

    
    private void lockWithoutRetry(@NonNull RedisLockDto lock) throws RedisLockException {
        if (!tryLock(lock)) {
            log.warn("failed get lock , key: {}", this.getLockKey(lock));
            throw new RedisLockException(lock.getPrefix(), lock.getBusinessId(), lock, getLockValue(lock));
        }
        log.info("successfully get lock , key: {}", this.getLockKey(lock));
    }

    
    private void lockWithRetry(RedisLockDto lock) throws RedisLockException {
        int retryTimes = 0;
        while (!tryLock(lock)) {
            ++retryTimes;

            long realMaxRetryTimes;
            if (lock.getMaxRetryTimes() != null) {
                realMaxRetryTimes = lock.getMaxRetryTimes();
            } else {
                realMaxRetryTimes = this.maxRetryTimes;
            }
            if (realMaxRetryTimes >= 0 && retryTimes >= realMaxRetryTimes) {
                log.warn("failed get lock ,key: {}", this.getLockKey(lock));
                throw new RedisLockException(lock.getPrefix(), lock.getBusinessId(), lock, getLockValue(lock));
            }

            long realDelayTime;
            if (lock.getDelayTime() != null) {
                realDelayTime = lock.getDelayTime();
            } else {
                realDelayTime = this.delayTime;
            }

            try {
                Thread.sleep(realDelayTime);
            } catch (InterruptedException | IllegalArgumentException e) {
                // 什么也不做
            }
        }
        log.info("successfully get lock ,key: {}", this.getLockKey(lock));
    }

    @Override
    public void unlock(@NonNull RedisLockDto lock) {
        if (lock.getLockValue().equals(redisTemplate.opsForValue().get(this.getLockKey(lock)))) {
            redisTemplate.delete(lock.getKey());
            log.info("successfully delete lock , key:{}", this.getLockKey(lock));
        }
    }

    
    private boolean tryLock(@NonNull RedisLockDto lock) {
        long start = System.currentTimeMillis();
        if(lock.getDelayTime()>0 && (System.currentTimeMillis()-start > 1000* (lock.getDelayTime()))){
            return false;
        }
        if(System.currentTimeMillis()-start > lock.getDelayTime()*1000){
            return false;
        }
        Boolean aBoolean = redisTemplate.opsForValue()
                .setIfAbsent(lock.getKey(), lock.getLockValue(), lock.getExpiredTime(), lock.getTimeUnit());
        if(!aBoolean){
            return tryLock(lock);
        }
        return Boolean.TRUE.equals(aBoolean );
    }

    
    private String getLockValue(@NonNull RedisLockDto lock) {
        return redisTemplate.opsForValue().get(this.getLockKey(lock));
    }

    
    @Override
    public boolean extend(@NonNull RedisLockDto lock) {
        return Boolean.TRUE.equals(
                redisTemplate.expire(this.getLockKey(lock), lock.getExpiredTime(), TimeUnit.MILLISECONDS)
        );
    }

    
    private String getLockKey(RedisLockDto lock) {
        return lock.getKey();
    }
}


异常类

import lombok.Getter;


public class RedisLockException extends Exception {
    
    @Getter
    private String prefix;

    
    @Getter
    private String businessId;

    
    @Getter
    private RedisLockDto lock;

    
    @Getter
    private String lockValue;

    public RedisLockException(String prefix, String businessId, RedisLockDto lock, String lockValue) {
        super("failed to acquire redis lock,prefix is " + prefix + ",business id is " + businessId + ",lock value is " + lockValue);
        this.prefix = prefix;
        this.businessId = businessId;
        this.lock = lock;
        this.lockValue = lockValue;
    }
}


切面类
;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.UUID;


@SuppressWarnings("DuplicatedCode")
@Slf4j
@Aspect
@Component
public class DistributedLockAspect {

    
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisLockService redisLockService;

    
    @Value("${ngsoc.redis-lock.maxRetryTimes:10}")
    private int maxRetryTimes;

    
    @Value("${ngsoc.redis-lock.delayTime:100}")
    private long delayTime;

  

    
    @Pointcut("@annotation(com.andon.springbootdistributedlock.annotation.DistributedLockApi)")
    public void apiPointCut() {
    }


    
    @SneakyThrows
    @Around(value = "apiPointCut() && @annotation(distributedLockApi)")
    public Object apiAround(ProceedingJoinPoint joinPoint, DistributedLockApi distributedLockApi) throws  Exception{
        // 1.从注解获取redis lock的配置参数
        RedisLockDto lock = this.getRedisLockFromAnnotation(joinPoint);
        // 2.尝试获取锁
        try {
            redisLockService.lock(lock);
            // 放行,目标方法的执行
            return joinPoint.proceed();
        } finally {
            // 3.释放锁
            redisLockService.unlock(lock);
        }
       // throw new DistributedLockException("请勿重复操作!!");
    }



    
    private RedisLockDto getRedisLockFromAnnotation(ProceedingJoinPoint joinPoint) {


        // 获取目标方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();

        // 获取方法上指定类型的注解
        DistributedLockApi redisLock = method.getAnnotation(DistributedLockApi.class);

        // 获取目标类名方法名
        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();
        // 从注解对象中获取元素的值
        String prefix = redisLock.prefix();
        String keyPre = prefix + "_" + className + "_" + methodName;
        if (StringUtils.isEmpty(redisLock.prefix())) {
            // 如果redis分布式锁的key的前缀为空,则使用目标方法的名称作为前缀
            prefix = method.getName();
        }

        // 获取方法中每个参数前加的注解的值,一个方法可能有多个参数,一个参数前可能加多个注解,所以为二维数组
        Annotation[][] annotations = method.getParameterAnnotations();
        StringBuilder businessIdBuilder = new StringBuilder();
        for (int i = 0; i < annotations.length; i++) {
            // 获取目标方法的第i个参数
            Object param = joinPoint.getArgs()[i];
            // 获取目标方法的第i个参数前所加的所有注解
            Annotation[] paramAnnotations = annotations[i];
            if (param == null || paramAnnotations.length == 0) {
                continue;
            }
            // 遍历目标方法第i个参数前的所有注解
            for (Annotation annotation : paramAnnotations) {
                // 判断注解类型是否BusinessId
                if (annotation.annotationType().equals(BusinessId.class)) {
                    businessIdBuilder.append(param);
                }
            }
        }

        return RedisLockDto.builder()
                .prefix(prefix)
                .key(keyPre + businessIdBuilder.toString())
                .timeUnit(redisLock.timeUnit())
                .delayTime(redisLock.waitLockSecondTime()>0?redisLock.waitLockSecondTime():delayTime)
                .expiredTime(redisLock.timeout())
                .retry(redisLock.retry())
                .maxRetryTimes(redisLock.maxRetryTimes())
                .lockValue(UUID.randomUUID().toString())
                .businessId(businessIdBuilder.toString())
                .build();
    }


}

配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisLockAutoConfiguration {

    
    @Value("${ngsoc.redis-lock.maxRetryTimes:10}")
    @Getter
    private int maxRetryTimes;

    
    @Value("${ngsoc.redis-lock.delayTime:100}")
    @Getter
    private long delayTime;

    
    

    
    @Bean(value = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
                Object.class
        );
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY
        );
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        StringRedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    
    @Bean
    public RedisLockService redisLockService(RedisTemplate redisTemplate) {
        return new RedisLockServiceImpl(redisTemplate, maxRetryTimes, delayTime);
    }
}


 
dto传输对象 
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Tolerate;

import java.util.concurrent.TimeUnit;


@Data
@Builder
public class RedisLockDto {
    @Tolerate
    public RedisLockDto() {
    }

    
    private String prefix;
    
    private String key;

    
    private Long expiredTime;

    
    private TimeUnit timeUnit;

    
    private String businessId;

    
    private Boolean retry = true;

    
    private String lockValue;

    
    private Long maxRetryTimes;

    
    private Long delayTime;
}


启动类
@SpringBootApplication
public class SpringbootDistributedLockApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDistributedLockApplication.class, args);
    }

}
配置文件
server:
  port: 8088
spring:
  #============== redis ===================
  redis:
    host: 127.0.0.1
    port: 6379
ngsoc:
  redis-lock:
    maxRetryTimes: 30
    delayTime: 3000
测试类
import lombok.extern.slf4j.Slf4j;
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;


@Slf4j
@RequestMapping(value = "/api")
@RestController
public class ApiController {

    @DistributedLockApi(prefix = "test_lock",timeout = 20, timeUnit = TimeUnit.SECONDS, immediatelyUnLock = false)
    @GetMapping(value = "/test")

    public String test(@BusinessId String taskId) {

        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        try {
            Thread.sleep(9999L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test!!";
    }
    @DistributedLockApi(prefix = "test_lock",timeout = 120, timeUnit = TimeUnit.SECONDS, immediatelyUnLock = false, waitLockSecondTime = 15)
    @GetMapping(value = "/test2")

    public String test2(@BusinessId String taskId) {

        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        try {
            Thread.sleep(20000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test!!";
    }
    @DistributedLockApi(prefix = "test_lock",timeout = 180,retry=true,maxRetryTimes=10, timeUnit = TimeUnit.SECONDS, immediatelyUnLock = false, waitLockSecondTime = 150)
    @GetMapping(value = "/test3")

    public String test3(@BusinessId String taskId) {

        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        log.info("test!!");
        try {
            Thread.sleep(20000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test!!";
    }
}

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号