使用lua+redis解决发多张券的并发问题
0浏览
收藏
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《使用lua+redis解决发多张券的并发问题》,聊聊并发、luaredis、多张券,我们一起来看看吧!
业务描述
这个接口的作用是给会员发多张券码。涉及到4张主体,分别是:用户,券,券码,用户领取记录。
下面是改造前的伪代码。
主要是因为查出券码那行存在并发安全问题,多个线程拿到同几个券码。以下都是基于如何让取券码变成原子的去展开。
public boolean sendCoupons(Long userId, Long couponId) {
// 一堆校验
// ...
// 查出券码
List
couponCodes = couponCodeService.findByCouponId(couponId, num);
// batchUpdateStatus是一个被@Transactional(propagation = Propagation.REQUIRES_NEW)修饰的方法
// 批量更新为已被领取状态
couponCodeService.batchUpdateStatus(couponCods);
// 发券
// 发权益
// 新增用户券码领取记录
}
改造过程
因为券码是多张,想用lua+redis的list结构去做弹出。为什么用这种方案是因为for update直接被否了。
这是写的lua脚本。。
local result = {}
for i=1,ARGV[1],1 do
result[i] = redis.call("lpop", KEYS[1])
end
return table.contact(result , "|")
这是写的执行lua脚本的client。。其实主要的解决方法就是在redis的list里rpush(存),lpop(取)取数据
@Slf4j
@Component
public class CouponCodeRedisQueueClient implements InitializingBean {
/**
* redis lua脚本文件路径
*/
public static final String POP_COUPON_CODE_LUA_PATH = "lua/pop-coupon-code.lua";
public static final String SEPARATOR = "|";
private static final String COUPON_CODE_KEY_PATTERN = "PROMOTION:COUPON_CODE_{0}";
private String LUA_COUPON_CODE_SCRIPT;
private String LUA_COUPON_CODE_SCRIPT_SHA;
@Autowired
private JedisTemplate jedisTemplate;
@Override
public void afterPropertiesSet() throws Exception {
LUA_COUPON_CODE_SCRIPT = Resources.toString(Resources.getResource(POP_COUPON_CODE_LUA_PATH), Charsets.UTF_8);
if (StringUtils.isNotBlank(LUA_COUPON_CODE_SCRIPT)) {
LUA_COUPON_CODE_SCRIPT_SHA = jedisTemplate.execute(jedis -> {
return jedis.scriptLoad(LUA_COUPON_CODE_SCRIPT);
});
log.info("redis lock script sha:{}", LUA_COUPON_CODE_SCRIPT_SHA);
}
}
/**
* 获取Code
*
* @param activityId
* @param num
* @return
*/
public List
popCouponCode(Long activityId, String num , int retryNum) {
if(retryNum == 0){
log.error("reload lua script error , try limit times ,activityId:{}", activityId);
return Collections.emptyList();
}
List
keys = Lists.newArrayList(); String key = buildKey(String.valueOf(activityId)); keys.add(key); List
args = Lists.newArrayList(); args.add(num); try { Object result = jedisTemplate.execute(jedis -> { if (StringUtils.isNotBlank(LUA_COUPON_CODE_SCRIPT_SHA)) { return jedis.evalsha(LUA_COUPON_CODE_SCRIPT_SHA, keys, args); } else { return jedis.eval(LUA_COUPON_CODE_SCRIPT, keys, args); } }); log.info("pop coupon code by lua script.result:{}", result); if (Objects.isNull(result)) { return Collections.emptyList(); } return Splitter.on(SEPARATOR).splitToList(result.toString()); } catch (JedisNoScriptException jnse) { log.error("no lua lock script found.try to reload it", jnse); reloadLuaScript(); //加载后重新执行 popCouponCode(activityId, num, --retryNum); } catch (Exception e) { log.error("failed to get a redis lock.key:{}", key, e); } return Collections.emptyList(); } /** * 重新加载LUA脚本 * * @throws Exception */ public void reloadLuaScript() { synchronized (CouponCodeRedisQueueClient.class) { try { afterPropertiesSet(); } catch (Exception e) { log.error("failed to reload redis lock lua script.retry load it."); reloadLuaScript(); } } } /** * 构建Key * * @param activityId * @return */ public String buildKey(String activityId) { return MessageFormat.format(COUPON_CODE_KEY_PATTERN, activityId); } }
当然这种操作需要去提前把所有券的券码丢到redis里去,这里我们也碰到了一些问题(券码量比较大的情况下)。比如开始直接粗暴的用@PostConstruct去放入redis,导致项目启动需要很久很久。。这里就不展开了,说一下我们尝试的几种方法
- @PostConstruct注解
- CommandLineRunner接口
- redis的pipeline技术
- 先保证每个卡券有一定量的券码在redis,再用定时任务定时(根据业务量)去补
好了,本文到此结束,带大家了解了《使用lua+redis解决发多张券的并发问题》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
版本声明 本文转载于:脚本之家 如有侵犯,请联系 删除
- kubernetes环境部署单节点redis数据库的方法
- redis-benchmark并发压力测试的问题解析
