1.使用Redis和Mq优化领取优惠卷的高并发操作
实现思路:
因为领取优惠券的操作,涉及到操作db的操作很多,比如说查询优惠卷,统计已经领取的数量,更新已经发放的数量和新增用户券。所以了防止在高并发的情况下对我们的数据库造成的压力。使用同步变异步的操作来减缓压力,主要是使用mq。由于一些校验的操作需要提醒给用户,所以红框的操作需要借助redis完成,因为mq是异步的。
存入redis的结构(Hash结构):
Redis的添加和删除的时机:
代码实现:
发放优惠卷的时候添加缓存:
private final StringRedisTemplate redisTemplate;
@Transactional
@Override
public void beginIssue(CouponIssueFormDTO dto) {
// 1.查询优惠券
Coupon coupon = getById(dto.getId());
if (coupon == null) {
throw new BadRequestException("优惠券不存在!");
}
// 2.判断优惠券状态,是否是暂停或待发放
if(coupon.getStatus() != CouponStatus.DRAFT && coupon.getStatus() != PAUSE){
throw new BizIllegalException("优惠券状态错误!");
}
// 3.判断是否是立刻发放
LocalDateTime issueBeginTime = dto.getIssueBeginTime();
LocalDateTime now = LocalDateTime.now();
boolean isBegin = issueBeginTime == null || !issueBeginTime.isAfter(now);
// 4.更新优惠券
// 4.1.拷贝属性到PO
Coupon c = BeanUtils.copyBean(dto, Coupon.class);
// 4.2.更新状态
if (isBegin) {
c.setStatus(ISSUING);
c.setIssueBeginTime(now);
}else{
c.setStatus(UN_ISSUE);
}
// 4.3.写入数据库
updateById(c);
// 5.添加缓存,前提是立刻发放的
if (isBegin) {
coupon.setIssueBeginTime(c.getIssueBeginTime());
coupon.setIssueEndTime(c.getIssueEndTime());
cacheCouponInfo(coupon);
}
// 6.判断是否需要生成兑换码,优惠券类型必须是兑换码,优惠券状态必须是待发放
if(coupon.getObtainWay() == ObtainType.ISSUE && coupon.getStatus() == CouponStatus.DRAFT){
coupon.setIssueEndTime(c.getIssueEndTime());
codeService.asyncGenerateCode(coupon);
}
}
private void cacheCouponInfo(Coupon coupon) {
// 1.组织数据
Map<String, String> map = new HashMap<>(4);
map.put("issueBeginTime", String.valueOf(DateUtils.toEpochMilli(coupon.getIssueBeginTime())));
map.put("issueEndTime", String.valueOf(DateUtils.toEpochMilli(coupon.getIssueEndTime())));
map.put("totalNum", String.valueOf(coupon.getTotalNum()));
map.put("userLimit", String.valueOf(coupon.getUserLimit()));
// 2.写缓存
redisTemplate.opsForHash().putAll(PromotionConstants.COUPON_CACHE_KEY_PREFIX + coupon.getId(), map);
}
暂停发放优惠券的时候移除缓存:
@Override
@Transactional
public void pauseIssue(Long id) {
// 1.查询旧优惠券
Coupon coupon = getById(id);
if (coupon == null) {
throw new BadRequestException("优惠券不存在");
}
// 2.当前券状态必须是未开始或进行中
CouponStatus status = coupon.getStatus();
if (status != UN_ISSUE && status != ISSUING) {
// 状态错误,直接结束
return;
}
// 3.更新状态
boolean success = lambdaUpdate()
.set(Coupon::getStatus, PAUSE)
.eq(Coupon::getId, id)
.in(Coupon::getStatus, UN_ISSUE, ISSUING)
.update();
if (!success) {
// 可能是重复更新,结束
log.error("重复暂停优惠券");
}
// 4.删除缓存
redisTemplate.delete(PromotionConstants.COUPON_CACHE_KEY_PREFIX + id);
}
至于过期移除缓存,需要编写一个定时任务,定期扫描优惠券并判断是否到达过期时间。如果到达则需要将优惠券状态置为发放结束,并移除Redis缓存。
领取优惠券的代码思路:注意因为此时为了保证其原子性,就锁住了整个逻辑代码,影响不大,因为是直接操作redis的,锁对象的话就是优惠券id。
package com.tianji.promotion.service.impl;
// ...略
import static com.tianji.promotion.constants.PromotionConstants.COUPON_CODE_MAP_KEY;
import static com.tianji.promotion.constants.PromotionConstants.COUPON_RANGE_KEY;
/**
* <p>
* 用户领取优惠券的记录,是真正使用的优惠券信息 服务实现类
* </p>
*
* @author 虎哥
*/
@Service
@RequiredArgsConstructor
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {
private final CouponMapper couponMapper;
private final IExchangeCodeService codeService;
private final StringRedisTemplate redisTemplate;
private final RabbitMqHelper mqHelper;
@Override
@Lock(name = "lock:coupon:#{couponId}")
public void receiveCoupon(Long couponId) {
// 1.查询优惠券
Coupon coupon = queryCouponByCache(couponId);
if (coupon == null) {
throw new BadRequestException("优惠券不存在");
}
// 2.校验发放时间
LocalDateTime now = LocalDateTime.now();
if (now.isBefore(coupon.getIssueBeginTime()) || now.isAfter(coupon.getIssueEndTime())) {
throw new BadRequestException("优惠券发放已经结束或尚未开始");
}
// 3.校验库存
if (coupon.getIssueNum() >= coupon.getTotalNum()) {
throw new BadRequestException("优惠券库存不足");
}
Long userId = UserContext.getUser();
// 4.校验每人限领数量
// 4.1.查询领取数量
String key = PromotionConstants.USER_COUPON_CACHE_KEY_PREFIX + couponId;
Long count = redisTemplate.opsForHash().increment(key, userId.toString(), 1);
// 4.2.校验限领数量
if(count > coupon.getUserLimit()){
throw new BadRequestException("超出领取数量");
}
// 5.扣减优惠券库存
redisTemplate.opsForHash().increment(
PromotionConstants.COUPON_CACHE_KEY_PREFIX + couponId, "totalNum", -1);
// 6.发送MQ消息
UserCouponDTO uc = new UserCouponDTO();
uc.setUserId(userId);
uc.setCouponId(couponId);
mqHelper.send(MqConstants.Exchange.PROMOTION_EXCHANGE, MqConstants.Key.COUPON_RECEIVE, uc);
}
private Coupon queryCouponByCache(Long couponId) {
// 1.准备KEY
String key = PromotionConstants.COUPON_CACHE_KEY_PREFIX + couponId;
// 2.查询
Map<Object, Object> objMap = redisTemplate.opsForHash().entries(key);
if (objMap.isEmpty()) {
return null;
}
// 3.数据反序列化
return BeanUtils.mapToBean(objMap, Coupon.class, false, CopyOptions.create());
}
// ...略
}
mq监听方的代码处理:
package com.tianji.promotion.handler;
import com.tianji.promotion.domain.dto.UserCouponDTO;
import com.tianji.promotion.service.IUserCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import static com.tianji.common.constants.MqConstants.Exchange.PROMOTION_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.COUPON_RECEIVE;
@RequiredArgsConstructor
@Component
public class PromotionMqHandler {
private final IUserCouponService userCouponService;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "coupon.receive.queue", durable = "true"),
exchange = @Exchange(name = PROMOTION_EXCHANGE, type = ExchangeTypes.TOPIC),
key = COUPON_RECEIVE
))
public void listenCouponReceiveMessage(UserCouponDTO uc){
userCouponService.checkAndCreateUserCoupon(uc);
}
}
// 移除了锁,这里不需要加锁了
@Transactional
@Override
public void checkAndCreateUserCoupon(UserCouponDTO uc) {
// 1.查询优惠券
Coupon coupon = couponMapper.selectById(uc.getCouponId());
if (coupon == null) {
throw new BizIllegalException("优惠券不存在!");
}
// 2.更新优惠券的已经发放的数量 + 1
int r = couponMapper.incrIssueNum(coupon.getId());
if (r == 0) {
throw new BizIllegalException("优惠券库存不足!");
}
// 3.新增一个用户券
saveUserCoupon(coupon, uc.getUserId());
// 4.更新兑换码状态
if (uc.getSerialNum()!= null) {
codeService.lambdaUpdate()
.set(ExchangeCode::getUserId, uc.getUserId())
.set(ExchangeCode::getStatus, ExchangeCodeStatus.USED)
.eq(ExchangeCode::getId, uc.getSerialNum())
.update();
}
}
2.优惠券推荐使用
业务流程分析:
功能需求:
-
判断一个优惠券是否可用,也就是检查订单金额是否达到优惠券使用门槛
-
按照优惠规则计算优惠金额,能够计算才能比较并找出最优方案
-
生成优惠券规则描述,目的是在页面直观的展示各种方案,供用户选择
四种优惠规则:
-
每满减:例如每满100减10
-
折扣:例如满100打9.5折,最大不超过50
-
无门槛:例如直接抵扣10元
-
满减:例如满100减15
数据库字段:
-
优惠门槛(thresholdAmount):也就是优惠的基本条件,例如满100
-
优惠值(discountValue):也就是具体折扣,例如减10、打9.5折
-
最大优惠(maxDiscountValue):最大折扣,限制折扣金额
优惠类型(discountType)对应到数据库的字段就是discount_type.
使用策略模式定义规则:
优惠券智能推荐接口:
思路分析:
入参与出参:
代码实现:
细筛:
private final ICouponScopeService scopeService;
private Map<Coupon, List<OrderCourseDTO>> findAvailableCoupon(
List<Coupon> coupons, List<OrderCourseDTO> courses) {
Map<Coupon, List<OrderCourseDTO>> map = new HashMap<>(coupons.size());
for (Coupon coupon : coupons) {
// 1.找出优惠券的可用的课程
List<OrderCourseDTO> availableCourses = courses;
if (coupon.getSpecific()) {
// 1.1.限定了范围,查询券的可用范围
List<CouponScope> scopes = scopeService.lambdaQuery().eq(CouponScope::getCouponId, coupon.getId()).list();
// 1.2.获取范围对应的分类id
Set<Long> scopeIds = scopes.stream().map(CouponScope::getBizId).collect(Collectors.toSet());
// 1.3.筛选课程
availableCourses = courses.stream()
.filter(c -> scopeIds.contains(c.getCateId())).collect(Collectors.toList());
}
if (CollUtils.isEmpty(availableCourses)) {
// 没有任何可用课程,抛弃
continue;
}
// 2.计算课程总价
int totalAmount = availableCourses.stream().mapToInt(OrderCourseDTO::getPrice).sum();
// 3.判断是否可用
Discount discount = DiscountStrategy.getDiscount(coupon.getDiscountType());
if (discount.canUse(totalAmount, coupon)) {
map.put(coupon, availableCourses);
}
}
return map;
}
排列组合:
package com.tianji.promotion.service.impl;
import com.tianji.api.dto.promotion.CouponDiscountDTO;
import com.tianji.api.dto.promotion.OrderCourseDTO;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.domain.po.CouponScope;
import com.tianji.promotion.mapper.UserCouponMapper;
import com.tianji.promotion.service.ICouponScopeService;
import com.tianji.promotion.service.IDiscountService;
import com.tianji.promotion.strategy.discount.Discount;
import com.tianji.promotion.strategy.discount.DiscountStrategy;
import com.tianji.promotion.utils.PermuteUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
public class DiscountServiceImpl implements IDiscountService {
private final UserCouponMapper userCouponMapper;
@Override
public List<CouponDiscountDTO> findDiscountSolution(List<OrderCourseDTO> orderCourses) {
// 1.查询我的所有可用优惠券
List<Coupon> coupons = userCouponMapper.queryMyCoupons(UserContext.getUser());
if (CollUtils.isEmpty(coupons)) {
return CollUtils.emptyList();
}
// 2.初筛
// 2.1.计算订单总价
int totalAmount = orderCourses.stream().mapToInt(OrderCourseDTO::getPrice).sum();
// 2.2.筛选可用券
List<Coupon> availableCoupons = coupons.stream()
.filter(c -> DiscountStrategy.getDiscount(c.getDiscountType()).canUse(totalAmount, c))
.collect(Collectors.toList());
if (CollUtils.isEmpty(availableCoupons)) {
return CollUtils.emptyList();
}
// 3.排列组合出所有方案
// 3.1.细筛(找出每一个优惠券的可用的课程,判断课程总价是否达到优惠券的使用需求)
Map<Coupon, List<OrderCourseDTO>> availableCouponMap = findAvailableCoupon(availableCoupons, orderCourses);
if (CollUtils.isEmpty(availableCouponMap)) {
return CollUtils.emptyList();
}
// 3.2.排列组合
availableCoupons = new ArrayList<>(availableCouponMap.keySet());
List<List<Coupon>> solutions = PermuteUtil.permute(availableCoupons);
// 3.3.添加单券的方案
for (Coupon c : availableCoupons) {
solutions.add(List.of(c));
}
// 4.计算方案的优惠明细
List<CouponDiscountDTO> list =
Collections.synchronizedList(new ArrayList<>(solutions.size()));
for (List<Coupon> solution : solutions) {
list.add(calculateSolutionDiscount(availableCouponMap, orderCourses, solution));
}
// 5.筛选最优解
return null;
}
// ... 略
}
计算明细:
private CouponDiscountDTO calculateSolutionDiscount(
Map<Coupon, List<OrderCourseDTO>> couponMap, List<OrderCourseDTO> courses, List<Coupon> solution) {
// 1.初始化DTO
CouponDiscountDTO dto = new CouponDiscountDTO();
// 2.初始化折扣明细的映射
Map<Long, Integer> detailMap = courses.stream().collect(Collectors.toMap(OrderCourseDTO::getId, oc -> 0));
// 3.计算折扣
for (Coupon coupon : solution) {
// 3.1.获取优惠券限定范围对应的课程
List<OrderCourseDTO> availableCourses = couponMap.get(coupon);
// 3.2.计算课程总价(课程原价 - 折扣明细)
int totalAmount = availableCourses.stream()
.mapToInt(oc -> oc.getPrice() - detailMap.get(oc.getId())).sum();
// 3.3.判断是否可用
Discount discount = DiscountStrategy.getDiscount(coupon.getDiscountType());
if (!discount.canUse(totalAmount, coupon)) {
// 券不可用,跳过
continue;
}
// 3.4.计算优惠金额
int discountAmount = discount.calculateDiscount(totalAmount, coupon);
// 3.5.计算优惠明细
calculateDiscountDetails(detailMap, availableCourses, totalAmount, discountAmount);
// 3.6.更新DTO数据
dto.getIds().add(coupon.getCreater());
dto.getRules().add(discount.getRule(coupon));
dto.setDiscountAmount(discountAmount + dto.getDiscountAmount());
}
return dto;
}
private void calculateDiscountDetails(Map<Long, Integer> detailMap, List<OrderCourseDTO> courses,
int totalAmount, int discountAmount) {
int times = 0;
int remainDiscount = discountAmount;
for (OrderCourseDTO course : courses) {
// 更新课程已计算数量
times++;
int discount = 0;
// 判断是否是最后一个课程
if (times == courses.size()) {
// 是最后一个课程,总折扣金额 - 之前所有商品的折扣金额之和
discount = remainDiscount;
} else {
// 计算折扣明细(课程价格在总价中占的比例,乘以总的折扣)
discount = discountAmount * course.getPrice() / totalAmount;
remainDiscount -= discount;
}
// 更新折扣明细
detailMap.put(course.getId(), discount + detailMap.get(course.getId()));
}
}
多线程计算方案的优惠明细:
这里的难点有两个:
-
1)线程任务是带返回值的任务
-
2)虽然是多线程运行,但是我们要等待所有线程都执行完毕后才返回结果
针对第二个点,我们可以利用JUC
包提供的工具CountDownLatch
来实现。
针对第一个点,我们则需要利用一个JDK1.8的新工具:CompletableFuture
来实现。
自定义线程池:
@Slf4j
@Configuration
public class PromotionConfig {
// ... 略
@Bean
public Executor discountSolutionExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 1.核心线程池大小
executor.setCorePoolSize(12);
// 2.最大线程池大小
executor.setMaxPoolSize(12);
// 3.队列大小
executor.setQueueCapacity(99999);
// 4.线程名称
executor.setThreadNamePrefix("discount-solution-calculator-");
// 5.拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
executor.initialize();
return executor;
}
}
为了保证线程安全使用Collections.synchronizedList来进行集合的添加。
@Slf4j
@Service
@RequiredArgsConstructor
public class DiscountServiceImpl implements IDiscountService {
private final UserCouponMapper userCouponMapper;
private final ICouponScopeService scopeService;
private final Executor discountSolutionExecutor;
@Override
public List<CouponDiscountDTO> findDiscountSolution(List<OrderCourseDTO> orderCourses) {
// 1.查询我的所有可用优惠券
List<Coupon> coupons = userCouponMapper.queryMyCoupons(UserContext.getUser());
if (CollUtils.isEmpty(coupons)) {
return CollUtils.emptyList();
}
// 2.初筛
// 2.1.计算订单总价
int totalAmount = orderCourses.stream().mapToInt(OrderCourseDTO::getPrice).sum();
// 2.2.筛选可用券
List<Coupon> availableCoupons = coupons.stream()
.filter(c -> DiscountStrategy.getDiscount(c.getDiscountType()).canUse(totalAmount, c))
.collect(Collectors.toList());
if (CollUtils.isEmpty(availableCoupons)) {
return CollUtils.emptyList();
}
// 3.排列组合出所有方案
// 3.1.细筛(找出每一个优惠券的可用的课程,判断课程总价是否达到优惠券的使用需求)
Map<Coupon, List<OrderCourseDTO>> availableCouponMap = findAvailableCoupon(availableCoupons, orderCourses);
if (CollUtils.isEmpty(availableCouponMap)) {
return CollUtils.emptyList();
}
// 3.2.排列组合
availableCoupons = new ArrayList<>(availableCouponMap.keySet());
List<List<Coupon>> solutions = PermuteUtil.permute(availableCoupons);
// 3.3.添加单券的方案
for (Coupon c : availableCoupons) {
solutions.add(List.of(c));
}
// 4.计算方案的优惠明细
List<CouponDiscountDTO> list = Collections.synchronizedList(new ArrayList<>(solutions.size()));
// 4.1.定义闭锁
CountDownLatch latch = new CountDownLatch(solutions.size());
for (List<Coupon> solution : solutions) {
// 4.2.异步计算
CompletableFuture
.supplyAsync(
() -> calculateSolutionDiscount(availableCouponMap, orderCourses, solution),
discountSolutionExecutor
).thenAccept(dto -> {
// 4.3.提交任务结果
list.add(dto);
latch.countDown();
});
}
// 4.4.等待运算结束
try {
latch.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("优惠方案计算被中断,{}", e.getMessage());
}
// TODO 5.筛选最优解
return null;
}
// ... 略
}