Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate nacos with distributed transaction seata1.3.0

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how distributed transaction seata1.3.0 integrates nacos. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Introduction to seata-demo version of distributed transaction

JDK:1.8

Spring-cloud.version:Hoxton.SR8

Alibaba.version: 2.2.1.RELEASE

Seata.version:1.3.0

Nacos: 1.3.1

Integration steps

Download nacos. I downloaded nacos-server-1.3.2.zip.

Seata, I downloaded seata-server-1.3.0.zip.

Create a database server database script while each database participating in a transaction needs to add a table download address

Upload seata configuration to nacos

Download the nacos-config.sh file and put it in the unzipped file conf folder

Modify the file.conf configuration file under the conf directory, mainly to modify the custom transaction group name, the transaction log storage mode is db, and modify the database connection information

Service {# vgroup- > rgroup vgroup_mapping.tx-service-group = "default" # modify the transaction group name to: tx-service-group Corresponds to the custom name of the client # only support single node default.grouplist = "127.0.0.1 disable disable 8091" # degrade current not support enableDegrade = false # disable disable = false # unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent max.commit.retry.timeout = "- 1" max.rollback.retry.timeout = "- 1"} # # transaction log store Only used in seata-serverstore {# # store mode: file, db, redis mode = "db" # # database store property db {# # the implement of javax.sql.DataSource Such as DruidDataSource (druid) / BasicDataSource (dbcp) / HikariDataSource (hikari) etc. Datasource = "druid" # # mysql/oracle/postgresql/h3/oceanbase etc. DbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://192.168.240.129:3306/seat_server" user = "root" password = "Root_123456" minConn = 5 maxConn = 30 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000}}

Modify the registry.conf configuration under the conf directory, indicate that the registry is nacos, and modify the nacos connection information.

Registry {# file, nacos, eureka, redis, zk, consul, etcd3, sofa type = "nacos" nacos {application = "seata-server" serverAddr = "127.0.0.1 nacos 8848" group = "default" namespace = "cluster =" default "username =" nacos "password =" nacos "} config {# file, nacos, apollo, zk, consul, Etcd3 SEATA_GROUP type = "nacos" nacos {serverAddr = "127.0.0.1 serverAddr 8848" namespace = "default" group = "username =" nacos "password =" nacos "}}

Upload configuration to nacos: open git bash here in the conf directory and execute the nacos-config.sh input command sh nacos-config.sh-h 127.0.0.1

Create a client service

Create a seat_order library to store order information

Create a seat_storage library to store inventory information

Create a seat_account to store account information

Three libraries add log rollback tables

Complete database schematic diagram

5. Create seata-order-service (order service), seata-storage-service (inventory service), seata-account-service (account service)

The configuration content is more or less the same. Take the configuration of order service as an example, modify the bootstrap.yml file.

Server: port: 8180 spring: application: name: seata-order-service cloud: nacos: discovery: server-addr: 127.0.0.1 nacos 8848 username: "nacos" password: "nacos" config: server-addr: 127.0.0.1 username: "nacos" password: "nacos" mybatis: mapperLocations: classpath:mapper/*.xml

Modify the application.yml file

Seata: enabled: true application-id: seata-order-service tx-service-group: my_test_tx_group # tx-service-group needs to be the same as the name of the file.conf file under the conf directory config: type: nacos nacos: namespace: serverAddr: 127.0.0.1 namespace 8848 group: SEATA_GROUP username: "nacos" password: "nacos" registry: type: nacos nacos: application : seata-server server-addr: 127.0.0.1 username 8848 group: SEATA_GROUP namespace: username: "nacos" password: "nacos" spring: datasource: # driver-class-name: com.mysql.jdbc.Driver username: root password: 'Root_123456' url: jdbc:mysql://192.168.240.129:3306/seat_order?characterEncoding=utf-8&useSSL=false

Add main method

@ RestController@RequestMapping (value = "/ order") public class OrderController {@ Autowired private OrderService orderService; / * create order * / @ GetMapping ("/ create") @ GlobalTransactional public CommonResult create (Order order) {orderService.create (order); return new CommonResult ("order created successfully!", 200);}}

Start nacos,seata,seata-order-service,seata-storage-service,seata-account-service

2020-09-21 16 INFO 178.307 INFO 29768-[main] i.s.s.a.GlobalTransactionScanner: Initializing GlobalTransaction Clients. 2020-09-21 16 INFO 17 i.s.s.a.GlobalTransactionScanner 08.394 INFO 29768-[main] i.s.core.rpc.netty.NettyClientBootstrap: NettyClientBootstrap has started2020-09-21 16 pigment 17 i.s.s.a.GlobalTransactionScanner 08.394 INFO 29768-[main] i.s.s. A.GlobalTransactionScanner: Transaction Manager Client is initialized. ApplicationId [seata-order-service] txServiceGroup [my _ test_tx_group] 2020-09-21 16 test_tx_group 17 INFO 08.403 INFO 29768-- [main] io.seata.rm.datasource.AsyncWorker: Async Commit Buffer Limit: 100002020-09-21 16 V 17 Vera 08.403 INFO 29768-[main] i.s.rm.datasource.xa.ResourceManagerXA: ResourceManagerXA init. 2020-09-21 1617 INFO 29768 -[main] i.s.core.rpc.netty.NettyClientBootstrap: NettyClientBootstrap has started2020-09-21 16 purl 17 NettyClientBootstrap has started2020 08.408 INFO 29768-[main] i.s.s.a.GlobalTransactionScanner: Resource Manager is initialized. ApplicationId [seata-order-service] txServiceGroup [my _ test_tx_group] 2020-09-21 16 INFO 17purl 08.408 INFO 29768-[main] i.s.s.a.GlobalTransactionScanner: GlobalTransaction Clients are initialized.

3. Test distributed transactions

The initial state of the library

Visit http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100

2020-09-21 16 i.seata.tm.api.DefaultGlobalTransaction 27i.seata.tm.api.DefaultGlobalTransaction: Begin new global transaction [192.168.240.1 i.seata.tm.api.DefaultGlobalTransaction 51345170083352576] 2020-09-21 16 INFO 2722. 497 INFO 29768-[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-> order start 2020-09-21 16 display 2722. 508. INFO 29768-[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-- > order-service inventory deduction starts 2020-09-21 16 c.m.cloud.service.impl.OrderServiceImpl 2722. 530 INFO 29768-[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-- > order-service inventory deduction ends 2020-09-21 16 c.m.cloud.service.impl.OrderServiceImpl 2722.530 INFO 29768-- -[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-> start of deduction balance in order-service 2020-09-21 16 INFO 22.543 INFO 29768-[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-- > end of deduction balance in order-service 2020-09-21 16 order-service 27order-service 22.543 INFO 29768-[nio] -8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-> order-service modify order status start 2020-09-21 16 INFO 27 22.550 INFO 29768-[nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-- > order-service modify order status end 2020-09-21 16 Switzerland 27 order-service 22.550 INFO 29768-[nio -8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl:-> end of order 2020-09-21 168180-exec-9 2722.552 INFO 29768-[nio-8180-exec-9] i.seata.tm.api.DefaultGlobalTransaction: [192.168.240.1 INFO 8091INFO 51345170083352576] commit status: Committed2020-09-21 162722.560 INFO 29768-- [h_RMROLE_1_6_16] i.s .c.r.p.c.RmBranchCommitProcessor: rm client handle branch commit process:xid=192.168.240.1:8091:51345170083352576 BranchId=51345170112712704,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order ApplicationData=null2020-09-21 16 h_RMROLE_1_6_16 2722. 560 INFO 29768-- [h_RMROLE_1_6_16] io.seata.rm.AbstractRMHandler: Branch committing: 192.168.240.1 io.seata.rm.AbstractRMHandler: 192.168.240.1 Branch committing: 51345170083352576 51345170083352576 51345170112712704 jdbc:mysql://192.168.240.129:3306/seat_order null2020-09-21 16purl 2722.560 INFO 29768-- [h_RMROLE_1_6_16] io.seata.rm.AbstractRMHandler: Branch commit result: PhaseTwo_Committed2020-09-21 16 INFO 2715 22.622 INFO 29768-[h_RMROLE_1_7_16] i.s.c.r.p.c.RmBranchCommitProcessor: rm client handle branch commit process:xid=192.168.240.1:8091:51345170083352576 BranchId=51345170297262080,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order ApplicationData=null2020-09-21 16 io.seata.rm.AbstractRMHandler 2722 INFO 29768-[h_RMROLE_1_7_16] io.seata.rm.AbstractRMHandler: Branch committing: 192.168.240.1 io.seata.rm.AbstractRMHandler: 192.168.240.1 io.seata.rm.AbstractRMHandler: 192.168.240.1

Database status

Simulated anomaly

/ * * deduction of account balance * / @ Override public void decrease (Long userId, BigDecimal money) {LOGGER.info ("- > account-service starts deducting account balance"); / / simulated timeout exception, global transaction rollback try {Thread.sleep (3001000);} catch (InterruptedException e) {e.printStackTrace () } accountDao.decrease (userId,money); LOGGER.info ("- > end of account balance deduction in account-service");}

You will find that the database has not changed, and the console prints the log as follows

2020-09-21 16 INFO 29Begin new global transaction 53.921 INFO 29768-[nio-8180-exec-3] i.seata.tm.api.DefaultGlobalTransaction: Begin new global transaction [192.168.240.1 INFO 8091 Begin new global transaction 51345805197447168] 2020-09-21 1614 INFO 29768-[nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl:-- > order start at 2020-09-21 16 race 2953.931 INFO 29768-[nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl:-- > order-service inventory deduction start 2020-09-21 16 c.m.cloud.service.impl.OrderServiceImpl 29 order-service 53.957 INFO 29768-[nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl:-- > order-service inventory deduction end 2020-09-21 16c.m.cloud.service.impl.OrderServiceImpl 29 order-service 29768-- -[nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl:-> the deduction balance in order-service begins 2020-09-21 16 INFO 29 INFO 56.099 INFO 29768-[h_RMROLE_1_8_16] i.s.c.r.p.c.RmBranchRollbackProcessor: rm handle branch rollback process:xid=192.168.240.1:8091:51345805197447168 BranchId=51345805231001600,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order ApplicationData=null2020-09-21 16 io.seata.rm.AbstractRMHandler 29 h_RMROLE_1_8_16 56.100 INFO 29768-- [h_RMROLE_1_8_16] io.seata.rm.AbstractRMHandler: 192.168.240.1 io.seata.rm.AbstractRMHandler: 51345805197447168 51345805231001600 jdbc:mysql://192.168.240.129:3306/seat_order2020-09-21 16lane 29lane 56.142 29768-- [h_RMROLE_1_8_16] 29768: Xid 192.168.240.1:8091:51345805197447168 branch 51345805231001600 Undo_log deleted with GlobalFinished2020-09-21 16 io.seata.rm.AbstractRMHandler io.seata.rm.AbstractRMHandler: Branch Rollbacked result: PhaseTwo_Rollbacked2020-09-21 16 h_RMROLE_1_8_16 io.seata.rm.AbstractRMHandler 56.147 INFO 29768-[nio-8180-exec-3] i.seata.tm.api.DefaultGlobalTransaction: [192.168.240.1 io.seata.rm.AbstractRMHandler 51345805197447168] rollback status: Rollbacked2020-09-21 16:29:56. ERROR 29768-[nio-8180-exec-3] o.a.c.c.C. [/]. [dispatcherServlet]: Servlet.service () for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed Nested exception is feign.RetryableException: Read timed out executing GET http://seata-account-service/account/decrease?userId=1&money=100] with root causejava.net.SocketTimeoutException: Read timed out... Thank you for reading! This is the end of the article on "how distributed transaction seata1.3.0 integrates nacos". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report