谷粒商城??订单服务??高级篇笔记十一-j9九游会登录

大家好!今天让小编来大家介绍下关于谷粒商城??服务??高级篇笔记十一的问题,以下是酷知号的小编对此问题的归纳整理,让我们一起来看看吧。

1.页面环境搭建

1.1 静态资源导入nginx

等待付款 ———>detail

订单页 ———>list

结算页 ———>confirm

收银页 ———> pay

1.2 配置host

# gulimall
192.168.157.128 gulimall.com
# search
192.168.157.128 search.gulimall.com
# item 商品详情
192.168.157.128 item.gulimall.com
#商城认证
192.168.157.128 auth.gulimall.com
#购物车
192.168.157.128 cart.gulimall.com
#订单
192.168.157.128 order.gulimall.com
#单点登录
127.0.0.1 ssoserver.com127.0.0.1 client1.com127.0.0.1 client2.com

1.3 配置网关

gulimall-gateway/src/main/resources/application.yml

        #订单- id: gulimall_order_routeuri: lb://gulimall-orderpredicates:- host=order.gulimall.com

1.4 开启注册发现

@enablediscoveryclient

1.5 新增依赖

        <dependency><groupid>org.springframework.bootgroupid><artifactid>spring-boot-starter-thymeleafartifactid>dependency>

1.6 修改各个页面的静态资源路径

src=" ===>src="/static/order/xxx/

herf=" ===>herf="/static/order/xxx/

1.7 测试

1.7.1 订单确认页

确认页前端代码:https://gitee.com/zhourui815/gulimall/blob/master/gulimall-order/src/main/resources/templates/confirm.html

order.gulimall.com/confirm.html

1.7.2 订单列表页

订单列表页前端代码:https://gitee.com/zhourui815/gulimall/blob/master/gulimall-order/src/main/resources/templates/list.html

谷粒商城订单 (gulimall.com)

1.7.3 订单详情页

订单详情页前端代码:https://gitee.com/zhourui815/gulimall/blob/master/gulimall-order/src/main/resources/templates/detail.html

order.gulimall.com/detail.html

1.7.4 订单支付页

订单支付页前端代码:https://gitee.com/zhourui815/gulimall/blob/master/gulimall-order/src/main/resources/templates/pay.html

order.gulimall.com/pay.html

2. 整合spring session

2.1 导入依赖

        <dependency><groupid>org.springframework.sessiongroupid><artifactid>spring-session-data-redisartifactid>dependency><dependency><groupid>org.springframework.bootgroupid><artifactid>spring-boot-starter-data-redisartifactid><exclusions><exclusion><groupid>io.lettucegroupid><artifactid>lettuce-coreartifactid>exclusion>exclusions>dependency><dependency><groupid>redis.clientsgroupid><artifactid>jedisartifactid>dependency>

2.2 开启spring session

@enableredishttpsession  //整合redis作为session存储

2.3 配置spring session存储方式

  redis:host: 192.168.157.128session:store-type: redis

2.4 springsession 自定义

package site.zhourui.gulimall.order.config;import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.serializer.genericjackson2jsonredisserializer;
import org.springframework.data.redis.serializer.redisserializer;
import org.springframework.session.web.http.cookieserializer;
import org.springframework.session.web.http.defaultcookieserializer;/*** @author zr* @date 2021/12/12 10:29*/
@configuration
public class gulimallsessionconfig {@beanpublic cookieserializer cookieserializer() {defaultcookieserializer cookieserializer = new defaultcookieserializer();//放大作用域cookieserializer.setdomainname("gulimall.com");cookieserializer.setcookiename("gulisession");cookieserializer.setcookiemaxage(60*60*24*7);return cookieserializer;}//session存储对象方式json,默认jdk@beanpublic redisserializer<object> springsessiondefaultredisserializer() {return new genericjackson2jsonredisserializer();}
}

2.5 整合后效果

可以实现登录成功后用户信息共享

3. 整合线程池

3.1 自定义线程池配置

gulimall-order/src/main/ja/site/zhourui/gulimall/order/config/mythreadconfig.ja

package site.zhourui.gulimall.order.config;import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;import ja.util.concurrent.executors;
import ja.util.concurrent.linkedblockingdeque;
import ja.util.concurrent.threadpoolexecutor;
import ja.util.concurrent.timeunit;/*** @author zr* @date 2021/11/28 10:12*/@configuration
public class mythreadconfig {@beanpublic threadpoolexecutor threadpoolexecutor(threadpoolconfigproperties pool) {return new threadpoolexecutor(pool.getcoresize(),pool.getmaxsize(),pool.getkeepalivetime(),timeunit.seconds,new linkedblockingdeque<>(100000),executors.defaultthreadfactory(),new threadpoolexecutor.abortpolicy());}
}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/config/threadpoolconfigproperties.ja

package site.zhourui.gulimall.order.config;import lombok.data;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.stereotype.component;@configurationproperties(prefix = "gulimall.thread")
@component
@data
public class threadpoolconfigproperties {private integer coresize;private integer maxsize;private integer keepalivetime;
}

3.2 配置

gulimall:thread:core-size: 20max-size: 200keep-alive-time: 10

4. 订单中心(理论)

电商系统涉及到 3 流, 分别时信息流, 资金流, 物流, 而订单系统作为中枢将三者有机的集合起来。订单模块是电商系统的枢纽, 在订单这个环节上需求获取多个模块的数据和信息, 同时对这些信息进行加工处理后流向下个环节, 这一系列就构成了订单的信息流通。

4.1 订单的构成

4.1.1 用户信息

用户信息包括用户账号、 用户等级、 用户的收货地址、 收货人、 收货人电话等组成, 用户账户需要绑定手机号码, 但是用户绑定的手机号码不一定是收货信息上的电话。 用户可以添加多个收货信息, 用户等级信息可以用来和促销系统进行匹配, 获取商品折扣, 同时用户等级还可以获取积分的奖励等

4.1.2 订单基础信息

订单基础信息是订单流转的核心, 其包括订单类型、 父/子订单、 订单编号、 订单状态、 订单流转的时间等。

(1) 订单类型包括实体商品订单和虚拟订单商品等, 这个根据商城商品和服务类型进行区分。
(2) 同时订单都需要做父子订单处理, 之前在初创公司一直只有一个订单, 没有做父子订单处理后期需要进行拆单的时候就比较麻烦, 尤其是多商户商场, 和不同仓库商品的时候,父子订单就是为后期做拆单准备的。
(3) 订单编号不多说了, 需要强调的一点是父子订单都需要有订单编号, 需要完善的时候可以对订单编号的每个字段进行统一定义和诠释。
(4) 订单状态记录订单每次流转过程, 后面会对订单状态进行单独的说明。
(5) 订单流转时间需要记录下单时间, 支付时间, 发货时间, 结束时间/关闭时间等等

4.1.3 商品信息

商品信息从商品库中获取商品的 sku 信息、 图片、 名称、 属性规格、 商品单价、 商户信息等, 从用户下单行为记录的用户下单数量, 商品合计价格等。

4.1.4 优惠信息

优惠信息记录用户参与的优惠活动, 包括优惠促销活动, 比如满减、 满赠、 秒杀等, 用户使用的优惠券信息, 优惠券满足条件的优惠券需要默认展示出来, 具体方式已在之前的优惠券篇章做过详细介绍, 另外还虚拟币抵扣信息等进行记录。

4.1.4.1为什么把优惠信息单独拿出来而不放在支付信息里面呢?

因为优惠信息只是记录用户使用的条目, 而支付信息需要加入数据进行计算, 所以做为区分。

4.1.5 支付信息

( 1) 支付流水单号, 这个流水单号是在唤起网关支付后支付通道返回给电商业务平台的支付流水号, 财务通过订单号和流水单号与支付通道进行对账使用。
( 2) 中国的支付方式用户使用的 中国的支付方式, 比如微信支付、 支付宝支付、 钱包支付、 快捷支付等。 中国的支付方式有时候可能有两个——余额支付 第三方支付。
( 3) 商品总金额, 每个商品加总后的金额; 运费, 物流产生的费用; 优惠总金额, 包括促销活动的优惠金额, 优惠券优惠金额, 虚拟积分或者虚拟币抵扣的金额, 会员折扣的金额等之和; 实付金额, 用户实际需要付款的金额。用户实付金额=商品总金额 运费-优惠总金额

4.1.6 物流信息

物流信息包括配送方式, 物流公司, 物流单号, 物流状态, 物流状态可以通过第三方接口来获取和向用户展示物流每个状态节点。

4.2 订单状态

  1. 待付款
    用户提交订单后, 订单进行预下单, 目前主流电商网站都会唤起支付, 便于用户快速完成支付, 需要注意的是待付款状态下可以对库存进行锁定, 锁定库存需要配置支付超时时间, 超时后将自动取消订单, 订单变更关闭状态。
  2. 已付款/待发货
    用户完成订单支付, 订单系统需要记录支付时间, 支付流水单号便于对账, 订单下放到 wms系统, 仓库进行调拨, 配货, 分拣, 出库等操作。
  3. 待收货/已发货
    仓储将商品出库后, 订单进入物流环节, 订单系统需要同步物流信息, 便于用户实时知悉物品物流状态
  4. 已完成
    用户确认收货后, 订单交易完成。 后续支付侧进行结算, 如果订单存在问题进入售后状态
  5. 已取消
    付款之前取消订单。 包括超时未付款或用户商户取消订单都会产生这种订单状态。
  6. 售后中
    用户在付款后申请退款, 或商家发货后用户申请退换货。售后也同样存在各种状态, 当发起售后申请后生成售后订单, 售后订单状态为待审核, 等待商家审核, 商家审核通过后订单状态变更为待退货, 等待用户将商品寄回, 商家收货后订单状态更新为待退款状态, 退款到用户原账户后订单状态更新为售后成功。

4.3 订单流程

订单流程是指从订单产生到完成整个流转的过程, 从而行程了一套标准流程规则。 而不同的产品类型或业务类型在系统中的流程会千差万别, 比如上面提到的线上实物订单和虚拟订单的流程, 线上实物订单与 o2o 订单等, 所以需要根据不同的类型进行构建订单流程。不管类型如何订单都包括正向流程和逆向流程, 对应的场景就是购买商品和退换货流程, 正向流程就是一个正常的网购步骤: 订单生成–>支付订单–>卖家发货–>确认收货–>交易成功。而每个步骤的背后, 订单是如何在多系统之间交互流转的, 可概括如下图

4.3.1 订单创建与支付 (重点)

  1. 订单创建前需要预览订单, 选择收货信息等
  2. 订单创建需要锁定库存, 库存有才可创建, 否则不能创建
  3. 订单创建后超时未支付需要解锁库存
  4. 支付成功后, 需要进行拆单, 根据商品打包方式, 所在仓库, 物流等进行拆单
  5. 支付的每笔流水都需要记录, 以待查账
  6. 订单创建, 支付成功等状态都需要给 mq 发送消息, 方便其他系统感知订阅

4.3.2 逆向流程

  1. 修改订单, 用户没有提交订单, 可以对订单一些信息进行修改, 比如配送信息,优惠信息, 及其他一些订单可修改范围的内容, 此时只需对数据进行变更即可。
  2. 订单取消, 用户主动取消订单和用户超时未支付, 两种情况下订单都会取消订单, 而超时情况是系统自动关闭订单, 所以在订单支付的响应机制上面要做支付的限时处理, 尤其是在前面说的下单减库存的情形下面, 可以保证快速的释放库存。另外需要需要处理的是促销优惠中使用的优惠券, 权益等视平台规则, 进行相应补回给用户。
  3. 退款, 在待发货订单状态下取消订单时, 分为缺货退款和用户申请退款。 如果是全部退款则订单更新为关闭状态, 若只是做部分退款则订单仍需进行进行, 同时生成一条退款的售后订单, 走退款流程。 退款金额需原路返回用户的账户。
  4. 发货后的退款, 发生在仓储货物配送, 在配送过程中商品遗失, 用户拒收, 用户收货后对商品不满意, 这样情况下用户发起退款的售后诉求后, 需要商户进行退款的审核, 双方达成一致后, 系统更新退款状态, 对订单进行退款操作, 金额原路返回用户的账户, 同时关闭原订单数据。 仅退款情况下暂不考虑仓库系统变化。 如果发生双方协调不一致情况下, 可以申请平台客服介入。 在退款订单商户不处理的情况下, 系统需要做限期判断, 比如 5 天商户不处理, 退款单自动变更同意退款。

5. 订单中心(代码)

5.1 订单登录拦截

因为订单系统必然涉及到用户信息,因此进入订单系统的请求必须是已经登录的,所以我们需要通过拦截器对未登录订单请求进行拦截

gulimall-order/src/main/ja/site/zhourui/gulimall/order/interceptor/loginuserinterceptor.ja

package site.zhourui.gulimall.order.interceptor;/*** @author zr* @date 2021/12/21 22:04*/import org.springframework.stereotype.component;
import org.springframework.util.antpathmatcher;
import org.springframework.web.servlet.handlerinterceptor;
import site.zhourui.common.constant.authserverconstant;
import site.zhourui.common.vo.memberresponsevo;import jax.servlet.http.httpservletrequest;
import jax.servlet.http.httpservletresponse;
import jax.servlet.http.httpsession;
import ja.io.printwriter;import static site.zhourui.common.constant.authserverconstant.login_user;/*** 登录拦截器* 从session中获取了登录信息(redis中),封装到了threadlocal中*/
@component
public class loginuserinterceptor implements handlerinterceptor {public static threadlocal<memberresponsevo> loginuser = new threadlocal<>();@overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {httpsession session = request.getsession();memberresponsevo memberresponsevo = (memberresponsevo) session.getattribute(authserverconstant.login_user);if (memberresponsevo != null) {loginuser.set(memberresponsevo);return true;}else {session.setattribute("msg","请先登录");response.sendredirect("http://auth.gulimall.com/login.html");return false;}}
}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/config/orderwebconfig.ja

package site.zhourui.gulimall.order.config;import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.interceptorregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
import site.zhourui.gulimall.order.interceptor.loginuserinterceptor;/*** @author zr* @date 2021/12/21 22:05*/
@configuration
public class orderwebconfig implements webmvcconfigurer {@autowiredprivate loginuserinterceptor loginuserinterceptor;@overridepublic void addinterceptors(interceptorregistry registry) {registry.addinterceptor(loginuserinterceptor).addpathpatterns("/**");}
}

5.2 订单确认页

5.2.1 模型抽取

确认页提交数据

gulimall-order/src/main/ja/site/zhourui/gulimall/order/vo/orderconfirmvo.ja

package site.zhourui.gulimall.order.vo;import lombok.getter;
import lombok.setter;import ja.math.bigdecimal;
import ja.util.list;
import ja.util.map;/*** 订单确认页需要用的数据* @author zr* @date 2021/12/21 22:22*/
public class orderconfirmvo {@getter@setterlist<memberaddressvo> memberaddressvos;/** 会员收获地址列表 **/@getter @setterlist<orderitemvo> items;    /** 所有选中的购物项【购物车中的所有项】 **/@getter @setterprivate integer integration;/** 优惠券(会员积分) **//** todo 防止重复提交的令牌 幂等性**/@getter @setterprivate string ordertoken;@getter @settermap<long,boolean> stocks;public integer getcount() {integer count = 0;if (items != null && items.size() > 0) {for (orderitemvo item : items) {count  = item.getcount();}}return count;}/** 总商品金额 **///bigdecimal total;//计算订单总额public bigdecimal gettotal() {bigdecimal totalnum = bigdecimal.zero;if (items != null && items.size() > 0) {for (orderitemvo item : items) {//计算当前商品的总价格bigdecimal itemprice = item.getprice().multiply(new bigdecimal(item.getcount().tostring()));//再计算全部商品的总价格totalnum = totalnum.add(itemprice);}}return totalnum;}/** 应付总额 **///bigdecimal payprice;public bigdecimal getpayprice() {return gettotal();}
}

确认页提交数据模型还需要地址信息

gulimall-order/src/main/ja/site/zhourui/gulimall/order/vo/memberaddressvo.ja

package site.zhourui.gulimall.order.vo;import lombok.data;/*** 地址信息* @author zr* @date 2021/12/21 22:24*/
@data
public class memberaddressvo {private long id;/*** member_id*/private long memberid;/*** 收货人姓名*/private string name;/*** 电话*/private string phone;/*** 邮政编码*/private string postcode;/*** 省份/直辖市*/private string province;/*** 城市*/private string city;/*** 区*/private string region;/*** 详细地址(街道)*/private string detailaddress;/*** 省市区代码*/private string areacode;/*** 是否默认*/private integer defaultstatus;}

确认页提交数据模型还需要订单行信息

gulimall-order/src/main/ja/site/zhourui/gulimall/order/vo/orderitemvo.ja

package site.zhourui.gulimall.order.vo;import lombok.data;import ja.math.bigdecimal;
import ja.util.list;/*** 购物项内容* @author zr* @date 2021/12/21 22:23*/
@data
public class orderitemvo {private long skuid;             // skuidprivate boolean check = true;   // 是否选中private string title;           // 标题private string image;           // 图片private list<string> skuattrvalues;// 商品销售属性private bigdecimal price;       // 单价private integer count;          // 当前商品数量private bigdecimal totalprice;  // 总价private bigdecimal weight = new bigdecimal("0.085");// 商品重量
}

5.2.2 提交确认订单

5.2.2.1 订单确认页流程

1、远程调用:获取所有收货地址【member-ums表】
2、远程调用:所有选中的商品(最新价格-远程调用)【cart-redis中】【product-查询最新价格】
3、查询用户积分【session的用户信息中】
4、订单总额【根据所有选中的价格之和 求得】
5、应付总额【暂时跟订单总额相等】【优惠卡等功能不做,直接用积分】

6、查询每个商品是否有货【批量查询ware服务】
7、收货地址高亮【选中地址调用ajax直接远程调用ware计算运费【远程调用会员服务member传入addrid获取详细地址】
wareinfocontroller /fare
接口返回运费信息,和地址信息
8、防重令牌【防止用户多次 提交订单】【点击提交订单后,数据库只保存一条订单信息(幂等性,提交1次和多次结果是一致的)】

5.2.2.2 去到订单确认页面

返回订单确认页所需要的数据orderconfirmvo

gulimall-order/src/main/ja/site/zhourui/gulimall/order/web/orderwebcontroller.ja

    /*** 去结算确认页* @param model* @param request* @return* @throws executionexception* @throws interruptedexception*/@getmapping(value = "/totrade")public string totrade(model model, httpservletrequest request) throws executionexception, interruptedexception {orderconfirmvo confirmvo = orderservice.confirmorder();model.addattribute("confirmorderdata",confirmvo);//展示订单确认的数据return "confirm";}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    orderconfirmvo confirmorder();

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

  1. 判断用户登录信息
  2. 远程查询所有的收获地址列表
  3. 远程查询购物车所有选中的购物项
  4. 远程查询商品库存信息
  5. 查询用户积分
  6. 价格数据自动计算
  7. 防重令牌(防止表单重复提交)
/*** 订单确认页返回需要用的数据* @return*/@overridepublic orderconfirmvo confirmorder() throws executionexception, interruptedexception {//构建orderconfirmvoorderconfirmvo confirmvo = new orderconfirmvo();//获取当前用户登录的信息memberresponsevo memberresponsevo = loginuserinterceptor.loginuser.get();//todo :获取当前线程请求头信息(解决feign异步调用丢失请求头问题)requestattributes requestattributes = requestcontextholder.getrequestattributes();//开启第一个异步任务completablefuture<void> addressfuture = completablefuture.runasync(() -> {//每一个线程都来共享之前的请求数据requestcontextholder.setrequestattributes(requestattributes);//1、远程查询所有的收获地址列表list<memberaddressvo> address = memberfeignservice.getaddress(memberresponsevo.getid());confirmvo.setmemberaddressvos(address);}, threadpoolexecutor);//开启第二个异步任务completablefuture<void> cartinfofuture = completablefuture.runasync(() -> {//每一个线程都来共享之前的请求数据【解决异步threadlocal 无法共享数据】requestcontextholder.setrequestattributes(requestattributes);//2、远程查询购物车所有选中的购物项list<orderitemvo> currentcartitems = cartfeignservice.getcurrentcartitems();confirmvo.setitems(currentcartitems);//feign在远程调用之前要构造请求,调用很多的拦截器}, threadpoolexecutor).thenrunasync(() -> {list<orderitemvo> items = confirmvo.getitems();//获取全部商品的idlist<long> skuids = items.stream().map((itemvo -> itemvo.getskuid())).collect(collectors.tolist());//3、远程查询商品库存信息r skuhasstock = wmsfeignservice.getskuhasstock(skuids);list<skustockvo> skustockvos = skuhasstock.getdata("data", new typereference<list<skustockvo>>() {});if (skustockvos != null && skustockvos.size() > 0) {//将skustockvos集合转换为mapmap<long, boolean> skuhasstockmap = skustockvos.stream().collect(collectors.tomap(skustockvo::getskuid, skustockvo::gethasstock));confirmvo.setstocks(skuhasstockmap);}},threadpoolexecutor);//4、、查询用户积分integer integration = memberresponsevo.getintegration();confirmvo.setintegration(integration);//5、、价格数据自动计算//todo 5、防重令牌(防止表单重复提交)//为用户设置一个token,三十分钟过期时间(存在redis)string token = uuid.randomuuid().tostring().replace("-", "");redistemplate.opsforvalue().set(user_order_token_prefixmemberresponsevo.getid(),token,30, timeunit.minutes);confirmvo.setordertoken(token);completablefuture.allof(addressfuture,cartinfofuture).get();return confirmvo;}
5.2.2.2.1 远程查询所有的收获地址列表

模拟创建两条该用户地址信息

gulimall-member/src/main/ja/site/zhourui/gulimall/member/service/memberreceiveaddressservice.ja

    list<memberreceiveaddressentity> getaddress(long memberid);

gulimall-member/src/main/ja/site/zhourui/gulimall/member/service/impl/memberreceiveaddressserviceimpl.ja

    @overridepublic list<memberreceiveaddressentity> getaddress(long memberid) {list<memberreceiveaddressentity> addresslist = this.basemapper.selectlist(new querywrapper<memberreceiveaddressentity>().eq("member_id", memberid));return addresslist;}

gulimall-member/src/main/ja/site/zhourui/gulimall/member/controller/memberreceiveaddresscontroller.ja

    /*** 根据会员id查询会员的所有地址* @param memberid* @return*/@getmapping(value = "/{memberid}/address")public list<memberreceiveaddressentity> getaddress(@pathvariable("memberid") long memberid) {list<memberreceiveaddressentity> addresslist = memberreceiveaddressservice.getaddress(memberid);return addresslist;}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/feign/memberfeignservice.ja

package site.zhourui.gulimall.order.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import site.zhourui.gulimall.order.vo.memberaddressvo;import ja.util.list;/*** @author zr* @date 2021/12/23 15:05*/
@feignclient("gulimall-member")
public interface memberfeignservice {/*** 查询当前用户的全部收货地址* @param memberid* @return*/@getmapping(value = "/member/memberreceiveaddress/{memberid}/address")list<memberaddressvo> getaddress(@pathvariable("memberid") long memberid);
}
5.2.2.2.2 远程查询购物车所有选中的购物项

gulimall-cart/src/main/ja/site/zhourui/gulimall/cart/service/cartservice.ja

    /*** 获取当前用户的购物车所有商品项* @return*/list<cartitemvo> getusercartitems();

gulimall-cart/src/main/ja/site/zhourui/gulimall/cart/service/impl/cartserviceimpl.ja

    /*** 远程调用:订单服务调用【更新最新价格】* 获取当前用户购物车所有选中的商品项check=true【从redis中取】*/@overridepublic list<cartitemvo> getusercartitems() {list<cartitemvo> cartitemvolist = new arraylist<>();//获取当前用户登录的信息userinfoto userinfoto = cartinterceptor.tothreadlocal.get();//如果用户未登录直接返回nullif (userinfoto.getuserid() == null) {return null;} else {//获取购物车项string cartkey =cartconstant.cart_prefix  userinfoto.getuserid();//获取所有的list<cartitemvo> cartitems = getcartitems(cartkey);if (cartitems == null) {throw new cartexceptionhandler();}//筛选出选中的cartitemvolist = cartitems.stream().filter(items -> items.getcheck()).map(item -> {//更新为最新的价格(查询数据库)// redis中的价格不是最新的bigdecimal price = productfeignservice.getprice(item.getskuid());item.setprice(price);return item;}).collect(collectors.tolist());}return cartitemvolist;}

gulimall-cart/src/main/ja/site/zhourui/gulimall/cart/controller/cartcontroller.ja

    /*** 订单服务调用:【购物车页面点击确认订单时】* 返回所有选中的商品项【从redis中取】* 并且要获取最新的商品价格信息,而不是redis中的数据** 获取当前用户的购物车所有商品项*/@getmapping(value = "/currentusercartitems")@responsebodypublic list<cartitemvo> getcurrentcartitems() {list<cartitemvo> cartitemvolist = cartservice.getusercartitems();return cartitemvolist;}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/feign/cartfeignservice.ja

package site.zhourui.gulimall.order.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import site.zhourui.gulimall.order.vo.orderitemvo;import ja.util.list;/*** @author zr* @date 2021/12/23 15:06*/
@feignclient("gulimall-cart")
public interface cartfeignservice {/*** 查询当前用户购物车选中的商品项* @return*/@getmapping(value = "/currentusercartitems")list<orderitemvo> getcurrentcartitems();}
5.2.2.2.3 远程查询商品库存信息

gulimall-order/src/main/ja/site/zhourui/gulimall/order/vo/skustockvo.ja

返回库存信息的vo

	package site.zhourui.gulimall.order.vo;import lombok.data;/*** 库存vo* @author zr* @date 2021/12/23 15:53*/
@data
public class skustockvo {private long skuid;private boolean hasstock;}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareskuservice.ja

    /*** 判断是否有库存*/list<skuhasstockvo> getskushasstock(list<long> skuids);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareskuserviceimpl.ja

    /***  检查sku 是否有库存*/@overridepublic list<skuhasstockvo> getskushasstock(list<long> skuids) {list<skuhasstockvo> vos = skuids.stream().map(skuid -> {skuhasstockvo vo = new skuhasstockvo();// 1、不止一个仓库有,多个仓库都有库存 sum// 2、锁定库存是别人下单但是还没下完long count = basemapper.getskustock(skuid);vo.setskuid(skuid);vo.sethasstock(count == null ? false : count > 0);return vo;}).collect(collectors.tolist());return vos;}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/controller/wareskucontroller.ja

    /*** 查询sku是否有库存*/@postmapping("/hasstock")// @requirespermissions("ware:waresku:list")public r getskushasstock(@requestbody list<long> skuids){// sku_id  stocklist<skuhasstockvo> vos = wareskuservice.getskushasstock(skuids);return r.ok().setdata(vos);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/feign/wmsfeignservice.ja

package site.zhourui.gulimall.order.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestbody;
import org.springframework.web.bind.annotation.requestparam;
import site.zhourui.common.utils.r;import ja.util.list;/*** @author zr* @date 2021/12/23 15:06*/
@feignclient("gulimall-ware")
public interface wmsfeignservice {/*** 查询sku是否有库存*/@postmapping(value = "/ware/waresku/hasstock")r getskuhasstock(@requestbody list<long> skuids);}
5.2.2.2.4 多线程异步编排

之前在章节3整合过线程池了,只需导入

    @autowiredprivate threadpoolexecutor threadpoolexecutor;
5.2.2.3 feign远程调用丢失请求头

原因:feign发送请求时构造的requesttemplate没有请求头(该请求头为空),请求参数等信息【cookie没了】

导致在cart服务中,拦截器拦截获取session中的登录信息,获取不到userid【没有cookie】

解决:同步新、老请求(老请求就是/totrade请求,带有cookie数据)的cookie

原理: feign在远程调用之前要构造请求,调用很多的拦截器(debug,查看到会调用 拦截器)

gulimall-order/src/main/ja/site/zhourui/gulimall/order/config/gulifeignconfig.ja

package site.zhourui.gulimall.order.config;/*** @author zr* @date 2021/12/23 17:03*/import feign.requestinterceptor;
import feign.requesttemplate;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.context.request.requestcontextholder;
import org.springframework.web.context.request.servletrequestattributes;import jax.servlet.http.httpservletrequest;/*** feign拦截器功能* 解决feign 远程请求头丢失问题**/
@configuration
public class gulifeignconfig {@bean("requestinterceptor")public requestinterceptor requestinterceptor() {requestinterceptor requestinterceptor = new requestinterceptor() {@overridepublic void apply(requesttemplate template) {system.out.println("feign远程调用,拦截包装请求头");//1、使用requestcontextholder拿到刚进来的请求数据servletrequestattributes requestattributes = (servletrequestattributes) requestcontextholder.getrequestattributes();if (requestattributes != null) {httpservletrequest request = requestattributes.getrequest();//老请求if (request != null) {//2、同步请求头的数据(主要是cookie)//把老请求的cookie值放到新请求上来,进行一个同步string cookie = request.getheader("cookie");template.header("cookie", cookie);}}}};return requestinterceptor;}}
5.2.2.4 feign异步情况丢失上下文问题

导致拦截器中 空指针异常
1、先在主线程的threadlocal中获取 请求头数据

        requestattributes requestattributes = requestcontextholder.getrequestattributes();

2、再在新线程给threadlocal设置 请求头数据【否则获取不到数据,不是同一个线程】

          //每一个线程都来共享之前的请求数据【解决异步threadlocal 无法共享数据】requestcontextholder.setrequestattributes(requestattributes);
5.2.2.5 模拟运费效果

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareinfoservice.ja

    /*** 获取运费和收货地址信息* @param addrid* @return*/farevo getfare(long addrid);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareinfoserviceimpl.ja

模拟运费,真实情况下需要计算得出

    /*** 计算运费* @param addrid* @return*/@overridepublic farevo getfare(long addrid) {farevo farevo = new farevo();//收获地址的详细信息r addrinfo = memberfeignservice.info(addrid);memberaddressvo memberaddressvo = addrinfo.getdata("memberreceiveaddress",new typereference<memberaddressvo>() {});if (memberaddressvo != null) {string phone = memberaddressvo.getphone();//截取用户手机号码最后一位作为我们的运费计算//1558022051string fare = phone.substring(phone.length() - 1);bigdecimal bigdecimal = new bigdecimal(fare);farevo.setfare(bigdecimal);farevo.setaddress(memberaddressvo);return farevo;}return null;}

需要获取用户选择的地址信息(远程调用)

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/feign/memberfeignservice.ja

    /*** 根据id获取用户地址信息* @param id* @return*/@requestmapping("/member/memberreceiveaddress/info/{id}")r info(@pathvariable("id") long id);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/controller/wareinfocontroller.ja

    /*** 获取运费信息,订单服务远程调用* @return*/@getmapping(value = "/fare")public r getfare(@requestparam("addrid") long addrid) {farevo fare = wareinfoservice.getfare(addrid);return r.ok().setdata(fare);}

前端页面当用户地址切换时,查询出运费及订单总金额为用户展示

测试需要创建两条地址信息数据

5.2.2.6 创建防重令牌

令牌前缀常量

package site.zhourui.gulimall.order.constant;/*** @author zr* @date 2021/12/23 22:05*/
public class orderconstant {public static final string user_order_token_prefix = "order:token";}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

        //todo 5、防重令牌(防止表单重复提交)//为用户设置一个token,三十分钟过期时间(存在redis)string token = uuid.randomuuid().tostring().replace("-", "");redistemplate.opsforvalue().set(user_order_token_prefixmemberresponsevo.getid(),token,30, timeunit.minutes);confirmvo.setordertoken(token);
5.2.2.7 提交订单

注意:创建的订单号很长,注意将oms_orderoms_order_item数据库表中的order_sn字段调大至50,否则会报错

  • 下单:去创建订单,验令牌,验价格,锁库存
  • 提交订单成功,则携带返回数据转发至支付页面
  • 提交订单失败,则携带错误信息重定向至确认页

gulimall-order/src/main/ja/site/zhourui/gulimall/order/web/orderwebcontroller.ja

    @requestmapping("/submitorder")public string submitorder(ordersubmitvo submitvo, model model, redirectattributes attributes) {try{submitorderresponsevo responsevo=orderservice.submitorder(submitvo);integer code = responsevo.getcode();if (code==0){model.addattribute("order", responsevo.getorder());return "pay";}else {string msg = "下单失败;";switch (code) {case 1:msg  = "防重令牌校验失败";break;case 2:msg  = "商品价格发生变化";break;}attributes.addflashattribute("msg", msg);return "redirect:http://order.gulimall.com/totrade";}}catch (exception e){if (e instanceof nostockexception){string msg = "下单失败,商品无库存";attributes.addflashattribute("msg", msg);}return "redirect:http://order.gulimall.com/totrade";}}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 提交订单* @param vo* @return*/// @transactional(isolation = isolation.read_committed) 设置事务的隔离级别// @transactional(propagation = propagation.required)   设置事务的传播级别// @globaltransactional(rollbackfor = exception.class)@transactional@overridepublic submitorderresponsevo submitorder(ordersubmitvo vo) {confirmvothreadlocal.set(vo);submitorderresponsevo responsevo = new submitorderresponsevo();//去创建、下订单、验令牌、验价格、锁定库存...//获取当前用户登录的信息memberresponsevo memberresponsevo = loginuserinterceptor.loginuser.get();responsevo.setcode(0);//1、验证令牌是否合法【令牌的对比和删除必须保证原子性】string script = "if redis.call('get', keys[1]) == argv[1] then return redis.call('del', keys[1]) else return 0 end";string ordertoken = vo.getordertoken();//通过lure脚本原子验证令牌和删除令牌long result = redistemplate.execute(new defaultredisscript<long>(script, long.class),arrays.aslist(user_order_token_prefix  memberresponsevo.getid()),ordertoken);if (result == 0l) {//令牌验证失败responsevo.setcode(1);return responsevo;} else {//令牌验证成功//1、创建订单、订单项等信息ordercreateto order = createorder();//2、验证价格bigdecimal payamount = order.getorder().getpayamount();bigdecimal payprice = vo.getpayprice();if (math.abs(payamount.subtract(payprice).doublevalue()) < 0.01) {//金额对比//todo 3、保存订单seorder(order);//4、库存锁定,只要有异常,回滚订单数据//订单号、所有订单项信息(skuid,skunum,skuname)wareskulockvo lockvo = new wareskulockvo();lockvo.setordersn(order.getorder().getordersn());//获取出要锁定的商品数据信息【order里面存储的是entity】list<orderitemvo> orderitemvos = order.getorderitems().stream().map((item) -> {orderitemvo orderitemvo = new orderitemvo();orderitemvo.setskuid(item.getskuid());orderitemvo.setcount(item.getskuquantity());orderitemvo.settitle(item.getskuname());return orderitemvo;}).collect(collectors.tolist());lockvo.setlocks(orderitemvos);//todo 调用远程锁定库存的方法//出现的问题:扣减库存成功了,但是由于网络原因超时,出现异常,导致订单事务回滚,库存事务不回滚( 中国的解决方案:seata)//为了保证高并发,不推荐使用seata,因为是加锁,并行化,提升不了效率,可以发消息给库存服务r r = wmsfeignservice.orderlockstock(lockvo);if (r.getcode() == 0) {//锁定成功responsevo.setorder(order.getorder());//int i = 10/0;//todo 订单创建成功,发送消息给mq
//                    rabbittemplate.convertandsend("order-event-exchange","order.create.order",order.getorder());//删除购物车里的数据
//                    redistemplate.delete(cart_prefix memberresponsevo.getid());return responsevo;} else {//锁定失败string msg = (string) r.get("msg");throw new nostockexception(msg);// responsevo.setcode(3);// return responsevo;}} else {responsevo.setcode(2);return responsevo;}}}
5.2.2.7.1 验证防重令牌
        //1、验证令牌是否合法【令牌的对比和删除必须保证原子性】string script = "if redis.call('get', keys[1]) == argv[1] then return redis.call('del', keys[1]) else return 0 end";string ordertoken = vo.getordertoken();//通过lure脚本原子验证令牌和删除令牌long result = redistemplate.execute(new defaultredisscript<long>(script, long.class),arrays.aslist(user_order_token_prefix  memberresponsevo.getid()),ordertoken);if (result == 0l) {//令牌验证失败responsevo.setcode(1);return responsevo;} else {//令牌验证成功//1、创建订单、订单项等信息//2、验证价格//3、保存订单//4、库存锁定,只要有异常,回滚订单数据}
5.2.2.7.2 创建订单、订单项等信息

需要远程调用获取spu信息

gulimall-order/src/main/ja/site/zhourui/gulimall/order/feign/productfeignservice.ja

package site.zhourui.gulimall.order.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import site.zhourui.common.utils.r;/*** @author zr* @date 2021/12/24 9:58*/
@feignclient("gulimall-product")
public interface productfeignservice {/*** 根据skuid查询spu的信息* @param skuid* @return*/@getmapping(value = "/product/spuinfo/skuid/{skuid}")r getspuinfobyskuid(@pathvariable("skuid") long skuid);}

gulimall-product/src/main/ja/site/zhourui/gulimall/product/app/spuinfocontroller.ja

    /*** 提交订单,远程接口* 根据skuid查询spu的信息*/@getmapping(value = "/skuid/{skuid}")public r getspuinfobyskuid(@pathvariable("skuid") long skuid) {spuinfoentity spuinfoentity = spuinfoservice.getspuinfobyskuid(skuid);return r.ok().setdata(spuinfoentity);}

gulimall-product/src/main/ja/site/zhourui/gulimall/product/service/spuinfoservice.ja

    /*** 根据skuid查询spu的信息* @param skuid* @return*/spuinfoentity getspuinfobyskuid(long skuid);

gulimall-product/src/main/ja/site/zhourui/gulimall/product/service/impl/spuinfoserviceimpl.ja

    /*** 根据skuid查询spu的信息* @param skuid* @return*/@overridepublic spuinfoentity getspuinfobyskuid(long skuid) {//先查询sku表里的数据skuinfoentity skuinfoentity = skuinfoservice.getbyid(skuid);//获得spuidlong spuid = skuinfoentity.getspuid();//再通过spuid查询spuinfo信息表里的数据spuinfoentity spuinfoentity = basemapper.selectbyid(spuid);//查询品牌表的数据获取品牌名brandentity brandentity = brandservice.getbyid(spuinfoentity.getbrandid());spuinfoentity.setbrandname(brandentity.getname());return spuinfoentity;}

gulimall-product/src/main/ja/site/zhourui/gulimall/product/entity/spuinfoentity.ja

需要加上品牌名称

	/*** 品牌名*/@tablefield(exist = false)private string brandname;

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 创建订单*/private ordercreateto createorder() {ordercreateto createto = new ordercreateto();//1、生成订单号string ordersn = idworker.gettimeid();// 构建订单数据【封装价格】orderentity orderentity = builderorder(ordersn);//2、获取到所有的订单项【封装价格】list<orderitementity> orderitementities = builderorderitems(ordersn);//3、验价(计算价格、积分等信息)computeprice(orderentity, orderitementities);createto.setorder(orderentity);createto.setorderitems(orderitementities);return createto;}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 构建订单数据* @param ordersn* @return*/private orderentity builderorder(string ordersn) {//获取当前用户登录信息memberresponsevo memberresponsevo = loginuserinterceptor.loginuser.get();orderentity orderentity = new orderentity();orderentity.setmemberid(memberresponsevo.getid());orderentity.setordersn(ordersn);orderentity.setmemberusername(memberresponsevo.getusername());ordersubmitvo ordersubmitvo = confirmvothreadlocal.get();//远程获取收货地址和运费信息r fareaddressvo = wmsfeignservice.getfare(ordersubmitvo.getaddrid());farevo fareresp = fareaddressvo.getdata("data", new typereference<farevo>() {});//获取到运费信息bigdecimal fare = fareresp.getfare();orderentity.setfreightamount(fare);//获取到收货地址信息memberaddressvo address = fareresp.getaddress();//设置收货人信息orderentity.setreceivername(address.getname());orderentity.setreceiverphone(address.getphone());orderentity.setreceiverpostcode(address.getpostcode());orderentity.setreceiverprovince(address.getprovince());orderentity.setreceivercity(address.getcity());orderentity.setreceiverregion(address.getregion());orderentity.setreceiverdetailaddress(address.getdetailaddress());//设置订单相关的状态信息orderentity.setstatus(orderstatusenum.create_new.getcode());orderentity.setautoconfirmday(7);orderentity.setconfirmstatus(0);return orderentity;}

订单状态枚举

gulimall-order/src/main/ja/site/zhourui/gulimall/order/enume/orderstatusenum.ja

package site.zhourui.gulimall.order.enume;/*** @author zr* @date 2021/12/24 9:52*//*** 订单状态枚举*/public enum orderstatusenum {create_new(0,"待付款"),payed(1,"已付款"),sended(2,"已发货"),recieved(3,"已完成"),cancled(4,"已取消"),servicing(5,"售后中"),serviced(6,"售后完成");private integer code;private string msg;orderstatusenum(integer code, string msg) {this.code = code;this.msg = msg;}public integer getcode() {return code;}public string getmsg() {return msg;}
}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 构建所有订单项数据* @return*/public list<orderitementity> builderorderitems(string ordersn) {list<orderitementity> orderitementitylist = new arraylist<>();//最后确定每个购物项的价格list<orderitemvo> currentcartitems = cartfeignservice.getcurrentcartitems();if (currentcartitems != null && currentcartitems.size() > 0) {orderitementitylist = currentcartitems.stream().map((items) -> {//构建订单项数据orderitementity orderitementity = builderorderitem(items);orderitementity.setordersn(ordersn);return orderitementity;}).collect(collectors.tolist());}return orderitementitylist;}
5.2.2.7.3 验价

将页面提交的价格和后台计算的价格进行对比,若不同则提示用户商品价格发生变化

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 计算价格的方法* @param orderentity* @param orderitementities*/private void computeprice(orderentity orderentity, list<orderitementity> orderitementities) {//总价bigdecimal total = new bigdecimal("0.0");//优惠价bigdecimal coupon = new bigdecimal("0.0");bigdecimal intergration = new bigdecimal("0.0");bigdecimal promotion = new bigdecimal("0.0");//积分、成长值integer integrationtotal = 0;integer growthtotal = 0;//订单总额,叠加每一个订单项的总额信息for (orderitementity orderitem : orderitementities) {//优惠价格信息coupon = coupon.add(orderitem.getcouponamount());promotion = promotion.add(orderitem.getpromotionamount());intergration = intergration.add(orderitem.getintegrationamount());//总价total = total.add(orderitem.getrealamount());//积分信息和成长值信息integrationtotal  = orderitem.getgiftintegration();growthtotal  = orderitem.getgiftgrowth();}//1、订单价格相关的orderentity.settotalamount(total);//设置应付总额(总额 运费)orderentity.setpayamount(total.add(orderentity.getfreightamount()));orderentity.setcouponamount(coupon);orderentity.setpromotionamount(promotion);orderentity.setintegrationamount(intergration);//设置积分成长值信息orderentity.setintegration(integrationtotal);orderentity.setgrowth(growthtotal);//设置删除状态(0-未删除,1-已删除)orderentity.setdeletestatus(0);}
5.2.2.7.4 保存订单

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 保存订单所有数据*/private void seorder(ordercreateto ordercreateto) {//获取订单信息orderentity order = ordercreateto.getorder();order.setmodifytime(new date());order.setcreatetime(new date());//保存订单this.basemapper.insert(order);//获取订单项信息list<orderitementity> orderitems = ordercreateto.getorderitems();//批量保存订单项数据orderitemservice.sebatch(orderitems);}
5.2.2.7.5 库存锁定

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

                //todo 调用远程锁定库存的方法//出现的问题:扣减库存成功了,但是由于网络原因超时,出现异常,导致订单事务回滚,库存事务不回滚( 中国的解决方案:seata)//为了保证高并发,不推荐使用seata,因为是加锁,并行化,提升不了效率,可以发消息给库存服务r r = wmsfeignservice.orderlockstock(lockvo);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/feign/wmsfeignservice.ja

  • 找出所有库存大于商品数的仓库
  • 遍历所有满足条件的仓库,逐个尝试锁库存,若锁库存成功则退出遍历
    /*** 锁定库存*/@postmapping(value = "/ware/waresku/lock/order")r orderlockstock(@requestbody wareskulockvo vo);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/controller/wareskucontroller.ja

    /*** 下订单时锁库存* @param* @return*/@requestmapping("/lock/order")public r orderlockstock(@requestbody wareskulockvo lockvo) {try {boolean lock = wareskuservice.orderlockstock(lockvo);return r.ok();} catch (nostockexception e) {return r.error(bizcodeenume.no_stock_exception.getcode(), bizcodeenume.no_stock_exception.getmsg());}}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareskuservice.ja

    /*** 锁定库存*/boolean orderlockstock(wareskulockvo vo);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareskuserviceimpl.ja

    /*** 为某个订单锁定库存*/@transactional(rollbackfor = exception.class)@overridepublic boolean orderlockstock(wareskulockvo vo) {/*** 保存库存工作单详情信息* 追溯* 如果没有库存,就不会发送消息给mq* 【不会进入se(wareordertaskdetailentity)逻辑,也不会发送消息给mq,也不会锁定库存,也不会监听到解锁服务】*/wareordertaskentity wareordertaskentity = new wareordertaskentity();wareordertaskentity.setordersn(vo.getordersn());wareordertaskentity.setcreatetime(new date());wareordertaskservice.se(wareordertaskentity);//1、按照下单的收货地址,找到一个就近仓库,锁定库存//2、找到每个商品在哪个仓库都有库存list<orderitemvo> locks = vo.getlocks();list<skuwarehasstock> collect = locks.stream().map((item) -> {skuwarehasstock stock = new skuwarehasstock();long skuid = item.getskuid();stock.setskuid(skuid);stock.setnum(item.getcount());//查询这个商品在哪个仓库有库存 stock-锁定num > 0list<long> wareidlist = wareskudao.listwareidhasskustock(skuid);stock.setwareid(wareidlist);return stock;}).collect(collectors.tolist());//2、锁定库存for (skuwarehasstock hasstock : collect) {boolean skustocked = false;long skuid = hasstock.getskuid();list<long> wareids = hasstock.getwareid();if (collectionutils.isempty(wareids)) {//没有任何仓库有这个商品的库存throw new nostockexception(skuid);}//1、如果每一个商品都锁定成功,将当前商品锁定了几件的工作单记录发给mq//2、锁定失败。前面保存的工作单信息都回滚了。发送出去的消息,即使要解锁库存,由于在数据库查不到指定的id,所有就不用解锁for (long wareid : wareids) {//锁定成功就返回1,失败就返回0long count = wareskudao.lockskustock(skuid,wareid,hasstock.getnum());// count==1表示锁定成功if (count == 1) {skustocked = true;
//                    wareordertaskdetailentity taskdetailentity = wareordertaskdetailentity.builder()
//                            .skuid(skuid)
//                            .skuname("")
//                            .skunum(hasstock.getnum())
//                            .taskid(wareordertaskentity.getid())
//                            .wareid(wareid)
//                            .lockstatus(1)
//                            .build();
//                    wareordertaskdetailservice.se(taskdetailentity);
//
//                    //todo 告诉mq库存锁定成功
//                    stocklockedto lockedto = new stocklockedto();
//                    lockedto.setid(wareordertaskentity.getid());
//                    stockdetailto detailto = new stockdetailto();
//                    beanutils.copyproperties(taskdetailentity,detailto);// 这里直接存entity。但是应该存id更好,数据最好来自db
//                    lockedto.setdetailto(detailto);
//                    rabbittemplate.convertandsend("stock-event-exchange","stock.locked",lockedto);// 锁定成功返回break;} else {//当前仓库锁失败,重试下一个仓库}}if (skustocked == false) {//当前商品所有仓库都没有锁住throw new nostockexception(skuid);}}//3、肯定全部都是锁定成功的return true;}
5.2.2.8 订单提交的问题 (本地事务在分布式情况下出现的问题)

分布式情况下,可能出现一些服务事务不一致的情况

  • 远程服务假失败
  • 远程服务执行完成后,下面其他方法出现异常

库存扣减成功但是订单业务执行出错,订单业务可以回滚但远程调用的库存服务是办法回滚的

5.2.2.9 使用seata解决分布式事务

有多种模式:at、tcc、saga 和 xa
doc:http://seata.io/zh-cn/docs/overview/what-is-seata.html

1、tc不会控制各rm回滚,而是调用补偿方案,at模式是根据 回滚日志表【每个数据库都创建一个回滚日志表】
2、而tcc模式的回滚是根据补偿方法 来回滚

at模式:auto transiaction:自动事务模式,根据回滚日志表自动回滚
tcc模式:就是根据自己手写的事务补偿方法 来回滚

seata术语

tc (transaction coordinator) – 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。

tm (transaction manager) – 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。

rm (resource manager) – 资源管理器
管理分支事务处理的资源,与tc交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。

5.2.2.9.1 at模式实现步骤【创建订单 锁定库存】【不推荐使用】

不适用高并发场景,适用于商品服务,保存商品的那个接口 spuinfocontroller
/se
1、保存spu pms_spu_info
2、保存attr
3、保存描述图片 pms_spu_info_desc
4、保存图片集 pms_spu_images
5、保存当前spu对应的所有sku信息
6、优惠券信息【远程调用】分布式事务【并发不高,可以使用at模式,@globaltransactional】
7、保存积分信息【远程调用】分布式事务【并发不高,可以使用at模式,@globaltransactional】

seata官方文档:https://github.com/seata/seata-samples/blob/master/doc/quick-integration-with-spring-cloud.md

1.创建 undo_log 表

-- 注意此处0.3.0  增加唯一索引 ux_undo_log
create table `undo_log` (`id` bigint(20) not null auto_increment,`branch_id` bigint(20) not null,`xid` varchar(100) not null,`context` varchar(128) not null,`rollback_info` longblob not null,`log_status` int(11) not null,`log_created` datetime not null,`log_modified` datetime not null,`ext` varchar(100) default null,primary key (`id`),unique key `ux_undo_log` (`xid`,`branch_id`)
) engine=innodb auto_increment=1 default charset=utf8;

2.导入依赖

        <dependency><groupid>com.alibaba.cloudgroupid><artifactid>spring-cloud-starter-alibaba-seataartifactid><exclusions><exclusion><groupid>io.seatagroupid><artifactid>seata-allartifactid>exclusion>exclusions>dependency>
<dependency><groupid>io.seatagroupid><artifactid>seata-allartifactid><version>0.7.1version>dependency>

下载安装事务协调器:seate-server0.7.1release v0.7.1 · seata/seata · github

版本与seata-all版本对应

3.配置seata的注册中心 registry.conf

4.启动‪d:\environment\seata-server-0.7.1\bin\seata-server.bat

seata各种属性配置 file.conf

5.所有想要用到分布式事务的微服务使用seata datasourceproxy 代理自己的数据源

package site.zhourui.gulimall.order.config;/*** @author zr* @date 2021/12/28 10:57*/import com.zaxxer.hikari.hikaridatasource;
import io.seata.rm.datasource.datasourceproxy;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.autoconfigure.jdbc.datasourceproperties;
import org.springframework.context.annotation.bean;
import org.springframework.util.stringutils;import jax.sql.datasource;/*** seata分布式事务* 配置代理数据源*/
//@configuration
public class myseataconfig {@autowireddatasourceproperties datasourceproperties;/*** 自动配置类,如果容器中存在数据源就不自动配置数据源了*/@beanpublic datasource datasource(datasourceproperties datasourceproperties) {hikaridatasource datasource = datasourceproperties.initializedatasourcebuilder().type(hikaridatasource.class).build();if (stringutils.hastext(datasourceproperties.getname())) {datasource.setpoolname(datasourceproperties.getname());}return new datasourceproxy(datasource);}
}

6.每个使用分布式事务的微服务都需要导入 file.conf registry.conf

注意file.conf:

需要注意的是 service.vgroup_mapping这个配置,在 spring cloud 中默认是${spring.application.name}-fescar-service-group ,可以通过指定application.propertiesspring.cloud.alibaba.seata.tx-service-group这个属性覆盖,但是必须要和 file.conf中的一致,否则会提示 no ailable server to connect

7.加注解

  • 给分布式大事务的入口标注@globaltransactional gulimall-order服务
  •  每一个远程的小事务用@trabsactional                     gulimall-ware服务
    

重启服务测试

测试完成后关闭seataglobaltransactional,排除依赖 gulimall-order,gulimall-ware

5.2.2.10 最终一致性库存解锁逻辑:基于消息队列的分布式事务 分布式表【库存自动解锁】
5.2.2.10.1 为库存模块创建业务交换机,队列,绑定(整合rabbitmq)

导入依赖

gulimall-ware/pom.xml

        <dependency><groupid>org.springframework.bootgroupid><artifactid>spring-boot-starter-amqpartifactid>dependency>

配置rabbitmq地址端口(虚拟主机,确认发送,抵达确认,手动ack)

spring:rabbitmq:host: 192.168.157.128port: 5672virtual-host: /#开启发送端确认publisher-confirms: true# 开启发送端消息抵达queue确认publisher-returns: true# 只要消息抵达queue,就会异步发送优先回调returnfirmtemplate:mandatory: true#    使用手动ack确认模式,关闭自动确认【消息丢失】listener:simple:acknowledge-mode: manual

开启rabbitmq

主启动类加上

@enablerabbit

配置确认回调,失败回调

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/config/myrabbitconfig.ja

package site.zhourui.gulimall.ware.config;import org.springframework.amqp.rabbit.core.rabbittemplate;
import org.springframework.amqp.support.converter.jackson2jsonmessageconverter;
import org.springframework.amqp.support.converter.messageconverter;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;import jax.annotation.postconstruct;/*** @author zr* @date 2021/12/15 9:56*/
@configuration
public class myrabbitconfig {@autowiredrabbittemplate rabbittemplate;@beanpublic messageconverter messageconverter(){return new jackson2jsonmessageconverter();}/*** 定制rabbittemplate* 1、服务收到消息就会回调*      1、spring.rabbitmq.publisher-confirms: true*      2、设置确认回调* 2、消息正确抵达队列就会进行回调*      1、spring.rabbitmq.publisher-returns: true*         spring.rabbitmq.template.mandatory: true*      2、设置确认回调returncallback** 3、消费端确认(保证每个消息都被正确消费,此时才可以broker删除这个消息)**/@postconstruct  //myrabbitconfig对象创建完成以后,执行这个方法public void initrabbittemplate() {/*** 1、只要消息抵达broker就ack=true* correlationdata:当前消息的唯一关联数据(这个是消息的唯一id)* ack:消息是否成功收到* cause:失败的原因*///设置确认回调rabbittemplate.setconfirmcallback((correlationdata,ack,cause) -> {system.out.println("confirm...correlationdata["correlationdata"]==>ack:["ack"]==>cause:["cause"]");});/*** 只要消息没有投递给指定的队列,就触发这个失败回调* message:投递失败的消息详细信息* replycode:回复的状态码* replytext:回复的文本内容* exchange:当时这个消息发给哪个交换机* routingkey:当时这个消息用哪个路邮键*/rabbittemplate.setreturncallback((message,replycode,replytext,exchange,routingkey) -> {system.out.println("fail message["message"]==>replycode["replycode"]" "==>replytext["replytext"]==>exchange["exchange"]==>routingkey["routingkey"]");});}
}

创建库存解锁延时队列及交换机,绑定

package site.zhourui.gulimall.ware.config;/*** @author zr* @date 2021/12/28 16:47*/import com.rabbitmq.client.channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;import ja.io.ioexception;
import ja.util.hashmap;/*** 创建队列,交换机,延迟队列,绑定关系 的configuration* 不会重复创建覆盖* 1、第一次使用队列【监听】的时候才会创建* 2、broker没有队列、交换机才会创建*/
@configuration
public class myrabbitmqconfig {@rabbitlistener(queues = "stock.release.stock.queue")public void listen( channel channel, message message) throws ioexception {system.out.println("收到库存解锁消息,准备解锁库存:------>");channel.basicnack(message.getmessageproperties().getdeliverytag(),false,false);}/*** 库存服务默认的交换机* @return*/@beanpublic exchange stockeventexchange() {//string name, boolean durable, boolean autodelete, map argumentstopicexchange topicexchange = new topicexchange("stock-event-exchange", true, false);return topicexchange;}/*** 普通队列* @return*/@beanpublic queue stockreleasestockqueue() {//string name, boolean durable, boolean exclusive, boolean autodelete, map argumentsqueue queue = new queue("stock.release.stock.queue", true, false, false);return queue;}/*** 延迟队列* @return*/@beanpublic queue stockdelay() {hashmap<string, object> arguments = new hashmap<>();arguments.put("x-dead-letter-exchange", "stock-event-exchange");arguments.put("x-dead-letter-routing-key", "stock.release");// 消息过期时间 2分钟arguments.put("x-message-ttl", 120000);queue queue = new queue("stock.delay.queue", true, false, false,arguments);return queue;}/*** 交换机与普通队列绑定* @return*/@beanpublic binding stocklocked() {//string destination, destinationtype destinationtype, string exchange, string routingkey,// 			map argumentsbinding binding = new binding("stock.release.stock.queue",binding.destinationtype.queue,"stock-event-exchange","stock.release.#",null);return binding;}/*** 交换机与延迟队列绑定* @return*/@beanpublic binding stocklockedbinding() {return new binding("stock.delay.queue",binding.destinationtype.queue,"stock-event-exchange","stock.locked",null);}}

启动测试

stock.locked路邮键发送队列,并监听死信队列,两分钟后监听到消息说明成功了

5.2.2.10.2 库存解锁

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareskuservice.ja

    /*** 解锁库存* @param to*/void unlockstock(stocklockedto to);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareskuserviceimpl.ja

此处需要调用订单服务远程服务根据订单号查询订单信息

    /*** 解锁库存*/@overridepublic void unlockstock(stocklockedto to) {//库存工作单的idstockdetailto detail = to.getdetailto();long detailid = detail.getid();/*** 解锁* 1、查询数据库关于这个订单锁定库存信息*   有:证明库存锁定成功了*      解锁:订单状况*          1、没有这个订单,必须解锁库存*          2、有这个订单,不一定解锁库存*              订单状态:已取消:解锁库存*                      已支付:不能解锁库存*/wareordertaskdetailentity taskdetailinfo = wareordertaskdetailservice.getbyid(detailid);if (taskdetailinfo != null) {//查出wms_ware_order_task工作单的信息long id = to.getid();wareordertaskentity ordertaskinfo = wareordertaskservice.getbyid(id);//获取订单号查询订单状态string ordersn = ordertaskinfo.getordersn();//远程查询订单信息r orderdata = orderfeignservice.getorderstatus(ordersn);if (orderdata.getcode() == 0) {//订单数据返回成功ordervo orderinfo = orderdata.getdata("data", new typereference<ordervo>() {});//判断订单状态是否已取消或者支付或者订单不存在// 1、订单不存在:解锁// 2、订单存在,且订单状态是取消状态:解锁if (orderinfo == null || orderinfo.getstatus() == 4) {// 工作单状态必须是 已锁定 才可以解锁【因为解锁方法没有加事务】if (taskdetailinfo.getlockstatus() == 1) {unlockstock(detail.getskuid(),detail.getwareid(),detail.getskunum(),detailid);}}} else {//消息拒绝以后重新放在队列里面,让别人继续消费解锁//远程调用服务失败throw new runtimeexception("远程调用服务失败");}} else {//无需解锁【回滚状态】}}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/feign/orderfeignservice.ja

package site.zhourui.gulimall.ware.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import site.zhourui.common.utils.r;/*** @author zr* @date 2021/12/29 15:29*/
@feignclient("gulimall-order")
public interface orderfeignservice {@getmapping(value = "/order/order/status/{ordersn}")r getorderstatus(@pathvariable("ordersn") string ordersn);}

远程订单服务

gulimall-order/src/main/ja/site/zhourui/gulimall/order/controller/ordercontroller.ja

    /*** 根据订单编号查询订单状态* @param ordersn* @return*/@getmapping(value = "/status/{ordersn}")public r getorderstatus(@pathvariable("ordersn") string ordersn) {orderentity orderentity = orderservice.getorderbyordersn(ordersn);return r.ok().setdata(orderentity);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    /*** 按照订单号获取订单信息* @param ordersn* @return*/orderentity getorderbyordersn(string ordersn);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 按照订单号获取订单信息* @param ordersn* @return*/@overridepublic orderentity getorderbyordersn(string ordersn) {orderentity orderentity = this.basemapper.selectone(new querywrapper<orderentity>().eq("order_sn", ordersn));return orderentity;}
5.2.2.10.3 监听库存解锁

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/entity/wareordertaskdetailentity.ja

增加两个字段 仓库id,锁定状态使用@builder

package site.zhourui.gulimall.ware.entity;import com.baomidou.mybatisplus.annotation.tableid;
import com.baomidou.mybatisplus.annotation.tablename;import ja.io.serializable;
import ja.util.date;import lombok.allargsconstructor;
import lombok.builder;
import lombok.data;
import lombok.noargsconstructor;/*** 库存工作单** @author zr* @email 2437264464@qq.com* @date 2021-09-28 15:47:50*/
@noargsconstructor
@allargsconstructor
@builder
@data
@tablename("wms_ware_order_task_detail")
public class wareordertaskdetailentity implements serializable {private static final long serialversionuid = 1l;/*** 仓库id*/private long wareid;/*** 锁定状态* 1-已锁定 2-已解锁 3-已扣减*/private integer lockstatus;/*** id*/@tableidprivate long id;/*** sku_id*/private long skuid;/*** sku_name*/private string skuname;/*** 购买个数*/private integer skunum;/*** 工作单id*/private long taskid;}

锁定库存时向库存延时队列发送一条库存工作单记录

库存工作单

gulimall-common/src/main/ja/site/zhourui/common/to/mq/stocklockedto.ja

package site.zhourui.common.to.mq;/***  锁定库存成功,往延时队列存入 工作单to 对象*  wms_ware_order_task* @author zr* @date 2021/12/29 15:07*/import lombok.data;/***/
@data
public class stocklockedto {/** 库存工作单的id **/private long id;/** 库存单详情 wms_ware_order_task_detail**/private stockdetailto detailto;
}

库存详情单

gulimall-common/src/main/ja/site/zhourui/common/to/mq/stockdetailto.ja

package site.zhourui.common.to.mq;/*** 库存单详情* wms_ware_order_task_detail* @author zr* @date 2021/12/29 15:07*/import lombok.data;@data
public class stockdetailto {private long id;/*** sku_id*/private long skuid;/*** sku_name*/private string skuname;/*** 购买个数*/private integer skunum;/*** 工作单id*/private long taskid;/*** 仓库id*/private long wareid;/*** 锁定状态* 1-锁定 2-解锁 3-扣减*/private integer lockstatus;}

监听库存死信队列,解锁库存

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/listener/stockreleaselistener.ja

package site.zhourui.gulimall.ware.listener;import com.rabbitmq.client.channel;
import lombok.extern.slf4j.slf4j;
import org.springframework.amqp.core.message;
import org.springframework.amqp.rabbit.annotation.rabbithandler;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import site.zhourui.common.to.mq.stocklockedto;
import site.zhourui.gulimall.ware.service.wareskuservice;import ja.io.ioexception;/*** 监听死信队列,解锁库存* @author zr* @date 2021/12/29 15:22*/
@slf4j
@rabbitlistener(queues = "stock.release.stock.queue")
@service
public class stockreleaselistener {@autowiredprivate wareskuservice wareskuservice;/*** 这个是监听死信消息* 1、库存自动解锁*  下订单成功,库存锁定成功,接下来的业务调用失败,导致订单回滚。之前锁定的库存就要自动解锁**  2、订单失败*      库存锁定失败**   只要解锁库存的消息失败,一定要告诉服务解锁失败*/@rabbithandlerpublic void handlestocklockedrelease(stocklockedto to, message message, channel channel) throws ioexception {system.out.println("******收到解锁库存的延时信息******,准备解锁"  to.getdetailto().getid());try {//当前消息是否被第二次及以后(重新)派发过来了// boolean redelivered = message.getmessageproperties().getredelivered();//解锁库存wareskuservice.unlockstock(to);// 手动删除消息channel.basicack(message.getmessageproperties().getdeliverytag(),false);} catch (exception e) {// 解锁失败 将消息重新放回队列,让别人消费channel.basicreject(message.getmessageproperties().getdeliverytag(),true);}}/*** 客户取消订单,监听到消息*/@rabbithandlerpublic void handleordercloserelease(orderto orderto, message message, channel channel) throws ioexception {system.out.println("******收到订单关闭,准备解锁库存的信息******订单号:"  orderto.getordersn());try {wareskuservice.unlockstock(orderto);// 手动删除消息channel.basicack(message.getmessageproperties().getdeliverytag(),false);} catch (exception e) {// 解锁失败 将消息重新放回队列,让别人消费channel.basicreject(message.getmessageproperties().getdeliverytag(),true);}}
}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareskuservice.ja

    /*** 解锁库存* @param to*/void unlockstock(stocklockedto to);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareskuserviceimpl.ja

此处需要调用订单服务远程查询订单信息

    /*** 解锁库存*/@overridepublic void unlockstock(stocklockedto to) {//库存工作单的idstockdetailto detail = to.getdetailto();long detailid = detail.getid();/*** 解锁* 1、查询数据库关于这个订单锁定库存信息*   有:证明库存锁定成功了*      解锁:订单状况*          1、没有这个订单,必须解锁库存*          2、有这个订单,不一定解锁库存*              订单状态:已取消:解锁库存*                      已支付:不能解锁库存*/wareordertaskdetailentity taskdetailinfo = wareordertaskdetailservice.getbyid(detailid);if (taskdetailinfo != null) {//查出wms_ware_order_task工作单的信息long id = to.getid();wareordertaskentity ordertaskinfo = wareordertaskservice.getbyid(id);//获取订单号查询订单状态string ordersn = ordertaskinfo.getordersn();//远程查询订单信息r orderdata = orderfeignservice.getorderstatus(ordersn);if (orderdata.getcode() == 0) {//订单数据返回成功ordervo orderinfo = orderdata.getdata("data", new typereference<ordervo>() {});//判断订单状态是否已取消或者支付或者订单不存在// 1、订单不存在:解锁// 2、订单存在,且订单状态是取消状态:解锁if (orderinfo == null || orderinfo.getstatus() == 4) {// 工作单状态必须是 已锁定 才可以解锁【因为解锁方法没有加事务】if (taskdetailinfo.getlockstatus() == 1) {unlockstock(detail.getskuid(),detail.getwareid(),detail.getskunum(),detailid);}}} else {//消息拒绝以后重新放在队列里面,让别人继续消费解锁//远程调用服务失败throw new runtimeexception("远程调用服务失败");}} else {//无需解锁【回滚状态】}}/*** 解锁库存的方法【设计db,没加事务】*/public void unlockstock(long skuid,long wareid,integer num,long taskdetailid) {// 1、库存解锁wareskudao.unlockstock(skuid,wareid,num);// 2、更新工作单的状态 为已解锁 2wareordertaskdetailentity taskdetailentity = new wareordertaskdetailentity();taskdetailentity.setid(taskdetailid);taskdetailentity.setlockstatus(2);wareordertaskdetailservice.updatebyid(taskdetailentity);}

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/feign/orderfeignservice.ja

远程查询订单信息

package site.zhourui.gulimall.ware.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import site.zhourui.common.utils.r;/*** @author zr* @date 2021/12/29 15:29*/
@feignclient("gulimall-order")
public interface orderfeignservice {@getmapping(value = "/order/order/status/{ordersn}")r getorderstatus(@pathvariable("ordersn") string ordersn);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/controller/ordercontroller.ja

    /*** 根据订单编号查询订单状态* @param ordersn* @return*/@getmapping(value = "/status/{ordersn}")public r getorderstatus(@pathvariable("ordersn") string ordersn) {orderentity orderentity = orderservice.getorderbyordersn(ordersn);return r.ok().setdata(orderentity);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    /*** 按照订单号获取订单信息* @param ordersn* @return*/orderentity getorderbyordersn(string ordersn);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 按照订单号获取订单信息* @param ordersn* @return*/@overridepublic orderentity getorderbyordersn(string ordersn) {orderentity orderentity = this.basemapper.selectone(new querywrapper<orderentity>().eq("order_sn", ordersn));return orderentity;}
5.2.2.10.4 远程服务order订单服务登录拦截跳转login.html

gulimall-order/src/main/ja/site/zhourui/gulimall/order/interceptor/loginuserinterceptor.ja

将该请求放行

        string uri = request.getrequesturi();antpathmatcher antpathmatcher = new antpathmatcher();boolean match = antpathmatcher.match("/order/order/status/**", uri);return match;
5.2.2.11 最终一致性库存解锁逻辑:基于消息队列的分布式事务 分布式表【订单自动关单】
5.2.2.11.1 为订单模块创建业务交换机,队列绑定

谷粒商城--消息队列--高级篇笔记十6.6 延时队列定时关单模拟已经创建了交换机队列,绑定

https://blog.csdn.net/qq_31745863/article/details/122212434

5.2.2.11.2 订单关闭

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    /*** 关闭订单* @param orderentity*/void closeorder(orderentity orderentity);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 关闭订单*/@overridepublic void closeorder(orderentity orderentity) {//关闭订单之前先查询一下数据库,判断此订单状态是否已支付orderentity orderinfo = this.getone(new querywrapper<orderentity>().eq("order_sn",orderentity.getordersn()));if (orderinfo.getstatus().equals(orderstatusenum.create_new.getcode())) {//代付款状态进行关单orderentity orderupdate = new orderentity();orderupdate.setid(orderinfo.getid());orderupdate.setstatus(orderstatusenum.cancled.getcode());this.updatebyid(orderupdate);// 发送消息给mqorderto orderto = new orderto();beanutils.copyproperties(orderinfo, orderto);try {//todo 确保每个消息发送成功,给每个消息做好日志记录,(给数据库保存每一个详细信息)保存每个消息的详细信息//todo 定期扫描数据库,重新发送失败的消息rabbittemplate.convertandsend("order-event-exchange", "order.release.other", orderto);} catch (exception e) {}}}
5.2.2.11.3 监听订单自动关单

gulimall-order/src/main/ja/site/zhourui/gulimall/order/listener/ordercloselistener.ja

package site.zhourui.gulimall.order.interceptor;import com.rabbitmq.client.channel;
import org.springframework.amqp.core.message;
import org.springframework.amqp.rabbit.annotation.rabbithandler;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import site.zhourui.gulimall.order.entity.orderentity;
import site.zhourui.gulimall.order.service.orderservice;import ja.io.ioexception;/*** @author zr* @date 2021/12/29 17:22*/
/*** 定时关闭订单**/
@rabbitlistener(queues = "order.release.order.queue")
@service
public class ordercloselistener {@autowiredprivate orderservice orderservice;@rabbithandlerpublic void listener(orderentity orderentity, channel channel, message message) throws ioexception {system.out.println("收到过期的订单信息,准备关闭订单"  orderentity.getordersn());try {orderservice.closeorder(orderentity);// 手动调用支付宝收单【这里省略了,可以参照demo中的代码】channel.basicack(message.getmessageproperties().getdeliverytag(),false);} catch (exception e) {channel.basicreject(message.getmessageproperties().getdeliverytag(),true);}}}
5.2.2.12 测试自动关单,自动解锁库存

清空之前的订单与库存锁定,库存工作单(也可以不清,但需要记住提交此时的状态,这样好看一点)

清空mq消息

下单

mq的订单延时队列(1分钟),库存延时队列(2分钟)

一分钟之内数据库状态

大于一分钟小于两分钟,自动关单

其他数据库表与一分钟一致

大于两分钟,库存自动解锁

5.2.2.13 订单卡顿导致的库存无法解锁

防止订单服务卡顿,导致订单状态消息一直改不了,库存优先到期,查订单状态新建,什么都不处理
导致卡顿的订单,永远都不能解锁库存

中国的解决方案

再往订单死信队列发送消息时,同时也往库存死信队列发送相同消息,通知库存解锁

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/listener/stockreleaselistener.ja

    /*** 客户取消订单,监听到消息*/@rabbithandlerpublic void handleordercloserelease(orderto orderto, message message, channel channel) throws ioexception {system.out.println("******收到订单关闭,准备解锁库存的信息******订单号:"  orderto.getordersn());try {wareskuservice.unlockstock(orderto);// 手动删除消息channel.basicack(message.getmessageproperties().getdeliverytag(),false);} catch (exception e) {// 解锁失败 将消息重新放回队列,让别人消费channel.basicreject(message.getmessageproperties().getdeliverytag(),true);}}

重载解锁库存

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareskuservice.ja

    /*** 解锁订单*/void unlockstock(orderto orderto);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareskuserviceimpl.ja

    /*** 防止订单服务卡顿,导致订单状态消息一直改不了,库存优先到期,查订单状态新建,什么都不处理* 导致卡顿的订单,永远都不能解锁库存* @param orderto*/@transactional(rollbackfor = exception.class)@overridepublic void unlockstock(orderto orderto) {string ordersn = orderto.getordersn();//查一下最新的库存解锁状态,防止重复解锁库存wareordertaskentity ordertaskentity = wareordertaskservice.getordertaskbyordersn(ordersn);//按照工作单的id找到所有 没有解锁的库存,进行解锁long id = ordertaskentity.getid();list<wareordertaskdetailentity> list = wareordertaskdetailservice.list(new querywrapper<wareordertaskdetailentity>().eq("task_id", id).eq("lock_status", 1));for (wareordertaskdetailentity taskdetailentity : list) {unlockstock(taskdetailentity.getskuid(),taskdetailentity.getwareid(),taskdetailentity.getskunum(),taskdetailentity.getid());}}

在库存解锁前查一下最新的库存解锁状态,防止重复解锁库存

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/wareordertaskservice.ja

    wareordertaskentity getordertaskbyordersn(string ordersn);

gulimall-ware/src/main/ja/site/zhourui/gulimall/ware/service/impl/wareordertaskserviceimpl.ja

    @overridepublic wareordertaskentity getordertaskbyordersn(string ordersn) {wareordertaskentity ordertaskentity = this.basemapper.selectone(new querywrapper<wareordertaskentity>().eq("order_sn", ordersn));return ordertaskentity;}
5.2.2.14 如何保证消息可靠性
5.2.2.14.1 消息丢失
  • 消息发送出去,由于网络问题没有抵达服务器
    • 做好容错方法(try-catch),发送消息可能会网络失败,失败后要有重试机制,可记录到数据库,采用定期扫描重发的方式
    • 做好日志记录,每个消息状态是否都被服务器收到都应该记录
    • 做好定期重发,如果消息没有发送成功,定期去数据库扫描未成功的消息进行重发
  • 消息抵达broker,broker要将消息写入磁盘(持久化)才算成功。此时broker尚未持久化完成,宕机。
    • publisher也必须加入确认回调机制,确认成功的消息,修改数据库消息状态。
  • 自动ack的状态下。消费者收到消息,但没来得及消息然后宕机
    • 一定开启手动ack,消费成功才移除,失败或者没来得及处理就noack并重新入队
5.2.2.14.2 消息重复
  • 消息消费成功,事务已经提交,ack时,机器宕机。导致没有ack成功,broker的消息
    重新由unack变为ready,并发送给其他消费者

  • 消息消费失败,由于重试机制,自动又将消息发送出去

  • 成功消费,ack时宕机,消息由unack变为ready,broker又重新发送

    • 消费者的业务消费接口应该设计为幂等性的。比如扣库存有工作单的状态标志

    • 使用防重表(redis/mysql),发送消息每一个都有业务的唯一标识,处理过就不用处理

      create table `mq_message`(`message_id` char(32) not null ,`content` text, #json`to_exchange` char(255) default null ,`routing_key` char(255) default null ,`class_type` char(255) default null ,`message_status` int(1) default '0' comment '0-新建 1-已发送 2-错误抵达 3-已抵达',`create_time` datetime default null ,`update_time` datetime default null
      )
      
    • rabbitmq的每一个消息都有redelivered字段,可以获取是否是被重新投递过来的,而不是第一次投递过来的

5.2.2.14.3 消息积压
  • 消费者宕机积压
  • 消费者消费能力不足积压
  • 发送者发送流量太大
    • 上线更多的消费者,进行正常消费
    • 上线专门的队列消费服务,将消息先批量取出来,记录数据库,离线慢慢处理

5.3 订单支付页

5.3.1 加密

5.3.1.1 对称加密

加密与解密用的秘钥都是一样的

5.3.1.2 非对称加密

加密与解密用到的秘钥不一致

5.3.2 支付宝加密原理

  • 支付宝加密采用rsa非对称加密,分别在商户端和支付宝端有两对公钥和私钥
  • 在发送订单数据时,直接使用明文,但会使用商户私钥加一个对应的签名,支付宝端会使用商户公钥对签名进行验签,只有数据明文和签名对应的时候才能说明传输正确
  • 支付成功后,支付宝发送支付成功数据之外,还会使用支付宝私钥加一个对应的签名,商户端收到支付成功数据之后也会使用支付宝公钥延签,成功后才能确认
5.3.2.1 什么是公钥、 私钥、 加密、 签名和验签?
5.3.2.1.1 公钥私钥

公钥和私钥是一个相对概念
它们的公私性是相对于生成者来说的。
一对密钥生成后, 保存在生成者手里的就是私钥,生成者发布出去大家用的就是公钥

5.3.2.1.2 加密和数字签名
  • 加密是指:
    • 我们使用一对公私钥中的一个密钥来对数据进行加密, 而使用另一个密钥来进行解密的技术。
    • 公钥和私钥都可以用来加密, 也都可以用来解密。
    • 但这个加解密必须是一对密钥之间的互相加解密, 否则不能成功。
    • 加密的目的是:
      • 为了确保数据传输过程中的不可读性, 就是不想让别人看到。
  • 签名:
    • 给我们将要发送的数据, 做上一个唯一签名(类似于指纹)
    • 用来互相验证接收方和发送方的身份;
    • 在验证身份的基础上再验证一下传递的数据是否被篡改过。 因此使用数字签名可以用来达到数据的明文传输。
  • 验签
    支付宝为了验证请求的数据是否商户本人发的,
    商户为了验证响应的数据是否支付宝发的

5.3.3 支付宝官方demo测试

官方demo下载地址:https://opendocs.alipay.com/open/54/106682

5.3.3.1 使用支付宝沙箱环境进行测试:https://open.alipay.com/platform/appdaily.htm?tab=account
5.3.3.2 自定义秘钥,点击rsa2秘钥后面的设置并启用
5.3.3.3 利用秘钥工具生成自己的公钥,私钥,拿到支付宝公钥
5.3.3.4 配置demo中的alipayconfig
5.3.3.5 启动demo

配置web目录

添加archive

配置tomcat

添加依赖

配置字符集

启动tomcat,测试

能够字符成功的话就说明测试成功了

测试账号密码https://open.alipay.com/platform/appdaily.htm?tab=account

5.3.4 支付宝支付流程

5.3.5 内网穿透

5.3.5.0 为什么使用内网穿透?

支付宝需要回调我们的接口进行异步通知

5.3.5.1 简介

内网穿透功能可以允许我们使用外网的网址来访问主机;
正常的外网需要访问我们项目的流程是:

  1. 买服务器并且有公网固定 ip
  2. 买域名映射到服务器的 ip
  3. 域名需要进行备案和审核
5.3.5.2 使用场景

1、 开发测试(微信、 支付宝)
2、 智慧互联
3、 远程控制
4、 私有云

5.3.5.3 内网穿透的几个常用软件

1、 natapp: https://natapp.cn/ 优惠码: 022b93fd(9 折) [仅限第一次使用]
2、 续断: www.zhexi.tech 优惠码: qmea(95 折) [仅限第一次使用]
3、 花生壳: https://www.oray.com/

5.3.5.4 natapp内网穿透

官方文档natapp1分钟快速新手图文教程 – natapp-内网穿透 基于ngrok的国内高速内网映射工具

  1. 注册,实名认证

  2. 购买免费隧道后拿到authtoken

  3. window启动命令

    natapp -authtoken=你的authtoken
    

5.3.6 整合支付

5.3.6.1 导入依赖

gulimall-order/pom.xml

        <dependency><groupid>com.alipay.sdkgroupid><artifactid>alipay-sdk-jaartifactid><version>4.10.111.allversion>dependency>
5.3.6.2 抽取阿里云支付模板

gulimall-order/src/main/ja/site/zhourui/gulimall/order/config/alipaytemplate.ja

package site.zhourui.gulimall.order.config;import com.alipay.api.alipayapiexception;
import com.alipay.api.alipayclient;
import com.alipay.api.defaultalipayclient;
import com.alipay.api.request.alipaytradepagepayrequest;
import lombok.data;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.stereotype.component;/*** @author zr* @date 2021/12/31 10:26*/
@configurationproperties(prefix = "alipay")
@component
@data
public class alipaytemplate {// 应用id,您的appid,收款账号既是您的appid对应支付宝账号public string app_id;// 商户私钥,您的pkcs8格式rsa2私钥public string merchant_private_key;// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keymanage.htm 对应appid下的支付宝公钥。public string alipay_public_key;// 服务器[异步通知]页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问// 支付宝会悄悄的给我们发送一个请求,告诉我们支付成功的信息public string notify_url;// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问//同步通知,支付成功,一般跳转到成功页public string return_url;// 签名方式private  string sign_type;// 字符编码格式private  string charset;//订单超时时间private string timeout = "1m";// 支付宝网关; https://openapi.alipaydev.com/gateway.dopublic string gatewayurl;public  string pay(payvo vo) throws alipayapiexception {//alipayclient alipayclient = new defaultalipayclient(alipaytemplate.gatewayurl, alipaytemplate.app_id, alipaytemplate.merchant_private_key, "json", alipaytemplate.charset, alipaytemplate.alipay_public_key, alipaytemplate.sign_type);//1、根据支付宝的配置生成一个支付客户端alipayclient alipayclient = new defaultalipayclient(gatewayurl,app_id, merchant_private_key, "json",charset, alipay_public_key, sign_type);//2、创建一个支付请求 //设置请求参数alipaytradepagepayrequest alipayrequest = new alipaytradepagepayrequest();alipayrequest.setreturnurl(return_url);alipayrequest.setnotifyurl(notify_url);//商户订单号,商户网站订单系统中唯一订单号,必填string out_trade_no = vo.getout_trade_no();//付款金额,必填string total_amount = vo.gettotal_amount();//订单名称,必填string subject = vo.getsubject();//商品描述,可空string body = vo.getbody();alipayrequest.setbizcontent("{\"out_trade_no\":\"" out_trade_no "\"," "\"total_amount\":\"" total_amount "\"," "\"subject\":\"" subject "\"," "\"body\":\"" body "\"," "\"timeout_express\":\""timeout"\"," "\"product_code\":\"fast_instant_trade_pay\"}");string result = alipayclient.pageexecute(alipayrequest).getbody();//会收到支付宝的响应,响应的是一个页面,只要浏览器显示这个页面,就会自动来到支付宝的收银台页面system.out.println("支付宝响应:登录页面的代码\n"result);return result;}@datapublic static class payvo {private string out_trade_no; // 商户订单号 必填private string subject; // 订单名称 必填private string total_amount;  // 付款金额 必填private string body; // 商品描述 可空}
}
5.3.6.3 配置模板所需相关配置
alipay:alipay_public_key: xxxapp_id: 2021000117672854charset: utf-8gatewayurl: https://openapi.alipaydev.com/gateway.domerchant_private_key: xxxx#此处先使用demo的回调接口页面notify_url: http://4wa8cx.natappfree.cc/alipay_trade_wap_pay_ja_utf_8_web_exploded/notify_url.jspreturn_url: http://4wa8cx.natappfree.cc/alipay_trade_wap_pay_ja_utf_8_web_exploded/return_url.jspsign_type: rsa2
5.3.6.4 支付宝支付接口

gulimall-order/src/main/ja/site/zhourui/gulimall/order/web/paywebcontroller.ja

package site.zhourui.gulimall.order.web;import com.alipay.api.alipayapiexception;
import lombok.extern.slf4j.slf4j;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;
import site.zhourui.gulimall.order.config.alipaytemplate;
import site.zhourui.gulimall.order.service.orderservice;/*** @author zr* @date 2021/12/31 10:57*/
@slf4j
@controller
public class paywebcontroller {@autowiredprivate alipaytemplate alipaytemplate;@autowiredprivate orderservice orderservice;/*** 用户下单:支付宝支付* 1、让支付页让浏览器展示* 2、支付成功以后,跳转到用户的订单列表页* @param ordersn* @return* @throws alipayapiexception*/@responsebody@getmapping(value = "/alipayorder",produces = "text/html")public string alipayorder(@requestparam("ordersn") string ordersn) throws alipayapiexception {alipaytemplate.payvo payvo = orderservice.getorderpay(ordersn);// 支付宝返回一个页面【支付宝账户登录的html页面】string pay = alipaytemplate.pay(payvo);system.out.println(pay);return pay;}
}

获取当前订单的支付信息

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    /*** 获取当前订单的支付信息* @param ordersn* @return*/alipaytemplate.payvo getorderpay(string ordersn);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 获取当前订单的支付信息*/@overridepublic alipaytemplate.payvo getorderpay(string ordersn) {alipaytemplate.payvo payvo = new alipaytemplate.payvo();orderentity orderinfo = this.getorderbyordersn(ordersn);//保留两位小数点,向上取值bigdecimal payamount = orderinfo.getpayamount().setscale(2, bigdecimal.round_up);payvo.settotal_amount(payamount.tostring());payvo.setout_trade_no(orderinfo.getordersn());//查询订单项的数据list<orderitementity> orderiteminfo = orderitemservice.list(new querywrapper<orderitementity>().eq("order_sn", ordersn));orderitementity orderitementity = orderiteminfo.get(0);payvo.setbody(orderitementity.getskuattrsvals());payvo.setsubject(orderitementity.getskuname());return payvo;}
5.3.6.5 前端支付页面

https://gitee.com/zhourui815/gulimall/blob/master/gulimall-order/src/main/resources/templates/pay.html

5.3.6.6 支付测试

支付成功后跳转页面,正常情况下应该跳转到订单列表页面

5.3.6.7 订单列表页渲染
5.3.6.7.1 静态资源上传
5.3.6.7.2 配置host
5.3.6.7.3 配置网关

gulimall-gateway/src/main/resources/application.yml

- id: gulimall_member_routeuri: lb://gulimall-memberpredicates:- host=member.gulimall.com
5.3.6.7.3 前端页面

前端页面https://gitee.com/zhourui815/gulimall/blob/master/gulimall-member/src/main/resources/templates/orderlist.html

5.3.6.7.4 回调页面接口

gulimall-member/src/main/ja/site/zhourui/gulimall/member/web/memberwebcontroller.ja

package site.zhourui.gulimall.member.web;import com.alibaba.fastjson.json;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import site.zhourui.common.utils.r;
import site.zhourui.gulimall.member.feign.orderfeignservice;import ja.util.hashmap;
import ja.util.map;/*** @author zr* @date 2021/12/31 15:23*/
@controller
public class memberwebcontroller {@autowiredprivate orderfeignservice orderfeignservice;@getmapping(value = "/memberorder.html")public string memberorderpage(@requestparam(value = "pagenum",required = false,defaultvalue = "0") integer pagenum,model model) {//获取到支付宝给我们转来的所有请求数据//request,验证签名//查出当前登录用户的所有订单列表数据map<string,object> page = new hashmap<>();page.put("page",pagenum.tostring());//远程查询订单服务订单数据r orderinfo = orderfeignservice.listwithitem(page);system.out.println(json.tojsonstring(orderinfo));model.addattribute("orders",orderinfo);return "orderlist";}}

需要调用订单远程服务

gulimall-member/src/main/ja/site/zhourui/gulimall/member/feign/orderfeignservice.ja

package site.zhourui.gulimall.member.feign;import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestbody;
import site.zhourui.common.utils.r;import ja.util.map;/*** @author zr* @date 2021/12/31 15:24*/
@feignclient("gulimall-order")
public interface orderfeignservice {/*** 分页查询当前登录用户的所有订单信息*/@postmapping("/order/order/listwithitem")r listwithitem(@requestbody map<string, object> params);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/controller/ordercontroller.ja

    /*** member远程调用:分页查询当前登录用户的所有订单信息*/@postmapping("/listwithitem")//@requirespermissions("order:order:list")public r listwithitem(@requestbody map<string, object> params){pageutils page = orderservice.querypagewithitem(params);return r.ok().put("page", page);}

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/orderservice.ja

    /*** 查询当前用户所有订单数据* @param params* @return*/pageutils querypagewithitem(map<string, object> params);

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

此处需要设置订单行信息

    /*** 查询当前用户所有订单数据*/@overridepublic pageutils querypagewithitem(map<string, object> params) {memberresponsevo memberresponsevo = loginuserinterceptor.loginuser.get();ipage<orderentity> page = this.page(new query<orderentity>().getpage(params),new querywrapper<orderentity>().eq("member_id",memberresponsevo.getid()).orderbydesc("create_time"));//遍历所有订单集合list<orderentity> orderentitylist = page.getrecords().stream().map(order -> {//根据订单号查询订单项里的数据list<orderitementity> orderitementities = orderitemservice.list(new querywrapper<orderitementity>().eq("order_sn", order.getordersn()));order.setorderitementitylist(orderitementities);return order;}).collect(collectors.tolist());page.setrecords(orderentitylist);return new pageutils(page);}

为orderentity 新增属性

	@tablefield(exist = false)private list<orderitementity> orderitementitylist;
5.3.6.8 会员服务整合spring session(需要登录后查看订单信息)
5.3.6.8.1 配置拦截器

gulimall-member/src/main/ja/site/zhourui/gulimall/member/interceptor/loginuserinterceptor.ja

新增拦截器,放行member/**,远程调用接口

package site.zhourui.gulimall.member.interceptor;import org.springframework.stereotype.component;
import org.springframework.util.antpathmatcher;
import org.springframework.web.servlet.handlerinterceptor;
import site.zhourui.common.constant.authserverconstant;
import site.zhourui.common.vo.memberresponsevo;import jax.servlet.http.httpservletrequest;
import jax.servlet.http.httpservletresponse;
import jax.servlet.http.httpsession;
import ja.io.printwriter;/*** @author zr* @date 2021/12/31 15:34*/
@component
public class loginuserinterceptor implements handlerinterceptor {public static threadlocal<memberresponsevo> loginuser = new threadlocal<>();@overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {string uri = request.getrequesturi();boolean match = new antpathmatcher().match("/member/**", uri);if (match) {return true;}httpsession session = request.getsession();//获取登录的用户信息memberresponsevo attribute = (memberresponsevo) session.getattribute(authserverconstant.login_user);if (attribute != null) {//把登录后用户的信息放在threadlocal里面进行保存loginuser.set(attribute);return true;} else {//未登录,返回登录页面response.setcontenttype("text/html;charset=utf-8");printwriter out = response.getwriter();out.println("");// session.setattribute("msg", "请先进行登录");// response.sendredirect("http://auth.gulimall.com/login.html");return false;}}
}

gulimall-member/src/main/ja/site/zhourui/gulimall/member/config/memberwebconfig.ja

package site.zhourui.gulimall.member.config;import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.interceptorregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
import site.zhourui.gulimall.member.interceptor.loginuserinterceptor;/*** @author zr* @date 2021/12/31 15:36*/
@configuration
public class memberwebconfig implements webmvcconfigurer {@autowiredprivate loginuserinterceptor loginuserinterceptor;@overridepublic void addinterceptors(interceptorregistry registry) {registry.addinterceptor(loginuserinterceptor).addpathpatterns("/**");}}
5.3.6.8.2 整合springsession

依赖

        <dependency><groupid>org.springframework.sessiongroupid><artifactid>spring-session-data-redisartifactid>dependency><dependency><groupid>org.springframework.bootgroupid><artifactid>spring-boot-starter-data-redisartifactid><exclusions><exclusion><groupid>io.lettucegroupid><artifactid>lettuce-coreartifactid>exclusion>exclusions>dependency><dependency><groupid>redis.clientsgroupid><artifactid>jedisartifactid>dependency>

配置文件

spring:application:name: gulimall-memberredis:port: 6379host: 192.168.157.128jackson:date-format: yyyy-mm-dd hh:mm:sssession:store-type: redis

session自定义配置

gulimall-member/src/main/ja/site/zhourui/gulimall/member/config/gulimallsessionconfig.ja

package site.zhourui.gulimall.member.config;import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.serializer.genericjackson2jsonredisserializer;
import org.springframework.data.redis.serializer.redisserializer;
import org.springframework.session.web.http.cookieserializer;
import org.springframework.session.web.http.defaultcookieserializer;/*** @author zr* @date 2021/12/12 10:29*/
@configuration
public class gulimallsessionconfig {@beanpublic cookieserializer cookieserializer() {defaultcookieserializer cookieserializer = new defaultcookieserializer();//放大作用域cookieserializer.setdomainname("gulimall.com");cookieserializer.setcookiename("gulisession");cookieserializer.setcookiemaxage(60*60*24*7);return cookieserializer;}@beanpublic redisserializer<object> springsessiondefaultredisserializer() {return new genericjackson2jsonredisserializer();}
}

解决feign远程调用请求头丢失问题

gulimall-member/src/main/ja/site/zhourui/gulimall/member/config/gulifeignconfig.ja

package site.zhourui.gulimall.member.config;import feign.requestinterceptor;
import feign.requesttemplate;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.context.request.requestcontextholder;
import org.springframework.web.context.request.servletrequestattributes;import jax.servlet.http.httpservletrequest;/*** @author zr* @date 2021/12/31 15:36*/
@configuration
public class gulifeignconfig {@bean("requestinterceptor")public requestinterceptor requestinterceptor() {requestinterceptor requestinterceptor = new requestinterceptor() {@overridepublic void apply(requesttemplate template) {//1、使用requestcontextholder拿到刚进来的请求数据servletrequestattributes requestattributes = (servletrequestattributes) requestcontextholder.getrequestattributes();if (requestattributes != null) {//老请求httpservletrequest request = requestattributes.getrequest();if (request != null) {//2、同步请求头的数据(主要是cookie)//把老请求的cookie值放到新请求上来,进行一个同步string cookie = request.getheader("cookie");template.header("cookie", cookie);}}}};return requestinterceptor;}
}
5.3.6.9 设置支付成功回调接口

修改return_url地址为member服务的订单列表页请求地址

gulimall-order/src/main/resources/application.yaml

  return_url: http://member.gulimall.com/memberorder.html
5.3.6.10 获取支付宝异步通知
  • 订单支付成功后支付宝会回调商户接口,这个时候需要修改订单状态
  • 由于同步跳转可能由于网络问题失败,所以使用异步通知
  • 支付宝使用的是最大努力通知方案,保障数据一致性,隔一段时间会通知商户支付成功,直到返回success
5.3.6.10.1 接收支付宝异步通知接口

gulimall-order/src/main/ja/site/zhourui/gulimall/order/listener/orderpayedlistener.ja

package site.zhourui.gulimall.order.listener;import com.alipay.api.alipayapiexception;
import com.alipay.api.internal.util.alipaysignature;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestbody;
import org.springframework.web.bind.annotation.restcontroller;
import site.zhourui.gulimall.order.config.alipaytemplate;
import site.zhourui.gulimall.order.service.orderservice;
import site.zhourui.gulimall.order.vo.payasyncvo;import jax.servlet.http.httpservletrequest;
import ja.io.unsupportedencodingexception;
import ja.util.hashmap;
import ja.util.map;/*** @author zr* @date 2021/12/31 17:30*/
@restcontroller
public class orderpayedlistener {@autowiredprivate orderservice orderservice;@autowiredprivate alipaytemplate alipaytemplate;@postmapping(value = "/payed/notify")public string handlealipayed(payasyncvo asyncvo, httpservletrequest request) throws alipayapiexception, unsupportedencodingexception {// 只要收到支付宝的异步通知,返回 success 支付宝便不再通知// 获取支付宝post过来反馈信息//todo 需要验签map<string, string> params = new hashmap<>();map<string, string[]> requestparams = request.getparametermap();for (string name : requestparams.keyset()) {string[] values = requestparams.get(name);string valuestr = "";for (int i = 0; i < values.length; i) {valuestr = (i == values.length - 1) ? valuestr  values[i]: valuestr  values[i]  ",";}//乱码解决,这段代码在出现乱码时使用// valuestr = new string(valuestr.getbytes("iso-8859-1"), "utf-8");params.put(name, valuestr);}boolean signverified = alipaysignature.rsacheckv1(params, alipaytemplate.getalipay_public_key(),alipaytemplate.getcharset(), alipaytemplate.getsign_type()); //调用sdk验证签名if (signverified) {system.out.println("签名验证成功...");//去修改订单状态string result = orderservice.handlepayresult(asyncvo);return result;} else {system.out.println("签名验证失败...");return "error";}}}

处理支付宝支付结果

gulimall-order/src/main/ja/site/zhourui/gulimall/order/service/impl/orderserviceimpl.ja

    /*** 处理支付宝的支付结果**/@transactional(rollbackfor = exception.class)@overridepublic string handlepayresult(payasyncvo asyncvo) {//保存交易流水信息paymentinfoentity paymentinfo = new paymentinfoentity();paymentinfo.setordersn(asyncvo.getout_trade_no());paymentinfo.setalipaytradeno(asyncvo.gettrade_no());paymentinfo.settotalamount(new bigdecimal(asyncvo.getbuyer_pay_amount()));paymentinfo.setsubject(asyncvo.getbody());paymentinfo.setpaymentstatus(asyncvo.gettrade_status());paymentinfo.setcreatetime(new date());paymentinfo.setcallbacktime(asyncvo.getnotify_time());//添加到数据库中this.paymentinfoservice.se(paymentinfo);//修改订单状态//获取当前状态string tradestatus = asyncvo.gettrade_status();if (tradestatus.equals("trade_success") || tradestatus.equals("trade_finished")) {//支付成功状态string ordersn = asyncvo.getout_trade_no(); //获取订单号this.updateorderstatus(ordersn,orderstatusenum.payed.getcode(), payconstant.alipay);}return "success";}/*** 修改订单状态* @param ordersn* @param code*/private void updateorderstatus(string ordersn, integer code,integer paytype) {this.basemapper.updateorderstatus(ordersn,code,paytype);}

修改订单状态

gulimall-order/src/main/ja/site/zhourui/gulimall/order/dao/orderdao.ja

    /*** 修改订单状态* @param ordersn* @param code* @param paytype*/void updateorderstatus(@param("ordersn") string ordersn,@param("code") integer code,@param("paytype") integer paytype);

gulimall-order/src/main/resources/mapper/order/orderdao.xml

    <update id="updateorderstatus">update oms_orderset `status` = #{code},modify_time = now(),pay_type = #{paytype},payment_time = now()where order_sn = #{ordersn}</update>
5.3.6.11 设置支付宝异步通知接口地址

修改notify_url地址为订单服务的回调接口地址

gulimall-order/src/main/resources/application.yaml

  notify_url: http://4wa8cx.natappfree.cc/payed/notify
5.3.6.12 是异步通知接口不被拦截

gulimall-order/src/main/ja/site/zhourui/gulimall/order/interceptor/loginuserinterceptor.ja

package site.zhourui.gulimall.order.interceptor;/*** @author zr* @date 2021/12/21 22:04*/import org.springframework.stereotype.component;
import org.springframework.util.antpathmatcher;
import org.springframework.web.servlet.handlerinterceptor;
import site.zhourui.common.constant.authserverconstant;
import site.zhourui.common.vo.memberresponsevo;import jax.servlet.http.httpservletrequest;
import jax.servlet.http.httpservletresponse;
import jax.servlet.http.httpsession;
import ja.io.printwriter;import static site.zhourui.common.constant.authserverconstant.login_user;/*** 登录拦截器* 从session中获取了登录信息(redis中),封装到了threadlocal中*/
@component
public class loginuserinterceptor implements handlerinterceptor {public static threadlocal<memberresponsevo> loginuser = new threadlocal<>();@overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {string uri = request.getrequesturi();antpathmatcher antpathmatcher = new antpathmatcher();boolean match = antpathmatcher.match("/order/order/status/**", uri);boolean match1 = antpathmatcher.match("/payed/notify", uri);if (match || match1) {return true;}httpsession session = request.getsession();memberresponsevo memberresponsevo = (memberresponsevo) session.getattribute(authserverconstant.login_user);if (memberresponsevo != null) {loginuser.set(memberresponsevo);return true;}else {session.setattribute("msg","请先登录");response.sendredirect("http://auth.gulimall.com/login.html");return false;}}
}
5.3.6.13 内网穿透设置异步通知地址
  • 将外网映射到本地的order.gulimall.com:80
  • 由于回调的请求头不是order.gulimall.com,因此nginx转发到网关后找不到对应的服务,所以需要对nginx进行设置

修改内网穿透接口

测试

nginx 配置域名转发

    listen       80;server_name   gulimall.com *.gulimall.com *.natappfree.cc;#server_name  search.gulimall.com;#charset koi8-r;#access_log  /var/log/nginx/log/host.access.log  main;location /static/ {root  /usr/share/nginx/html;}location /payed/ {proxy_set_header host order.gulimall.com;proxy_pass http://gulimall;}location / {proxy_set_header host $host;proxy_pass http://gulimall;}

拦截器放行通知接口

gulimall-order/src/main/ja/site/zhourui/gulimall/order/interceptor/loginuserinterceptor.ja

    @overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {string uri = request.getrequesturi();antpathmatcher antpathmatcher = new antpathmatcher();boolean match = antpathmatcher.match("/order/order/status/**", uri);boolean match1 = antpathmatcher.match("/payed/notify", uri);if (match || match1) {return true;}httpsession session = request.getsession();memberresponsevo memberresponsevo = (memberresponsevo) session.getattribute(authserverconstant.login_user);if (memberresponsevo != null) {loginuser.set(memberresponsevo);return true;}else {session.setattribute("msg","请先登录");response.sendredirect("http://auth.gulimall.com/login.html");return false;}}
5.3.6.13.1 日期格式问题
field error in object 'payasyncvo' on field 'notify_time': rejected value [2022-01-02 10:50:06]; codes [typemiatch.payasyncvo.notify_time,typemiatch.notify_time,typemiatch.ja.util.date,typemiatch]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [payasyncvo.notify_time,notify_time]; arguments []; default message [notify_time]]; default message [failed to convert property value of type 'ja.lang.string' to required type 'ja.util.date' for property 'notify_time'; nested exception is org.springframework.core.convert.conversionfailedexception: failed to convert from type [ja.lang.string] to type [ja.util.date] for value '2022-01-02 10:50:06'; nested exception is ja.lang.illegalargumentexception]]

中国的解决方案

spring:mvc:date-format: yyyy-mm-dd hh:mm:ss

订单号长度报错

修改oms_payment_info 的订单号长度

5.3.6.9 支付测试

付款成功后自动跳转到订单列表页

5.3.6.10 收单
  1. 订单在支付页,不支付,一直刷新,订单过期了才支付,订单状态改为已支付了,但是库存解锁了。

    • 使用支付宝自动收单功能解决。只要一段时间不支付,就不能支付了。

    • 效果

  2. 由于时延等问题。订单解锁完成,正在解锁库存的时候,异步通知才到

    • 订单解锁,手动调用收单
  3. 网络阻塞问题,订单支付成功的异步通知一直不到达

    • 查询订单列表时,ajax获取当前未支付的订单状态,查询订单状态时,再获取一下支付宝此订单的状态
  4. 其他各种问题

    • 每天晚上闲时下载支付宝对账单,一一进行对账

6 接口幂等性

6.1 什么是幂等性

接口幂等性就是用户对于同一操作发起的一次请求或者多次请求的结果是一致的, 不会因为多次点击而产生了副作用; 比如说支付场景, 用户购买了商品支付扣款成功, 但是返回结果的时候网络异常, 此时钱已经扣了, 用户再次点击按钮, 此时会进行第二次扣款, 返回结果成功, 用户查询余额返发现多扣钱了, 流水记录也变成了两条. . . ,这就没有保证接口的幂等性。

6.2 哪些情况需要防止

  • 用户多次点击按钮
  • 用户页面回退再次提交
  • 微服务互相调用, 由于网络问题, 导致请求失败。 feign 触发重试机制
  • 其他业务情况

6.3 什么情况下需要幂等

以 sql 为例, 有些操作是天然幂等的。

  • select * from table wher id=?, 无论执行多少次都不会改变状态, 是天然的幂等。
  • update tab1 set col1=1 where col2=2, 无论执行成功多少次状态都是一致的, 也是幂等操作。
  • delete from user where userid=1, 多次操作, 结果一样, 具备幂等性
  • insert into user(userid,name) values(1,‘a’) 如 userid 为唯一主键, 即重复操作上面的业务, 只会插入一条用户数据, 具备幂等性。
  • update tab1 set col1=col1 1 where col2=2, 每次执行的结果都会发生变化, 不是幂等的。
  • insert into user(userid,name) values(1,‘a’) 如 userid 不是主键, 可以重复, 那上面业务多次操作, 数据都会新增多条, 不具备幂等性。

6.4 幂等 中国的解决方案

6.4.1 token 机制 (本次使用)

  1. 服务端提供了发送 token 的接口。 我们在分析业务的时候, 哪些业务是存在幂等问题的,就必须在执行业务前, 先去获取 token, 服务器会把 token 保存到 redis 中。
  2. 然后调用业务接口请求时, 把 token 携带过去, 一般放在请求头部。
  3. 服务器判断 token 是否存在 redis 中, 存在表示第一次请求, 然后删除 token,继续执行业务。
  4. 如果判断 token 不存在 redis 中, 就表示是重复操作, 直接返回重复标记给 client, 这样就保证了业务代码, 不被重复执行。

危险性:

  1. 删除 token 还是后删除 token;

    • 先删除可能导致, 业务确实没有执行, 重试还带上之前 token, 由于防重设计导致,请求还是不能执行。
    • 后删除可能导致, 业务处理成功, 但是服务闪断, 出现超时, 没有删除 token, 别人继续重试, 导致业务被执行两边
    • 我们最好设计为先删除 token, 如果业务调用失败, 就重新获取 token 再次请求。
  2. token 获取、 比较和删除必须是原子性

    • redis.get(token) 、 token.equals、 redis.del(token)如果这两个操作不是原子, 可能导致, 高并发下, 都 get 到同样的数据, 判断都成功, 继续业务并发执行
    • 可以在 redis 使用 lua 脚本完成这个操作
    if redis.call('get', keys[1]) == argv[1] then return redis.call('del', keys[1]) else return 0 end  
    

6.4.2 各种锁机制

6.4.2.1 数据库悲观锁

select * from xxxx where id = 1 for update;
悲观锁使用时一般伴随事务一起使用, 数据锁定时间可能会很长, 需要根据实际情况选用。另外要注意的是, id 字段一定是主键或者唯一索引, 不然可能造成锁表的结果, 处理起来会非常麻烦。

6.4.2.2 数据库乐观锁

这种方法适合在更新的场景中,
update t_goods set count = count -1 , version = version 1 where good_id=2 and version = 1根据 version 版本, 也就是在操作库存前先获取当前商品的 version 版本号, 然后操作的时候带上此 version 号。 我们梳理下, 我们第一次操作库存时, 得到 version 为 1, 调用库存服务version 变成了 2; 但返回给订单服务出现了问题, 订单服务又一次发起调用库存服务, 当订单服务传如的 version 还是 1, 再执行上面的 sql 语句时, 就不会执行; 因为 version 已经变为 2 了, where 条件就不成立。 这样就保证了不管调用几次, 只会真正的处理一次。乐观锁主要使用于处理读多写少的问题

6.4.2.3 业务层分布式锁

如果多个机器可能在同一时间同时处理相同的数据, 比如多台机器定时任务都拿到了相同数据处理, 我们就可以加分布式锁, 锁定此数据, 处理完成后释放锁。 获取到锁的必须先判断这个数据是否被处理过。

6.4.3 各种唯一约束

6.4.3.1 数据库唯一约束

插入数据, 应该按照唯一索引进行插入, 比如订单号, 相同的订单就不可能有两条记录插入。我们在数据库层面防止重复。

这个机制是利用了数据库的主键唯一约束的特性, 解决了在 insert 场景时幂等问题。 但主键的要求不是自增的主键, 这样就需要业务生成全局唯一的主键。

如果是分库分表场景下, 路由规则要保证相同请求下, 落地在同一个数据库和同一表中, 要不然数据库主键约束就不起效果了, 因为是不同的数据库和表主键不相关。

6.4.3.2 redis set 防重

很多数据需要处理, 只能被处理一次, 比如我们可以计算数据的 md5 将其放入 redis 的 set,每次处理数据, 先看这个 md5 是否已经存在, 存在就不处理。

6.4.4 防重表

使用订单号 orderno 做为去重表的唯一索引, 把唯一索引插入去重表, 再进行业务操作, 且他们在同一个事务中。 这个保证了重复请求时, 因为去重表有唯一约束, 导致请求失败, 避免了幂等问题。 这里要注意的是, 去重表和业务表应该在同一库中, 这样就保证了在同一个事务, 即使业务操作失败了, 也会把去重表的数据回滚。 这个很好的保证了数据一致性。之前说的 redis 防重也算

6.4.5 全局请求唯一 id

调用接口时, 生成一个唯一 id, redis 将数据保存到集合中(去重) , 存在即处理过。可以使用 nginx 设置每一个请求的唯一 id;

proxy_set_header x-request-id $request_id;  

7 本地事务与分布式事务

7.1 本地事务

7.1.1 事务的基本性质

数据库事务的几个特性: 原子性(atomicity )、 一致性( consistency )、 隔离性或独立性( isolation)和持久性(durabilily), 简称就是 acid;

  • 原子性: 一系列的操作整体不可拆分, 要么同时成功, 要么同时失败
  • 一致性: 数据在事务的前后, 业务整体一致。
    • 转账。 a:1000; b:1000; 转 200 事务成功; a: 800 b: 1200
  • 隔离性: 事务之间互相隔离。
  • 持久性: 一旦事务成功, 数据一定会落盘在数据库。

在以往的单体应用中, 我们多个业务操作使用同一条连接操作不同的数据表, 一旦有异常,我们可以很容易的整体回滚;

7.1.2 事务的隔离级别

  1. read uncommitted(读未提交)
    该隔离级别的事务会读到其它未提交事务的数据, 此现象也称之为脏读。
  2. read committed( 读提交)
    一个事务可以读取另一个已提交的事务, 多次读取会造成不一样的结果, 此现象称为不可重复读问题, oracle 和 sql server 的默认隔离级别。
  3. repeatable read( 可重复读)
    该隔离级别是 mysql 默认的隔离级别, 在同一个事务里, select 的结果是事务开始时时间点的状态, 因此, 同样的 select 操作读到的结果会是一致的, 但是, 会有幻读现象。 mysql的 innodb 引擎可以通过 next-key locks 机制( 参考下文"行锁的算法"一节) 来避免幻读。
  4. serializable( 序列化)
    在该隔离级别下事务都是串行顺序执行的, mysql 数据库的 innodb 引擎会给读操作隐式加一把读共享锁, 从而避免了脏读、 不可重读复读和幻读问题。

7.1.3 事务的传播行为

  1. propagation_required: 如果当前没有事务, 就创建一个新事务, 如果当前存在事务,就加入该事务, 该设置是最常用的设置。
  2. propagation_supports: 支持当前事务, 如果当前存在事务, 就加入该事务, 如果当前不存在事务, 就以非事务执行。
  3. propagation_mandatory: 支持当前事务, 如果当前存在事务, 就加入该事务, 如果当前不存在事务, 就抛出异常。
  4. propagation_requires_new: 创建新事务, 无论当前存不存在事务, 都创建新事务。
  5. propagation_not_supported: 以非事务方式执行操作, 如果当前存在事务, 就把当前事务挂起。
  6. propagation_never: 以非事务方式执行, 如果当前存在事务, 则抛出异常。
  7. propagation_nested: 如果当前存在事务, 则在嵌套事务内执行。 如果当前没有事务,则执行与 propagation_required 类似的操作。

7.1.4 springboot 事务关键点

7.1.4.1 事务的自动配置

transactionautoconfiguration

7.1.4.2 本地事务的坑

在同一个类里面, 编写两个方法, 内部调用的时候, 会导致事务设置失效。 原因是没有用到代理对象的缘故。

解决办法

  1. 导入 spring-boot-starter-aop
  2. @enabletransactionmanagement(proxytargetclass = true)
  3. @enableaspectjautoproxy(exposeproxy=true)
  4. aopcontext.currentproxy() 调用方法

示例:

1、如果方法a、b、c都在同一个service里面,事务传播行为不生效,共享一个事务原理:事务是用代理对象来控制的,内部调用b(),c(),就相当于直接调用没有经过事务【绕过了代理对象】解决:不能使用this.b();也不能注入自己【要使用代理对象来调用事务方法】@transactional(timeout=30)
public void a() {b();// a事务传播给了b事务,并且b事务的设置失效c();// c单独创建一个新事务
}@transactional(propagation = propagation.required, timeout=2)
public void b() {}@transactional(propagation = propagation.requires_new)
public void c() {}

解决步骤

具体步骤:
1、引入aop依赖<!-- 引入aop,解决本地事务失效问题 --><dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-aop</artifactid></dependency>2、开启动态代理【默认使用jdk动态代理,需要有接口】
@enableaspectjautoproxy(exposeproxy = true)     //开启了aspect动态代理模式,对外暴露代理对象
好处:cglib继承的方式完成动态代理
exposeproxy = true:对外暴露代理对象3、获取动态代理对象
orderserviceimpl orderservice = (orderserviceimpl)aopcontext.currentproxy();
orderservice.b();
orderservice.c();

7.2 分布式事务

7.2.1 为什么会有分布式事务?

分布式系统经常出现的异常机器宕机、 网络异常、 消息丢失、 消息乱序、 数据错误、 不可靠的 tcp、 存储数据丢失…

分布式事务是企业集成中的一个技术难点, 也是每一个分布式系统架构中都会涉及到的一个东西, 特别是在微服务架构中, 几乎可

以说是无法避免。

7.2.2 cap定理与base理论

7.2.2.1 cap定理

cap 原则又称 cap 定理, 指的是在一个分布式系统中

  • 一致性(consistency) :
    • 在分布式系统中的所有数据备份, 在同一时刻是否同样的值。 (等同于所有节点访问同一份最新的数据副本)
  • 可用性(ailability)
    • 在集群中一部分节点故障后, 集群整体是否还能响应客户端的读写请求。 (对数据更新具备高可用性)
  • 分区容错性(partition tolerance)
    • 大多数分布式系统都分布在多个子网络。 每个子网络就叫做一个区(partition) 。分区容错的意思是, 区间通信可能失败。 比如, 一台服务器放在中国, 另一台服务器放在美国, 这就是两个区, 它们之间可能无法通信。

cap 原则指的是, 这三个要素最多只能同时实现两点, 不可能三者兼顾

一般来说, 分区容错无法避免, 因此可以认为 cap 的 p 总是成立。 cap 定理告诉我们,剩下的 c 和 a 无法同时做到(ca没有p就是单体应用,没有必要)。

如果满足p,此时要满足a(所有机器都可用包括通信故障那台【数据未同步】),就不能保证一致性【同步数据的通信线故障,无法同步】
如果满足p,此时要满足c,那网络通信故障的节点就不应该继续提供服务(因为他的数据不一致)【宕机的那台机器数据 无法同步】
ap:容易,就算未同步的数据也可用
cp:牺牲可用性

​ 1、算法:raft和paxos算法:http://thesecretlivesofdata.com/raft/【raft算法演示】

7.2.2.2 raft算法
7.2.2.2.1 领导选举机制
  1. 集群所有节点启动默认都是随从状态,在此期间每个随从都会自旋,如果该节点满足了自旋时间,那么该节点就会成为候选者,领导者需要在一定的时间内为所有随从者响应,告知自己还活着(心跳),随从者收到响应就会终止本次自旋开始新一轮的自旋
  2. 如果没有监听到到领导者的心跳,满足自旋时间的节点变成候选者,同时向其他节点发送投票请求(同时终止其自旋),终止其他节点自旋,成为候选者的可能有多个,此时候选者发起投票,直到有一个候选者获胜
  3. 最终成为领导

具体步骤:
1、选举超时 election timeout
随从变成候选者的时间【150ms and 300ms随机的】【自旋时间,如果没有收到领导的命令变成候选者】
例如:启动集群,3个节点获得随机自旋时间,自旋时间到了就成为候选节点
2、成为候选节点,并给自己投票1,然后给其他随从节点发送选举请求【随从节点的票可能投给更快的候选者】
随从节点的票一旦投出便重新自旋

3、心跳时间(heartbeat timeout):每隔一段时间发送一个心跳,然后随从节点刷新自旋时间【小于300ms,否则大家都成为候选者了】
此时领导网络延时,自旋结束产生候选者,产生新领导

4、有多个候选者,并且票数一样,就自旋重新投

7.2.2.2.2 领导日志复制(可保证数据一致性)

所有节点修改数据,都要通过领导来修改

  1. 客户端通知领导修改一个数据,领导先创建一个 节点日志
  2. 领导将这条日志 发送给所有所有随从节点【随从节点收到并返回确认消息给领导】
  3. 3、领导等待大多数随从节点的确认消息,领导提交数据,然后通知随从节点可以提交了
  4. 随从节点也提交数据。最后领导节点给请求返回提交成功

具体步骤:
1、领导收到后并不会马上给随从节点发送 日志,等待下一次心跳时发送日志
2、然后领导提交并马上返回请求提交成功。然后跟随下一个心跳发送随从 告诉其提交
3、可保证数据一致性【例如选出来两个领导,不同机房。2个和3个组成两个群】

demo:此时2个的那个客户端发请求,一直保存失败,因为不是大多数人成功【所以数据未提交】,但是另外一边3个节点组成的集群可以保存成功【大多数节点】
如果此时两个集群恢复了数据通信,旧领导退位,并且跟着旧领导未提交的数据需要回滚【低轮领导退位,新领导上位】
然后匹配上新领导的日志

7.2.2.3 cp的缺点

对于多数大型互联网应用的场景, 主机众多、 部署分散, 而且现在的集群规模越来越大, 所以节点故障、 网络故障是常态, 而且要保证服务可用性达到 99.99999%(n 个 9) , 即保证p 和 a, 舍弃 c。

7.2.2.4 base 理论

是对 cap 理论的延伸, 思想是即使无法做到强一致性(cap 的一致性就是强一致性) , 但可以采用适当的采取弱一致性, 即最终一致性

base 是指

  • 基本可用(basically ailable)
    • 基本可用是指分布式系统在出现故障的时候, 允许损失部分可用性(例如响应时间、功能上的可用性) , 允许损失部分可用性。 需要注意的是, 基本可用绝不等价于系统不可用
    • 响应时间上的损失: 正常情况下搜索引擎需要在 0.5 秒之内返回给用户相应的查询结果, 但由于出现故障(比如系统部分机房发生断电或断网故障) , 查询结果的响应时间增加到了 1~2 秒。
    • 功能上的损失: 购物网站在购物高峰(如双十一) 时, 为了保护系统的稳定性,部分消费者可能会被引导到一个降级页面。
  • 软状态( soft state)
    • 软状态是指允许系统存在中间状态, 而该中间状态不会影响系统整体可用性。 分布式存储中一般一份数据会有多个副本, 允许不同副本同步的延时就是软状态的体现。 mysql replication 的异步复制也是一种体现。
  • 最终一致性( eventual consistency)
    • 最终一致性是指系统中的所有数据副本经过一定时间后, 最终能够达到一致的状态。 弱一致性和强一致性相反, 最终一致性是弱一致性的一种特殊情况。
7.2.2.5 强一致性、 弱一致性、 最终一致性

从客户端角度, 多进程并发访问时, 更新过的数据在不同进程如何获取的不同策略, 决定了不同的一致性。 对于关系型数据库, 要求更新过的数据能被后续的访问都能看到, 这是强一致性。 如果能容忍后续的部分或者全部访问不到, 则是弱一致性。 如果经过一段时间后要求能访问到更新后的数据, 则是最终一致性

7.2.3 分布式事务几种方案

7.2.3.1 2pc 模式

数据库支持的 2pc【 2 phase commit 二阶提交】 , 又叫做 xa transactions。mysql 从 5.5 版本开始支持, sql server 2005 开始

支持, oracle 7 开始支持。其中, xa 是一个两阶段提交协议, 该协议分为以下两个阶段:

第一阶段: 事务协调器要求每个涉及到事务的数据库预提交(precommit)此操作, 并反映是否可以提交.

第二阶段: 事务协调器要求每个数据库提交数据。

其中, 如果有任何一个数据库否决此次提交, 那么所有数据库都会被要求回滚它们在此事务中的那部分信息。

  • xa 协议比较简单, 而且一旦商业数据库实现了 xa 协议, 使用分布式事务的成本也比较低。
  • xa 性能不理想, 特别是在交易下单链路, 往往并发量很高, xa 无法满足高并发场景
  • xa 目前在商业数据库支持的比较理想, 在 mysql 数据库中支持的不太理想, mysql 的xa 实现, 没有记录 prepare 阶段日志, 主备切换回导致主库与备库数据不一致。
  • 许多 nosql 也没有支持 xa, 这让 xa 的应用场景变得非常狭隘。
  • 也有 3pc, 引入了超时机制( 无论协调者还是参与者, 在向对方发送请求后, 若长时间未收到回应则做出相应处理)
7.2.3.2 柔性事务-tcc 事务补偿型方案 (seata)

刚性事务: 遵循 acid 原则, 强一致性。
柔性事务: 遵循 base 理论, 最终一致性;
与刚性事务不同, 柔性事务允许一定时间内, 不同节点的数据不一致, 但要求最终一致。

一阶段 prepare 行为: 调用 自定义 的 prepare 逻辑。
二阶段 commit 行为: 调用 自定义 的 commit 逻辑。
二阶段 rollback 行为: 调用 自定义 的 rollback 逻辑。
所谓 tcc 模式, 是指支持把 自定义 的分支事务纳入到全局事务的管理中(seata)。

实现:
将业务代码拆成三部分。
1、try锁库存
2、confirm提交数据
3、事务补偿逻辑:一旦出现异常执行cancel来回滚【取消锁定库存】

其实就是2pc的手动实现

7.2.3.3 柔性事务-最大努力通知型方案【支付宝支付】【多,高并发场景】【基于消息服务mq】

按规律进行通知, 不保证数据一定能通知成功, 但会提供可查询操作接口进行核对。 这种方案主要用在与第三方系统通讯时, 比如: 调用微信或支付宝支付后的支付结果通知。 这种方案也是结合 mq 进行实现, 例如: 通过 mq 发送 http 请求, 设置最大通知次数。 达到通知次数后即不再通知。

案例: 银行通知、 商户通知等( 各大交易业务平台间的商户通知: 多次通知、 查询校对、 对账文件) , 支付宝的支付成功异步回调

例如支付宝支付成功,往mq发送消息【隔几秒发一个】
订单订阅topic,一旦订单确认消息,给支付宝发送确认,支付宝就不再通知了

以上就是小编对于谷粒商城??订单服务??高级篇笔记十一问题和相关问题的解答了,谷粒商城??订单服务??高级篇笔记十一的问题希望对你有用!

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文链接:https://www.andon8.com/444581.html

网站地图