package com.*.data.busisystem;
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Lock { /** 模块 */ String operateModelNM() default ""; /**描述*/ String operateDescribe() default""; }
package com.*.data.busisystem;
import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.net.URLDecoder; import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.redisson.Config; import org.redisson.RedissonClient; import org.redisson.SingleServerConfig; import org.redisson.core.RBucket; import org.redisson.core.RLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Controller; @Aspect @EnableAspectJAutoProxy @Controller("lockAspect") public class LockAspect { private static final Logger log = LoggerFactory.getLogger(LockAspect.class); @Autowired private RedisUtils redisUtils; private RLock lock; // 配置织入点 @Pointcut("@annotation(com.*.data.busisystem.Lock)") public void logPointCut() { } /** * 前置通知 用于拦截操作,在方法返回后执行 * @param joinPoint 切点 */ @Before(value = "logPointCut()") public void doBefore(JoinPoint joinPoint) { Config config = new Config(); SingleServerConfig singleSerververConfig = config.useSingleServer(); singleSerververConfig.setAddress("10.114.10.37:9101"); singleSerververConfig.setPassword("admin"); //redisson客户端 // RedissonClient redissonClient = RedisUtils.getInstance().getRedisson(config); RedissonClient redissonClient = redisUtils.getRedisson(); // RBucket rBucket = redisUtils.getRBucket(redissonClient, "key"); // lock = redissonClient.getLock("lock");
RBucket rBucket = redisUtils.getRBucket("key"); lock = redisUtils.getRLock("lock"); try { System.out.println("加锁前"); lock.tryLock(0, 100, TimeUnit.SECONDS); System.out.println("加锁后"); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 拦截异常操作,有异常时执行 * * @param joinPoint * @param e */ @AfterThrowing(value = "logPointCut()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Exception e) { this.release(); } @AfterReturning(value = "logPointCut()") public void doAfter(JoinPoint joinPoint) { System.out.println("释放锁前"); this.release(); System.out.println("释放锁后"); } private void release() { lock.unlock(); }
/** * 是否存在注解,如果存在就获取 */ private static Lock getAnnotationLog(JoinPoint joinPoint) throws Exception { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { return method.getAnnotation(Lock.class); } return null; } /** * * @title getIpAddress *
- * @description *
- 获取IP地址 *
- 注意:IP地址经过多次反向代理后会有多个IP值, *
- 其中第一个IP才是真实IP,所以不能通过request.getRemoteAddr()获取IP地址, *
- 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址, *
- X-Forwarded-For中第一个非unknown的有效IP才是用户真实IP地址。 *