当前位置: 首页 > news >正文

做餐饮企业网站的费用软文网站推荐

做餐饮企业网站的费用,软文网站推荐,网上的毕业设计代做网站靠谱吗,wordpress 登陆插件下载分布式事务一站式解决方案- 分布式事务一站式解决方案分布式事务产生背景三个概念Seata下载和安装实际业务模拟演示不加 GlobalTransactional 注解,正常操作下单不加 GlobalTransactional 注解,下单过程出异常或者超时了加 GlobalTransactional 注解&…

分布式事务一站式解决方案-

  • 分布式事务一站式解决方案
    • 分布式事务产生背景
    • 三个概念
    • Seata下载和安装
    • 实际业务模拟演示
      • 不加 @GlobalTransactional 注解,正常操作下单
      • 不加 @GlobalTransactional 注解,下单过程出异常或者超时了
      • 加 @GlobalTransactional 注解,下单过程出异常或者超时了
    • 原理
      • undo_log 表作用
      • 二阶段提交原理
      • 分布式事务的执行流程(下订单-减库存-账户更新)

分布式事务一站式解决方案

分布式事务产生背景

一般来说,如果是微服务架构,会采用分布式系统开发,既然是多个微服务,那肯定是有多个独立的数据库的,那问题来了,
在这里插入图片描述
所以迫切希望提供一种分布式事务,解决微服务架构下的分布式事务问题

三个概念

在这里插入图片描述

Seata下载和安装

下载就不说了,直接去官方网站下载最新版本即可,注意安装 Seata 之前需要启动 Nacos,下载 Nacos 后直接 startup.cmd -m standalone 启动即可。
解压 seata-server-2.0.0.zip ,然后进入 conf 目录,更改 application.yml 配置如下,注意 console.user.username console.user.password seata.security.secretKey seata.security.tokenValidityInMilliseconds 这4个一定要配置,不然启动报错

20:37:31.390  WARN --- [                     main] [letWebServerApplicationContext] [             refresh]  [] : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUserDetailsServiceImpl': Injection of autowired dependencies failed; 
nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'console.user.username' in value "${console.user.username}"
20:39:36.510  WARN --- [                     main] [letWebServerApplicationContext] [             refresh]  [] : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'tokenProvider'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jwtTokenUtils': Injection of autowired dependencies failed;
nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'seata.security.secretKey' in value "${seata.security.secretKey}
server:port: 7091
spring:application:name: seata-server
logging:config: classpath:logback-spring.xmlfile:path: ${log.home:${user.home}/logs/seata}extend:logstash-appender:destination: 127.0.0.1:4560kafka-appender:bootstrap-servers: 127.0.0.1:9092topic: logback_to_logstash
console:user:username: seatapassword: seata      seata:security:secretKey: 'seata'tokenValidityInMilliseconds: 1000000config:# support: nacos 、 consul 、 apollo 、 zk  、 etcd3type: nacosnacos:server-addr: 127.0.0.1:8848namespace:group: SEATA_GROUPusername: nacospassword: nacoscontext-path:##if use MSE Nacos with auth, mutex with username/password attribute#access-key:#secret-key:# data-id: seataServer.propertiesregistry:# support: nacos 、 eureka 、 redis 、 zk  、 consul 、 etcd3 、 sofatype: nacosnacos:application: seata-serverserver-addr: 127.0.0.1:8848group: SEATA_GROUPnamespace:cluster: defaultusername: nacospassword: nacoscontext-path:##if use MSE Nacos with auth, mutex with username/password attribute#access-key:#secret-key:store:# support: file 、 db 、 redis 、 raftmode: dbsession:mode: dblock:mode: dbfile:dir: sessionStoremax-branch-session-size: 16384max-global-session-size: 512file-write-buffer-cache-size: 16384session-reload-read-size: 100flush-disk-mode: asyncdb:datasource: druiddb-type: mysqldriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.133.128:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMTuser: rootpassword: rootmin-conn: 10max-conn: 100global-table: global_tablebranch-table: branch_tablelock-table: lock_tabledistributed-lock-table: distributed_lockquery-limit: 1000max-wait: 5000

启动成功便可以进入 Seata 前端管理页面
在这里插入图片描述

在这里插入图片描述
同时 Nacos 也可以看到 Seata 服务注册上来了
在这里插入图片描述

实际业务模拟演示

本次学习会用到如下三个模块,对应三个微服务,每个服务用的单独的数据库,其中 cloud-seata-order-service2001 是订单微服务、cloud-seata-storage-service2002 是库存微服务、cloud-seata-account-service2003 是账户微服务,具体的代码可以去 github 上获取
在这里插入图片描述
项目用的数据库及表如下
在这里插入图片描述
初始阶段,订单表、库存表、账户表数据如下:
在这里插入图片描述

不加 @GlobalTransactional 注解,正常操作下单

实际的业务需求就是 下订单-扣减库存-更新账户余额,由于现在是分布式系统,如何保证事务呢?
先看看正常操作下单,1号用户花费100块买了10个1号产品,发送如下请求 http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100, 此时数据库表数据是正常的
在这里插入图片描述

不加 @GlobalTransactional 注解,下单过程出异常或者超时了

假设,在下订单->扣减库存->更新账户余额,在更新账户余额这一步代码逻辑超时了或者出异常了,为什么这里超时设置的是65秒?因为 OpenFeign 远程调用的默认超时时间是 60秒

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {@Resourceprivate AccountMapper accountMapper;@Overridepublic void decrease(Long userId, Long money) {log.info("------------->AccountService 开始扣减余额");accountMapper.decrease(userId, money);log.info("------------->AccountService 开始扣减余额");// 超时异常timeout();// 抛出异常// int i = 10 / 0;}private void timeout() {try {TimeUnit.SECONDS.sleep(65);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

重启项目再发送一次请求,发现页面提示超时了,被全局异常捕获
在这里插入图片描述
查看表数据,发现库存扣减了,余额扣减了,但是订单状态是创建中…,这显然是有问题的
在这里插入图片描述

加 @GlobalTransactional 注解,下单过程出异常或者超时了

同样的过程,有了 GlobalTransactional 后,经过测试,正常下单,没问题;下单过程出异常或者超时了,数据库正确回滚,结果符合预期

@Override
// 微服务项目可能会有多个方法需要保证分布式事务,name 可以很好的区分
@GlobalTransactional(name = "create-order-transaction", rollbackFor = Exception.class)
public void create(Order order) {// xid全局事务检查String xid = RootContext.getXID();// 1. 新建订单log.info("-------------> 开始新建订单, XID: {}", xid);order.setStatus(0);int result = orderMapper.insertSelective(order);Order orderFromDB;if (result > 0) {orderFromDB = orderMapper.selectOne(order);log.info("-------------> 新建订单成功, OrderInfo: {}", orderFromDB);// 2. 扣减库存log.info("-------------> 开始扣减库存");storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());log.info("-------------> 扣减库存成功");// 3. 扣减账户余额log.info("-------------> 开始扣减余额");accountFeignApi.decrease(order.getUserId(), order.getMoney());log.info("-------------> 扣余额存成功");// 4. 修改订单状态log.info("-------------> 开始修改订单状态");Example whereCondition = new Example(Order.class);Example.Criteria criteria = whereCondition.createCriteria();criteria.andEqualTo("id", orderFromDB.getId());criteria.andEqualTo("status", 0);orderFromDB.setStatus(1);int updateResult = orderMapper.updateByExampleSelective(orderFromDB, whereCondition);log.info("-------------> 修改订单状态成功");}log.info("-------------> 结束新建订单, XID: {}", xid);
}

原理

答案就是 二阶段提交,从日志输出也可以看到,PhaseTwo_Rollbacked->二阶段回滚了…

2024-08-07T21:59:43.441+08:00  INFO 26572 --- [seata-account-service] [nio-2003-exec-2] e.wong.service.impl.AccountServiceImpl   : ------------->AccountService 开始扣减余额
2024-08-07T22:00:43.500+08:00  INFO 26572 --- [seata-account-service] [h_RMROLE_1_1_24] i.s.c.r.p.c.RmBranchRollbackProcessor    : rm handle branch rollback process:BranchRollbackRequest{xid='192.168.133.1:8091:2414480718769926145', branchId=2414480718769926148, branchType=AT, resourceId='jdbc:mysql://192.168.133.128:3306/seata_account', applicationData='null'}
2024-08-07T22:00:43.501+08:00  INFO 26572 --- [seata-account-service] [h_RMROLE_1_1_24] io.seata.rm.AbstractRMHandler            : Branch Rollbacking: 192.168.133.1:8091:2414480718769926145 2414480718769926148 jdbc:mysql://192.168.133.128:3306/seata_account
2024-08-07T22:00:43.542+08:00  INFO 26572 --- [seata-account-service] [h_RMROLE_1_1_24] i.s.r.d.undo.AbstractUndoLogManager      : xid 192.168.133.1:8091:2414480718769926145 branch 2414480718769926148, undo_log deleted with GlobalFinished
2024-08-07T22:00:43.543+08:00  INFO 26572 --- [seata-account-service] [h_RMROLE_1_1_24] i.seata.rm.datasource.DataSourceManager  : branch rollback success, xid:192.168.133.1:8091:2414480718769926145, branchId:2414480718769926148
2024-08-07T22:00:43.543+08:00  INFO 26572 --- [seata-account-service] [h_RMROLE_1_1_24] io.seata.rm.AbstractRMHandler            : Branch Rollbacked result: PhaseTwo_Rollbacked

undo_log 表作用

而且上面你会发现每个微服务数据库都有张 undo_log 表,这个是做什么的呢?
在订单业务方法加了GlobalTransactional 注解后,打开 65 秒注释,目的是观察中间状态,重新请求 http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100,打开 undo_log 表,发现此时有数据
在这里插入图片描述
JSON 格式化后结构如下
在这里插入图片描述
当然此时在 Seata 管理页面也可以看到分布式事务中间状态的数据
在这里插入图片描述

不过 undo_log 表的数据在事务成功提交或者回滚之后在自动删除

二阶段提交原理

第一阶段
在这里插入图片描述
二阶段有分成两种情况:正常提交和异常回滚
在这里插入图片描述
在这里插入图片描述

分布式事务的执行流程(下订单-减库存-账户更新)

在这里插入图片描述


文章转载自:
http://balkhash.wqfj.cn
http://unbundling.wqfj.cn
http://rode.wqfj.cn
http://fashionably.wqfj.cn
http://aha.wqfj.cn
http://calvary.wqfj.cn
http://programmetry.wqfj.cn
http://zingaro.wqfj.cn
http://precocity.wqfj.cn
http://gottwaldov.wqfj.cn
http://electrotaxis.wqfj.cn
http://discredited.wqfj.cn
http://foumart.wqfj.cn
http://surroundings.wqfj.cn
http://reductase.wqfj.cn
http://gallygaskins.wqfj.cn
http://tulipwood.wqfj.cn
http://warrison.wqfj.cn
http://componential.wqfj.cn
http://refine.wqfj.cn
http://pelota.wqfj.cn
http://grav.wqfj.cn
http://soccage.wqfj.cn
http://orchil.wqfj.cn
http://conus.wqfj.cn
http://notchboard.wqfj.cn
http://porphyropsin.wqfj.cn
http://discission.wqfj.cn
http://discission.wqfj.cn
http://smarmy.wqfj.cn
http://heterotopy.wqfj.cn
http://brushed.wqfj.cn
http://coattail.wqfj.cn
http://arginine.wqfj.cn
http://equipment.wqfj.cn
http://lrl.wqfj.cn
http://tectosphere.wqfj.cn
http://cassab.wqfj.cn
http://guilloche.wqfj.cn
http://fulbe.wqfj.cn
http://hade.wqfj.cn
http://clincher.wqfj.cn
http://noncontrastive.wqfj.cn
http://leporid.wqfj.cn
http://retread.wqfj.cn
http://recovery.wqfj.cn
http://mammalogy.wqfj.cn
http://epicentre.wqfj.cn
http://agreeableness.wqfj.cn
http://fibrosarcoma.wqfj.cn
http://piggyback.wqfj.cn
http://gramarye.wqfj.cn
http://microfilm.wqfj.cn
http://quidsworth.wqfj.cn
http://hyoscyamine.wqfj.cn
http://undertip.wqfj.cn
http://hibernaculum.wqfj.cn
http://gibberellin.wqfj.cn
http://sesamoid.wqfj.cn
http://cycloidal.wqfj.cn
http://hyperpyretic.wqfj.cn
http://valuableness.wqfj.cn
http://enamored.wqfj.cn
http://shul.wqfj.cn
http://gustily.wqfj.cn
http://caffeic.wqfj.cn
http://ibid.wqfj.cn
http://valspeak.wqfj.cn
http://sliphorn.wqfj.cn
http://blighty.wqfj.cn
http://annulated.wqfj.cn
http://understock.wqfj.cn
http://ramble.wqfj.cn
http://affectlessly.wqfj.cn
http://ooa.wqfj.cn
http://thigmotaxis.wqfj.cn
http://fughetta.wqfj.cn
http://invisibly.wqfj.cn
http://virosis.wqfj.cn
http://theophyline.wqfj.cn
http://intown.wqfj.cn
http://osteoradionecrosis.wqfj.cn
http://wiggly.wqfj.cn
http://attrition.wqfj.cn
http://dictaphone.wqfj.cn
http://heavyish.wqfj.cn
http://gauze.wqfj.cn
http://goalkeeper.wqfj.cn
http://rtm.wqfj.cn
http://triviality.wqfj.cn
http://bearward.wqfj.cn
http://eire.wqfj.cn
http://millionocracy.wqfj.cn
http://jackleg.wqfj.cn
http://zoosperm.wqfj.cn
http://leaflike.wqfj.cn
http://bonderize.wqfj.cn
http://tapestried.wqfj.cn
http://tang.wqfj.cn
http://photoceramic.wqfj.cn
http://www.hrbkazy.com/news/77091.html

相关文章:

  • 做网站与考研百度地图推广电话
  • 购物网站seo搜索引擎前期分析seo sem是什么
  • 做悬赏任务的网站企业网站制作步骤
  • 网站备案 类型中国最新军事新闻最新消息
  • 吉林网站建设平台实时热搜榜
  • 网页开发用什么语言百度整站优化
  • 网站设置了权限搜索引擎推广的费用
  • 即墨网站开发今天的重要新闻
  • 怎么用自己注册的域名做网站合肥网站优化
  • 如何做视频解析网站百度关键词指数查询工具
  • 坪山网站建设哪家便宜搜狗推广登录入口
  • 深圳网站网页制作公司seo基本概念
  • b2c的电商网站网页模板网站
  • 网站开发技术的历史网站推广排名收费
  • 哈尔滨做网站哪里好b2b多平台一键发布
  • 郴州网站建设较好的公司今天最新新闻
  • 做网站管理系统沈阳seo排名公司
  • 番禺区手机版网站建设广州专门做seo的公司
  • flash可以用来制作网页吗seo顾问服务公司
  • dede网站后台设置wap模板目录正规百度推广
  • 有哪些建设网站公司吗营销策略分析
  • 深圳头条新闻在线看seo排名优化教学
  • 做外贸大一点的网站指数函数和对数函数
  • php网站qq互联郑州百度seo关键词
  • 搭建直播网站需要怎么做站长工具综合查询2020
  • 做ppt找素材的网站网站seo优化公司
  • 做网站定金交多少合适全网关键词优化公司哪家好
  • 一个教做网页的网站济南seo公司报价
  • 浙0577 icp网站建设站长素材
  • 做网站都要学什么互联网营销怎么做