6.购物车的实现
购物车分为临时购物车和登录购物车,使用了redis的hash结构
整个存放结构:登录购物车key=gulimall:cart:userId field:skuId value:skuId对应的商品id
临时购物车key=gulimall:cart:uuid field:skuId value:skuId对应的商品id
添加商品到购物车都需要判断用户的登录状态,通过拦截器将用户的登录信息放到Threadlocal中,如果第一次使用购物车默认会为用户添加一个uuid
获取购物车:首先判断用户有没有登录,没有登录展示临时购物车,登录了首先要看临时购物车有没有数据,如果有数据就要合并到登录购物中去,合并完成删除临时购物车
7.feign的相关问题
解决方案:添加一个feign的请求拦截器,在调用之前设置一下请求头信息
代码:
/**
* feign配置类
**/
@Configuration
public class GulimallFeignConfig {
/**
* 注入拦截器
* feign调用时根据拦截器构造请求头,封装cookie解决远程调用时无法获取springsession
*/
@Bean("requestInterceptor")
public RequestInterceptor requestInterceptor() {
// 创建拦截器
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
System.out.println("feign远程调用,拦截器封装请求头...RequestInterceptor.apply");
// 1、使用RequestContextHolder拿到原生请求的请求头信(上下文环境保持器)
// 从ThreadLocal中获取请求头(要保证feign调用与controller请求处在同一线程环境)
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
HttpServletRequest request = requestAttributes.getRequest();// 获取controller请求对象
if (request != null) {
//2、同步请求头的数据(cookie)
String cookie = request.getHeader("Cookie");// 获取Cookie
template.header("Cookie", cookie);// 同步Cookie
}
}
}
};
}
}
解决方案:将原来共享的ThreadLocal中的信息放到自己的Threadlocal中
代码:
// 获取当前线程上下文环境器
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
CompletableFuture<Void> addressFuture = CompletableFuture.runAsync(() -> {
// 1.查询封装当前用户收货列表
// 同步上下文环境器,解决异步无法从ThreadLocal获取RequestAttributes
RequestContextHolder.setRequestAttributes(requestAttributes);
List<MemberAddressVO> address = memberFeignService.getAddress(member.getId());
result.setMemberAddressVos(address);
}, executor);
8.下单流程
选中商品添加到购物车,点击去结算,来到结算详情页,需要查询会员的收货地址列表、所有选中的购物项、优惠信息(会员的积分),查询库存信息(有货无货),商品总金额,应付金额,商品总数,最重要的就是防重令牌,确认号商品信息就可以点击去支付,这样的话库存就会锁定,订单如果出现异常或者不支付就会通过消息队列进行通知,让库存解锁
9.幂等性的解决方案
1.使用令牌机制(token)
2.各种锁机制(乐观锁,悲观锁,分布式锁)
3.唯一约束(数据库唯一约束,set nx)
4.防重表
5.全局请求唯一id
10.支付功能
使用的是支付宝的沙箱功能
涉及到了内网穿透