虽然数据库有锁的实现,但是有时候对于数据的操作更需要业务层控制。

这个解决的问题有次面试被问到过,当时不知道怎么解决,乱说一通,今天也算是有个解决方案了

项目中有个需求,就是订单需要经过一层一层的审核,审核过程中当前审核人有权限审核,上一审核人有权限撤销上一步的审核。这样在审核过程中就需要对订单审核权限进行控制:

  1. 只有当前审核人和上一审核人可以进行操作
  2. 当前审核人审核后上一审核人就不能撤回
  3. 上一审核人撤回后当前审核人就无法审核

实现上述需求,我就需要对订单的审核/撤销接口进行控制,即同一订单的审核/撤销要互斥(审核/撤销是同一个接口)

最简单的解决方案是在该接口的方法上使用 synchronized,这种方案解决了上述的问题,但是这种方案的问题是不同订单的审核操作也不能同时进行。

回到问题本身,我们要解决的是同一订单的审核操作要互斥,互斥是基于订单的,所以只要审核接口所操作的对象不是同一订单就不需要互斥,怎么实现呢。

我想到的第一个方案是使用redis来为每个订单加锁

思路是

  1. 当有审核的请求线程时,先通过订单编号(订单的唯一索引)往redis中set一组值(使用

    RedisTemplate.opsForValue().setIfAbsent(key, value)

    如果已经存在key,返回false且不做任何改变,不存在就将 key 的值设为 value),在这里我把订单编号作为key,set成功后在设置一个过期时间(为了避免死锁)

  2. 当1返回true时代表加锁成功,当前请求线程继续执行,执行结束后需要释放锁,即删除redis中的key
  3. 当1返回false时,等待,继续执行2

这是锁实现

 package pers.lan.jc.compnent;

 import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /**
* @author lan [1728209643@qq.com]
* @create 2018-12-01 14:12
* @desc redis锁
*/
@Slf4j
@Component
public class RedisLock { private final static String LOCK_PREFIX = "LOCK:"; @Autowired
private RedisTemplate<String, String> redisTemplate; public boolean lock(String key) {
while (true) {
try {
if (setIfAbsent(key)) {
return true;
}
Thread.sleep(100);
} catch (Exception e) {
return false;
} finally {
unlock(key);
}
}
} private synchronized boolean setIfAbsent(String key) {
try {
Boolean locked = redisTemplate.opsForValue().setIfAbsent(LOCK_PREFIX + key, key);
if (locked != null && locked) {
redisTemplate.expire(LOCK_PREFIX + key, 120, TimeUnit.SECONDS);
return true;
}
} finally {
unlock(key);
}
return false;
} public void unlock(String key) {
redisTemplate.delete(LOCK_PREFIX + key);
} }

这种方案不好的地方在于,set和expire操作不是原子的,于是setIfAbsent()方法是互斥的,并发性能并不是很好

另一种方案是使用redisson

添加依赖

        <dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.9.1</version>
</dependency>

  配置

package pers.lan.jc.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author lan [1728209643@qq.com]
* @create 2018-12-01 16:19
* @desc redissonConfig
*/
@Slf4j
@Configuration
public class RedissonConfig { @Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.setLockWatchdogTimeout(10000L);
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setPassword("travis");
singleServerConfig.setAddress("redis://118.25.43.205:6379");
singleServerConfig.setDatabase(0);
return Redisson.create(config);
}
}

  使用

package pers.lan.jc.controller;

import org.redisson.api.RReadWriteLock;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author lan [1728209643@qq.com]
* @create 2018-12-01 14:32
* @desc redis锁控制器
*/
@RequestMapping("/lock")
@RestController
public class RedisLockController { @Autowired
private RedissonClient redisson; private final static String PREFIX = "lan:"; @GetMapping("/get2")
public Object lock2(@RequestParam String key) {
RReadWriteLock lock = redisson.getReadWriteLock(PREFIX + key); try {
lock.writeLock().lock();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " ##########");
System.out.println(Thread.currentThread().getName() + " @@@@@@@@@@");
System.out.println(Thread.currentThread().getName() + " %%%%%%%%%%");
System.out.println();
System.out.println();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
} return "ok";
}
}

  redisson具体的文档见https://github.com/redisson/redisson/wiki

项目中使用:

为了不影响原有业务和代码冗余等,我想通过注解+AOP使用redisson加锁,在每个接口上通过如下注解

package pers.lan.jc.annotation;

import java.lang.annotation.*;

/**
* @author lan [1728209643@qq.com]
* @create 2018-12-01 18:12
* @desc 加锁器注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Locking { String key(); }

  key的值为每个接口加锁的关键索引(比如订单编号)

但是问题又来了,有的接口订单表号是放在实体类中的,怎么引用呢?仿照spring中的Cache类注解,,通过Spring EL表达式使用,

需要加锁的接口使用注解如下

    @Locking(key = "#book.id")
@CachePut(key = "#book.id")
public void update(Book book) {
bookMapper.update(book);
}

  其中CachePut注解可以忽略

切面如下

package pers.lan.jc.compnent;

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.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import pers.lan.jc.annotation.Locking; import java.lang.reflect.Method; /**
* @author lan [1728209643@qq.com]
* @create 2018-12-01 18:20
* @desc 锁切面
*/
@Aspect
@Component
@Slf4j
public class LockAspect { @Autowired
private RedissonClient redisson; @Pointcut("@annotation(pers.lan.jc.annotation.Locking)")
public void lockAspect() {
} @Around("lockAspect()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Locking locking = method.getDeclaredAnnotation(Locking.class);
String prefix = "lockKey:" + joinPoint.getTarget().getClass().getSimpleName() + "." + method.getName() + ".";
if (locking != null) {
try {
ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
String[] parameterNames = discoverer.getParameterNames(method);
Object[] args = joinPoint.getArgs();
if (parameterNames != null) {
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext();
int len = Math.min(args.length, parameterNames.length);
for (int i = 0; i < len; i++) {
ctx.setVariable(parameterNames[i], args[i]);
}
Object value = parser.parseExpression(locking.key()).getValue(ctx);
RReadWriteLock lock = redisson.getReadWriteLock(prefix + value);
log.info("正在尝试向[" + prefix + "." + method.getName() + "]加锁, key = " + prefix + value);
try {
lock.writeLock().lock();
log.info("加锁成功,正在处理业务, key = " + prefix + value);
return joinPoint.proceed();
} finally {
log.info("业务处理结束,释放锁, key = " + prefix + value);
lock.writeLock().unlock();
}
} } catch (Exception e) {
e.printStackTrace();
}
} return joinPoint.proceed();
}
}

  为了不影响原有业务和各种异常,所以多了很多try catch块,因为业务中用到的都是互斥锁,所以这里我使用的都是writeLock实现的

都是为了给自己看,所以没写太详细,哈哈哈

最新文章

  1. 数据库优化实践【MS SQL优化开篇】
  2. soa vs cop
  3. Wmware Center安装过程记录
  4. 【转】Nginx模块开发入门
  5. Unity3D热更新
  6. Android ViewPager FragmentPagerAdapter
  7. list实例应用
  8. 设计模式-策略模式Strategy以及消灭if else
  9. Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据
  10. 关于:未能加载文件或程序集“ICSharpCode.SharpZipLib”或它的某一个依赖项异常的解决方案
  11. Codechef August Challenge 2018 : Interactive Matrix
  12. 从PMP培训归来,跟大家聊聊做项目的套路
  13. SpringCache实战遇坑
  14. [转帖]NotePad++编辑Linux中的文件
  15. .net core开发工具与SDK
  16. jenkins+ant+jmeter自动化性能测试平台
  17. manifest.xml微擎系统模块的安装文件内容
  18. codeforces959C
  19. 9月list
  20. Django自带的后台管理样式找不到的问题。

热门文章

  1. php常用代码段
  2. pycharts实现可视化
  3. Serv-U设置允许用户更改密码【转】
  4. JNDI(Java Naming and Directory Interface,Java命名和目录接口)
  5. POJ 2230 Watchcow(欧拉回路:输出点路径)
  6. DB2:FETCH FIRST 1 ROWS ONLY
  7. MySQL之正则表达式
  8. Button Bashing(搜索)
  9. csu 1547(01背包)
  10. [MySQL] specified key was too long max key length is 767bytes