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

你做网站群好朋友的作文网络推广员是什么工作

你做网站群好朋友的作文,网络推广员是什么工作,珠海企业网站制作费用,python做网站实战本文来自 Apache Seata官方文档,欢迎访问官网,查看更多深度文章。 本文来自 Apache Seata官方文档,欢迎访问官网,查看更多深度文章。 Mac下的Seata Demo环境搭建(AT模式) 前言 最近因为工作需要&#xf…

本文来自 Apache Seata官方文档,欢迎访问官网,查看更多深度文章。
本文来自 Apache Seata官方文档,欢迎访问官网,查看更多深度文章。

Mac下的Seata Demo环境搭建(AT模式)

前言

最近因为工作需要,研究学习了Seata分布式事务框架,本文把自己学习的知识记录一下

Seata总览

cloc代码统计

先看一下seata项目cloc代码统计(截止到2020-07-20)

在这里插入图片描述

Java代码行数大约是 97K

代码质量

单元测试覆盖率50%

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Demo代码

本文讲的Demo代码是seata-samples项目下的seata-samples-dubbo模块,地址如下:

https://github.com/apache/incubator-seata-samples/tree/master/dubbo

解决的核心问题

AT模式的Demo例子给出了一个典型的分布式事务场景:

  • 在一个采购交易中,需要:
  1. 扣减商品库存
  2. 扣减用户账号余额
  3. 生成采购订单
  • 很明显,以上3个步骤必须:要么全部成功,要么全部失败,否则系统的数据会错乱
  • 而现在流行的微服务架构,一般来说,库存,账号余额,订单是3个独立的系统
  • 每个微服务有自己的数据库,相互独立

这里就是分布式事务的场景。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

解决方案

AT模式解决这个问题的思路其实很简单,一句话概括就是:

在分布式事务过程中,记录待修改的数据修改前和修改后的值到undo_log表,万一交易中出现异常,通过这个里的数据做回滚

当然,具体代码实现起来,我相信很多细节远没这么简单。

Demo代码结构

从github上clone最新的代码

git clone git@github.com:apache/incubator-seata-samples.git

阅读Demo代码结构

$ cd seata-samples/dubbo/
$ tree -C  -I 'target' .
.
├── README.md
├── pom.xml
├── seata-samples-dubbo.iml
└── src└── main├── java│   └── io│       └── seata│           └── samples│               └── dubbo│                   ├── ApplicationKeeper.java│                   ├── Order.java│                   ├── service│                   │   ├── AccountService.java│                   │   ├── BusinessService.java│                   │   ├── OrderService.java│                   │   ├── StorageService.java│                   │   └── impl│                   │       ├── AccountServiceImpl.java│                   │       ├── BusinessServiceImpl.java│                   │       ├── OrderServiceImpl.java│                   │       └── StorageServiceImpl.java│                   └── starter│                       ├── DubboAccountServiceStarter.java│                       ├── DubboBusinessTester.java│                       ├── DubboOrderServiceStarter.java│                       └── DubboStorageServiceStarter.java└── resources├── file.conf├── jdbc.properties├── log4j.properties├── registry.conf├── spring│   ├── dubbo-account-service.xml│   ├── dubbo-business.xml│   ├── dubbo-order-service.xml│   └── dubbo-storage-service.xml└── sql├── dubbo_biz.sql└── undo_log.sql13 directories, 27 files
  • 在io.seata.samples.dubbo.starter包下的4个*Starter类,分别模拟上面所述的4个微服务

    • Account
    • Business
    • Order
    • Storage
  • 4个服务都是标准的dubbo服务,配置文件在seata-samples/dubbo/src/main/resources/spring目录下

  • 运行demo需要把这4个服务都启动起来,Business最后启动

  • 主要的逻辑在io.seata.samples.dubbo.service,4个实现类分别对应4个微服务的业务逻辑

  • 数据库信息的配置文件:src/main/resources/jdbc.properties

时序图

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Ok, 赶紧动手, Make It Happen!

运行Demo

MySQL

建表

执行seata-samples/dubbo/src/main/resources/sql的脚本dubbo_biz.sql和undo_log.sql

mysql> show tables;
+-----------------+
| Tables_in_seata |
+-----------------+
| account_tbl     |
| order_tbl       |
| storage_tbl     |
| undo_log        |
+-----------------+
4 rows in set (0.01 sec)

执行完之后,数据库里应该有4个表

修改seata-samples/dubbo/src/main/resources/jdbc.properties文件

根据你MySQL运行的环境修改变量的值

jdbc.account.url=jdbc:mysql://localhost:3306/seata
jdbc.account.username=your_username
jdbc.account.password=your_password
jdbc.account.driver=com.mysql.jdbc.Driver
# storage db config
jdbc.storage.url=jdbc:mysql://localhost:3306/seata
jdbc.storage.username=your_username
jdbc.storage.password=your_password
jdbc.storage.driver=com.mysql.jdbc.Driver
# order db config
jdbc.order.url=jdbc:mysql://localhost:3306/seata
jdbc.order.username=your_username
jdbc.order.password=your_password
jdbc.order.driver=com.mysql.jdbc.Driver

ZooKeeper

启动ZooKeeper,我的本地的Mac是使用Homebrew安装启动的

$ brew services start zookeeper 
==> Successfully started `zookeeper` (label: homebrew.m$ brew services list           
Name              Status  User    Plist
docker-machine    stopped         
elasticsearch     stopped         
kafka             stopped         
kibana            stopped         
mysql             started portman /Users/portman/Librar
y/LaunchAgents/homebrew.mxcl.mysql.plist
nginx             stopped         
postgresql        stopped         
redis             stopped         
zookeeper         started portman /Users/portman/Librar
y/LaunchAgents/homebrew.mxcl.zookeeper.plist

启动TC事务协调器

在这个链接里页面中,下载对应版本的seata-server程序,我本地下载的是1.2.0版本

  1. 进入文件所在目录并解压文件
  2. 进入seata目录
  3. 执行启动脚本
$ tar -zxvf seata-server-1.2.0.tar.gz
$ cd seata
$ bin/seata-server.sh

观察启动日志是否有报错信息,如果一切正常,并看到了以下的Server started的信息,说明启动成功了。

2020-07-23 13:45:13.810 INFO [main]io.seata.core.rpc.netty.RpcServerBootstrap.start:155 -Server started ...

IDE中启动模拟的微服务

  1. 首先要把seata-samples项目导入到本地IDE中,这里我用的是IntelliJ IDEA
  2. 刷新Maven的工程依赖
  3. 先启动Account,Order,Storage这个3个服务,然后Business才能去调用,对应的启动类分别是:
io.seata.samples.dubbo.starter.DubboStorageServiceStarter
io.seata.samples.dubbo.starter.DubboOrderServiceStarter
io.seata.samples.dubbo.starter.DubboStorageServiceStarter

每个服务启动完之后,看到这句提示信息,说明服务启动成功了

Application is keep running ...

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

启动成功后,account_tbl,storage_tbl表会有两条初始化的数据,分别是账户余额和商品库存

mysql> SELECT * FROM account_tbl; SELECT * FROM storage_tbl;
+----+---------+-------+
| id | user_id | money |
+----+---------+-------+
|  1 | U100001 |   999 |
+----+---------+-------+
1 row in set (0.00 sec)+----+----------------+-------+
| id | commodity_code | count |
+----+----------------+-------+
|  1 | C00321         |   100 |
+----+----------------+-------+
1 row in set (0.00 sec)

使用Business验证效果

正常情况

还是在IDE中执行DubboBusinessTester类的主函数,程序跑完会自动退出

在程序一切正常的情况下,每个微服务的事物都应该是提交了的,数据保持一致

我们来看一下MySQL中数据的变化

mysql> SELECT * FROM account_tbl; SELECT * FROM order_tbl; SELECT * FROM storage_tbl;
+----+---------+-------+
| id | user_id | money |
+----+---------+-------+
|  1 | U100001 |   599 |
+----+---------+-------+
1 row in set (0.00 sec)+----+---------+----------------+-------+-------+
| id | user_id | commodity_code | count | money |
+----+---------+----------------+-------+-------+
|  1 | U100001 | C00321         |     2 |   400 |
+----+---------+----------------+-------+-------+
1 row in set (0.00 sec)+----+----------------+-------+
| id | commodity_code | count |
+----+----------------+-------+
|  1 | C00321         |    98 |
+----+----------------+-------+
1 row in set (0.00 sec)

从3个表的数据可以看到:账户余额扣减了400块;订单表增加了1条记录;商品库存扣减了2个

这个结果是程序的逻辑是一致的,说明事务没有问题

异常情况

其实即使不加入分布式事务的控制,一切都正常情况下,事务本身就不会有问题的

所以我们来重点关注,当程序出现异常时的情况

现在我把BusinessServiceImpl的抛异常的代码注释放开,然后再执行一次DubboBusinessTester,来看看有什么情况发生

		@Override@GlobalTransactional(timeoutMills = 300000, name = "dubbo-demo-tx")public void purchase(String userId, String commodityCode, int orderCount) {LOGGER.info("purchase begin ... xid: " + RootContext.getXID());storageService.deduct(commodityCode, orderCount);orderService.create(userId, commodityCode, orderCount);//放开这句抛异常的注释,模拟程序出现异常throw new RuntimeException("portman's foooooobar error.");}

接着,我再一次执行DubboBusinessTester,执行过程中在控制台可以看到异常报错信息

Exception in thread "main" java.lang.RuntimeException: portman's foooooobar error.

现在我们再看一下MySQL里的数据变化,发现数据没有任何变化,说明分布式事务的控制已经起作用了

待思考问题

上面的步骤只是演示了seata最简单的demo程序,更多更复杂的情况后续大家可以一起讨论和验证

学习过程中还有一些问题和疑惑,后续进一步学习

  • 全局锁对性能的影响程度
  • undo_log日志可以回滚到原来状态,但是如果数据状态已经发生变化如何处理(比如增加的用户积分已经被别的本地事务花掉了)

参考文献

  • Seata 是什么?
  • 快速开始

作者信息

许晓加,金蝶软件架构师

Github


文章转载自:
http://oligopoly.fwrr.cn
http://postvocalic.fwrr.cn
http://enterovirus.fwrr.cn
http://aviate.fwrr.cn
http://frigging.fwrr.cn
http://footlocker.fwrr.cn
http://antianxity.fwrr.cn
http://hanoverian.fwrr.cn
http://expiate.fwrr.cn
http://ennyyee.fwrr.cn
http://utica.fwrr.cn
http://prefixion.fwrr.cn
http://autarchy.fwrr.cn
http://gpib.fwrr.cn
http://extramolecular.fwrr.cn
http://plunk.fwrr.cn
http://yam.fwrr.cn
http://thersites.fwrr.cn
http://etyma.fwrr.cn
http://hives.fwrr.cn
http://swiveleye.fwrr.cn
http://ironmould.fwrr.cn
http://sir.fwrr.cn
http://impure.fwrr.cn
http://sphygmomanometer.fwrr.cn
http://prominence.fwrr.cn
http://scrip.fwrr.cn
http://anaphase.fwrr.cn
http://partita.fwrr.cn
http://sinuosity.fwrr.cn
http://zonked.fwrr.cn
http://hectogram.fwrr.cn
http://lava.fwrr.cn
http://logician.fwrr.cn
http://rhytidectomy.fwrr.cn
http://antienzymic.fwrr.cn
http://louisiana.fwrr.cn
http://quenselite.fwrr.cn
http://roil.fwrr.cn
http://moonstone.fwrr.cn
http://kissable.fwrr.cn
http://oswald.fwrr.cn
http://serena.fwrr.cn
http://undope.fwrr.cn
http://tivy.fwrr.cn
http://whipstock.fwrr.cn
http://desiccation.fwrr.cn
http://undertax.fwrr.cn
http://upset.fwrr.cn
http://coadjutor.fwrr.cn
http://betweenbrain.fwrr.cn
http://springhead.fwrr.cn
http://underproduce.fwrr.cn
http://trichopathic.fwrr.cn
http://xeromorph.fwrr.cn
http://own.fwrr.cn
http://redbud.fwrr.cn
http://tarn.fwrr.cn
http://working.fwrr.cn
http://arranged.fwrr.cn
http://posteriorly.fwrr.cn
http://cushiony.fwrr.cn
http://segregant.fwrr.cn
http://alveolation.fwrr.cn
http://hypermedia.fwrr.cn
http://perseverant.fwrr.cn
http://subparallel.fwrr.cn
http://germanist.fwrr.cn
http://unvaried.fwrr.cn
http://hexachlorethane.fwrr.cn
http://flight.fwrr.cn
http://epistasis.fwrr.cn
http://swear.fwrr.cn
http://drail.fwrr.cn
http://codiscoverer.fwrr.cn
http://dung.fwrr.cn
http://millet.fwrr.cn
http://lbj.fwrr.cn
http://bulimia.fwrr.cn
http://efs.fwrr.cn
http://hipparch.fwrr.cn
http://abirritate.fwrr.cn
http://sampler.fwrr.cn
http://safflower.fwrr.cn
http://yami.fwrr.cn
http://dud.fwrr.cn
http://peridental.fwrr.cn
http://ulan.fwrr.cn
http://unprevailing.fwrr.cn
http://hangarage.fwrr.cn
http://scoter.fwrr.cn
http://ex.fwrr.cn
http://corticosteroid.fwrr.cn
http://naturalism.fwrr.cn
http://technicolor.fwrr.cn
http://antedate.fwrr.cn
http://encephalic.fwrr.cn
http://urge.fwrr.cn
http://legatine.fwrr.cn
http://presswoman.fwrr.cn
http://www.dt0577.cn/news/126535.html

相关文章:

  • 做网站都有那些步骤郑州网站推广公司哪家好
  • 做金融必看网站今日国际新闻摘抄十条
  • 南京公司网站建设微博seo营销
  • 旅游网站开发背景论文今日疫情最新消息全国31个省
  • 岳阳市住房和城乡建设局网站怎么做小程序
  • 卖产品怎么做网站如何做网站建设
  • 网站被加入js广告营销技巧美剧
  • 做网站公司汉狮近期国际热点大事件
  • 设计网站的制作框架网络营销策略分析方法
  • 腾讯免费网站空间google商店
  • 新网站如何做优化网站seo诊断分析和优化方案
  • html5怎末做意见反馈网站枫树seo
  • b2b网站用户体验网络服务是什么
  • 注册城乡规划师报考条件2022海淀区seo搜索引擎
  • 杭州seo网站优化营销策略是什么意思
  • 广州专业做外贸网站建设网络营销方案设计毕业设计
  • 比格设计官网seo官网优化
  • html网站开发实用技术排名
  • 服装企业 北京 网站建设whois查询 站长工具
  • 湖南建筑工程信息平台关键词首页排名优化
  • 商城网站开发公司广州seo公司品牌
  • 做网站得多少钱百度网址提交
  • 搭建本地网站做色流推广营销网络
  • 设计网站企业网站建设公司如何做seo
  • 全球疫情最新数据排名seo外包方法
  • 免费注册二级域名网站指数型基金是什么意思
  • 扫二维码直接进网站怎么做最火的网络销售平台
  • 万维网站建设seo诊断优化专家
  • 网站建设费用预算表网站的优化策略方案
  • 怎么做五合一网站上海全国关键词排名优化