微服务痛点-基于Dubbo + Seata的分布式事务(AT)模式 (5)

第二阶段: 二阶段如果是提交的话,因为“业务 SQL”在一阶段已经提交至数据库, 所以 Seata 框架只需将一阶段保存的快照数据和行锁删掉,完成数据清理即可。

下单异常流程

微服务痛点-基于Dubbo + Seata的分布式事务(AT)模式

第一阶段:在一阶段下单流程中,storage、order、account和business应用分别注册到Seata这个事务协调器上,当用户进行下单时,数据更新前后的日志将会别记录到每个数据库下的undo_log表中,并形成一个全局的锁。以上操作全部在一个数据库事务内完成,这样保证了一阶段操作的原子性。

第二阶段: 当下单出现异常时,Seata将会对数据进行回滚,回滚的逻辑是按照一阶段的日志。

演示

启动Dubbo、Seata、MySQ并初始化数据, 使各服务应用注册到Seata上。

Dubbo、Seata和MySQL服务

mushuwei@mushuweideMacBook-Pro-2 seata % docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0c9c325a039c mysql:latest "docker-entrypoint.s…" 2 weeks ago Up 7 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7 b8031fa865cd seataio/seata-server "java -Djava.securit…" 2 weeks ago Up 20 seconds 0.0.0.0:8091->8091/tcp seata_seata-server_1 2af927368a15 apache/dubbo-admin "java -XX:+UnlockExp…" 2 weeks ago Up 2 hours 0.0.0.0:8080->8080/tcp dubbo_admin_1 7afec07234c9 zookeeper "/docker-entrypoint.…" 2 weeks ago Up 2 hours 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp dubbo_zookeeper_1

初始化数据

mysql> use storage; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from storage; +----+----------------+------+-------+ | id | commodity_code | name | count | +----+----------------+------+-------+ | 1 | cola | ???? | 2000 | +----+----------------+------+-------+ 1 row in set (0.00 sec) mysql> use account; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from account; +----+---------+---------+ | id | user_id | amount | +----+---------+---------+ | 1 | user123 | 1250.00 | +----+---------+---------+ 1 row in set (0.00 sec) mysql> use order; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from order; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1 mysql> select * from `order`; Empty set (0.00 sec)

启动Storage、Account、Order和Business

微服务痛点-基于Dubbo + Seata的分布式事务(AT)模式

Seata上各应用的注册情况

Starting seata_seata-server_1 ... done Attaching to seata_seata-server_1 seata-server_1 | [0.001s][warning][gc] -Xloggc is deprecated. Will use -Xlog:gc:/var/log/seata_gc.log instead. seata-server_1 | [0.015s][info ][gc] Using G1 seata-server_1 | [0.841s][info ][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 14M->4M(32M) 11.654ms seata-server_1 | SLF4J: A number (18) of logging calls during the initialization phase have been intercepted and are seata-server_1 | SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system. seata-server_1 | SLF4J: See also #replay seata-server_1 | 08:16:30.938 INFO --- [ main] io.seata.server.Server : The server is running in container. seata-server_1 | 08:16:30.972 INFO --- [ main] io.seata.config.FileConfiguration : The file name of the operation is registry seata-server_1 | 08:16:30.980 INFO --- [ main] io.seata.config.FileConfiguration : The configuration file used is /seata-server/resources/registry.conf seata-server_1 | [1.385s][info ][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 15M->6M(32M) 14.280ms seata-server_1 | 08:16:31.221 INFO --- [ main] io.seata.config.FileConfiguration : The file name of the operation is file.conf seata-server_1 | 08:16:31.222 INFO --- [ main] io.seata.config.FileConfiguration : The configuration file used is file.conf seata-server_1 | WARNING: An illegal reflective access operation has occurred seata-server_1 | WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$2 (file:/seata-server/libs/cglib-3.1.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) seata-server_1 | WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$2 seata-server_1 | WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations seata-server_1 | WARNING: All illegal access operations will be denied in a future release seata-server_1 | [1.734s][info ][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 16M->7M(32M) 6.400ms seata-server_1 | [2.101s][info ][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 18M->7M(32M) 4.828ms seata-server_1 | 08:16:31.924 INFO --- [ main] i.s.core.rpc.netty.NettyServerBootstrap : Server started, listen port: 8091 seata-server_1 | 08:26:12.007 INFO --- [rverHandlerThread_1_1_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/storage', applicationId='seata-action-storage', transactionServiceGroup='service_tx_group'},channel:[id: 0xae1ea1b1, L:/172.20.0.2:8091 - R:/172.20.0.1:52380],client version:1.3.0 seata-server_1 | 08:26:12.080 INFO --- [rverHandlerThread_1_2_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/storage', applicationId='seata-action-storage', transactionServiceGroup='service_tx_group'},channel:[id: 0xae1ea1b1, L:/172.20.0.2:8091 - R:/172.20.0.1:52380],client version:1.3.0 seata-server_1 | 08:26:33.704 INFO --- [rverHandlerThread_1_3_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/account', applicationId='seata-action-account', transactionServiceGroup='service_tx_group'},channel:[id: 0xd949a994, L:/172.20.0.2:8091 - R:/172.20.0.1:52396],client version:1.3.0 seata-server_1 | 08:26:33.758 INFO --- [rverHandlerThread_1_4_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/account', applicationId='seata-action-account', transactionServiceGroup='service_tx_group'},channel:[id: 0xd949a994, L:/172.20.0.2:8091 - R:/172.20.0.1:52396],client version:1.3.0 seata-server_1 | 08:26:57.466 INFO --- [rverHandlerThread_1_5_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/order', applicationId='seata-action-order', transactionServiceGroup='service_tx_group'},channel:[id: 0xfd51f88b, L:/172.20.0.2:8091 - R:/172.20.0.1:52412],client version:1.3.0 seata-server_1 | 08:26:57.518 INFO --- [rverHandlerThread_1_6_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/order', applicationId='seata-action-order', transactionServiceGroup='service_tx_group'},channel:[id: 0xfd51f88b, L:/172.20.0.2:8091 - R:/172.20.0.1:52412],client version:1.3.0 seata-server_1 | 08:27:10.600 INFO --- [ettyServerNIOWorker_1_4_8] i.s.c.r.processor.server.RegTmProcessor : TM register success,message:RegisterTMRequest{applicationId='seata-action-storage', transactionServiceGroup='service_tx_group'},channel:[id: 0x0e0b6c24, L:/172.20.0.2:8091 - R:/172.20.0.1:52424],client version:1.3.0 seata-server_1 | 08:27:32.694 INFO --- [ettyServerNIOWorker_1_5_8] i.s.c.r.processor.server.RegTmProcessor : TM register success,message:RegisterTMRequest{applicationId='seata-action-account', transactionServiceGroup='service_tx_group'},channel:[id: 0x2fd20474, L:/172.20.0.2:8091 - R:/172.20.0.1:52432],client version:1.3.0 seata-server_1 | 08:27:56.453 INFO --- [ettyServerNIOWorker_1_6_8] i.s.c.r.processor.server.RegTmProcessor : TM register success,message:RegisterTMRequest{applicationId='seata-action-order', transactionServiceGroup='service_tx_group'},channel:[id: 0xc8f6ba94, L:/172.20.0.2:8091 - R:/172.20.0.1:52436],client version:1.3.0 seata-server_1 | 08:28:15.847 INFO --- [rverHandlerThread_1_7_500] i.s.c.r.processor.server.RegRmProcessor : RM register success,message:RegisterRMRequest{resourceIds='null', applicationId='seata-action-business', transactionServiceGroup='service_tx_group'},channel:[id: 0x9ef75d68, L:/172.20.0.2:8091 - R:/172.20.0.1:52444],client version:1.3.0 seata-server_1 | 08:28:15.863 INFO --- [ettyServerNIOWorker_1_7_8] i.s.c.r.processor.server.RegTmProcessor : TM register success,message:RegisterTMRequest{applicationId='seata-action-business', transactionServiceGroup='service_tx_group'},channel:[id: 0x2b6c19d5, L:/172.20.0.2:8091 - R:/172.20.0.1:52440],client version:1.3.0

检查各服务Service在Dubbo上的情况。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwgsj.html