I. Overview
In the micro-service architecture, although we will try to avoid distributed transactions, but as long as the business this is a complex case not open around the problem, how to ensure data consistency does business? This paper describes the use of synchronous scenario Seata
is AT模式
to solve the consistency problem.
Seata
Is Ali Baba open source one-stop solution for distributed transactional middleware for efficient and business 0-invasive way to address micro-services distributed transaction issues facing the next scene
Two, Seata introduction
Overall business logic is based on the two-phase commit model, the core concepts include the following three roles:
- TM : the originator of the transaction. Used to tell TC, global transaction begin, commit, rollback.
- RM : specific transaction resources, RM each transaction will be registered as a branch in TC.
- TC : coordinator affairs seata-server, for receiving our business registration, commit and rollback.
Currently the Seata
two modes may be used correspond to different scenarios.
2.1. AT mode
The appropriate scene mode:
- Based on local support
ACID
transactional relational database. - Java applications, by
JDBC
accessing the database.
A typical distributed transaction process:
TM
ToTC
start a global transaction application, to create a successful global transactions and generate a globally uniqueXID
.XID
In the context of the spread of micro-service call link.RM
ToTC
register branch affairs, it is included in the jurisdiction of the corresponding XID global transaction.TM
ToTC
initiate aXID
global commit or roll back the resolution.TC
SchedulingXID
all branches under the jurisdiction of the completion of the transaction commit or rollback request.
2.2. MT mode
Similarly the mode logic TCC
requires custom implementations prepare
, commit
and rollback
logic for non-relational databases scenes
Three, Seata scene sample
Simulation under a simple single user scenario, four sub-projects were Bussiness (transaction originator) , the Order (creating an order) , Storage (deductions inventory) and Account (deductions account balance)
3.1. Server-side deployment of Seata
Discover
Registration, Config
configuration, and Store
memory modules use default is file
only suitable for stand-alone, when we installed were changed to use nacos
and Mysql
to support server-side cluster
3.1.1. Download the latest version and unzip
https://github.com/seata/seata/releases
3.1.2. Modify conf / registry.conf Configuration
Registry and configuration center default is file
changed here nacos
; setting registry and config node type
to nacos
modify serverAddr
your nacos
node address.
registry {
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
}
config {
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
}
3.1.3 Modify conf / nacos-config.txt configuration
Modify service.vgroup_mapping corresponding to the application name for itself; if there are multiple services, add the appropriate configuration
Default group name
${spring.application.name}-fescar-service-group
, byspring.cloud.alibaba.seata.tx-service-group
modifying the configurationModify store.mode to
db
, and modify the database configuration
3.1.4. Initialization seata configuration of nacos
cd conf
sh nacos-config.sh 192.168.28.130
After the success nacos
of the configuration can be seen in the list seata
of configuration
3.1.5. Initialize the database
Execution conf/db_store.sql
of the script
3.1.6. Start seata-server
sh bin/seata-server.sh -p 8091 -h 192.168.28.130
3.2. Application Configuration
3.2.1. Initialize the database
Execute scripts seata-demo.sql
In business-related need to add database undo_log tables for storing data rollback
3.2.2. Adding registry.conf Configuration
Directly seata-server are registry.conf
copied to each service to go, you do not need to modify
3.2.3. Modify the configuration
demo of each respective service modify the configuration file
- bootstrap.yml modify nacos address
- application.yml modify the database configuration
3.2.4. Agent configuration data source
Seata
Distributed transaction is achieved through a proxy data source, you need to configure io.seata.rm.datasource.DataSourceProxy
the Bean
, and is the @Primary
default data source, otherwise the transaction will not be rolled back, it can not be achieved Distributed Transaction
public class DataSourceProxyConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource druidDataSource() {
return new DruidDataSource();
}
@Primary
@Bean
public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
}
Because of the use of starter mybatis it is necessary to exclude DataSourceAutoConfiguration
, or will produce circular dependencies
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
3.2.5. Adding a global transaction transaction originator comment
The initiator of the transaction business-service
to add @GlobalTransactional
annotations
@GlobalTransactional
public void placeOrder(String userId) {
......
}
3.3. Test
It provides two interface test
- Transaction Success: excluding treasury successfully> Create Order Success> deduction account balance success
http: // localhost: 9090 / placeOrder - Transaction failed: excluding treasury successfully> Create Order Success> deduction account balances fail, the transaction is rolled back
http: // localhost: 9090 / placeOrderFallBack
3.4. Demo Download
https://gitee.com/zlt2000/microservices-platform/tree/master/zlt-demo/seata-demo
Recommended Reading
- Logs to troubleshoot difficult problems? Distributed link tracking log to help you
- zuul latest Sentinel integrated gateway flow control assembly
- Ali registry Nacos production deployment
- Spring Boot custom configuration item inside the IDE automatically prompted
- How dynamic routing Spring Cloud Zuul do? Nacos very simple to achieve integration
- How Spring Cloud service developers to resolve conflicts and instances scurrying?
Scan code concern surprise!