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 analyze transaction management in micro-service mode based on Seata middleware

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Based on Seata middleware how to analyze transaction management in micro-service mode, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

1. Introduction to Seata 1. Seata components

Seata is an open source distributed transaction solution dedicated to providing high-performance and easy-to-use distributed transaction services. Seata will provide users with AT, TCC, SAGA and XA transaction modes, and create an one-stop distributed solution for users.

2. Support mode

AT mode

Based on a relational database that supports local ACID transactions.

Java application, access to the database through JDBC.

Phase 1: business data and rollback log records are committed in the same local transaction, releasing local locks and connection resources.

The second phase: the submission is asynchronized and completed very quickly. The rollback is reverse compensated through an one-stage rollback log.

TCC mode

A distributed global transaction, the whole is a two-phase commit model, the global transaction is composed of several branch transactions, branch transactions to meet the requirements of the two-phase commit model, that is, each branch transaction needs to have its own:

One-stage prepare behavior

Two-stage commit or rollback behavior

Saga mode

Saga pattern is a long transaction solution provided by SEATA. In Saga pattern, each participant in a business process submits a local transaction, and when a participant fails, the previous successful participant is compensated. One-stage forward service and two-stage compensation service are implemented by business development.

XA mode

XA is a distributed transaction protocol, which is a non-intrusive distributed transaction solution. XA commit protocol needs the database support of transaction participants. XA transactions have strong consistency. In the whole process of two-phase commit, resources will always hold locks. The disadvantage of poor performance is obvious.

Second, server deployment 1. Download component packages

Version 1.2: seata-server-1.2.0.zip

Extract the catalog

Bin: the server runs the startup script

Lib: stores the jar package of resources that the server depends on

Conf: configuration file directory.

2. Modify the configuration

File.conf configuration

Even though mode:db uses a database to store transaction information, you can also choose the file storage method.

File mode is stand-alone mode, and the global transaction session information is read and written in memory and the local file root.data is persisted with high performance.

Db mode is highly available, and global transaction session information is shared through db, resulting in poor performance.

Redis mode Seata-Server 1.3 or above is supported with high performance and the risk of loss of transaction information. Please configure the redis persistence configuration suitable for the current scenario in advance.

Store {# # store mode: file, db mode = "db" db {datasource = "druid" dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata_server" user = "root" password = "root" minConn = 5 maxConn = 30 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100maxWait = 5000}

Registry.conf configuration

Here, eureka is selected as the registry, and seata-server is also added to the registry as a service. No configuration center is used, so the config configuration can be done by default.

Registry {# file, nacos, eureka, redis, zk, consul, etcd3, sofa type = "eureka" eureka {serviceUrl = "http://localhost:8761/eureka" application =" default "weight =" 1 "} 3, transaction management table

Three transaction management tables need to be established in seata-server, that is, the MySQL library configured above:

Global transaction: global_table

Branch transaction: branch_table

Global lock: lock_table

Transaction rollback: undo_log

SQL scripts: mysql-script directory

4. Start the command

Linux environment: sh seata-server.sh

Third, business service building 1. Code structure

Seata-eureka: registry

Seata-order: order service

Seata-account: account service

Seata-inventor: inventory service

Seata-client: client servic

Account-feign: account Feign API

Inventory-feign: inventory Feign API

Order-feign: order Feign API

Request link: client-> order-> account + inventory to test the distributed transaction problem of the whole process.

2. Database structure

Seata_server:seata component server-side dependent library

Seata_account: simulated account database

Seata_inventor: simulated inventory database

Seata_order: simulated order database

Location of each library script: mysql-script/data-biz.sql

3. Start the service

Start in turn: registry, inventory service, account service, order service, client service

The Eureka service list is as follows:

4. Detailed explanation of Seata usage 1. Basic configuration of Seata

Several basic services are configured in the same way.

Conf configuration

File.conf focuses on the following, the name of the transaction group, which needs to be used in the yml file.

My_test_tx_group = "default"

Registry.conf: is the registry's choice.

2. Database configuration

Notice the transaction group name configuration here.

Spring: # name of transaction group cloud: alibaba: seata: tx-service-group: my_test_tx_group # data source configuration datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/seata_account username: root password: 123456

The database as a whole will be managed by Seata agent, the core API:DataSourceProxy.

Configurationpublic class SeataAccountConfig {@ Value ("${spring.application.name}") private String applicationName; @ Bean public GlobalTransactionScanner globalTransactionScanner () {return new GlobalTransactionScanner (applicationName, "test-tx-group");} @ Bean @ ConfigurationProperties (prefix = "spring.datasource.druid") public DruidDataSource druidDataSource () {return new DruidDataSource () } @ Primary @ Bean ("dataSource") public DataSourceProxy dataSourceProxy (DataSource druidDataSource) {return new DataSourceProxy (druidDataSource);} @ Bean public SqlSessionFactory sqlSessionFactory (DataSourceProxy dataSourceProxy) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean (); sqlSessionFactoryBean.setDataSource (dataSourceProxy); sqlSessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath*:/mapper/*.xml")); sqlSessionFactoryBean.setTransactionFactory (new SpringManagedTransactionFactory ()) Return sqlSessionFactoryBean.getObject ();}} 3, business code

Core note: GlobalTransactional, managing the overall distributed transaction.

@ Servicepublic class OrderServiceImpl implements OrderService {private final Logger LOGGER = LoggerFactory.getLogger (OrderServiceImpl.class); @ Resource private OrderMapper orderMapper; @ Resource private AccountFeign accountFeign; @ Resource private InventoryFeign inventoryFeign; @ GlobalTransactional @ Override public Integer createOrder (String orderNo) {LOGGER.info ("Order generating" + orderNo); / / order library Integer insertFlag = orderMapper.insert (orderNo) under this service / / handle account and inventory accountFeign.updateAccount (10L) based on feign interface; inventoryFeign.updateInventory (10); return insertFlag;}}

Testing process: throw an exception under any service, observe the overall transaction status, and observe whether there is an overall transaction control effect.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report