How Spring Cloud synchronization scenario distributed transactions do? Try Seata

file

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 Seatais AT模式to solve the consistency problem.

SeataIs 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 Seatatwo modes may be used correspond to different scenarios.

2.1. AT mode

The appropriate scene mode:

  • Based on local support ACIDtransactional relational database.
  • Java applications, by JDBCaccessing the database.

file

 
A typical distributed transaction process:

  1. TMTo TCstart a global transaction application, to create a successful global transactions and generate a globally unique XID.
  2. XID In the context of the spread of micro-service call link.
  3. RMTo TCregister branch affairs, it is included in the jurisdiction of the corresponding XID global transaction.
  4. TMTo TCinitiate a XIDglobal commit or roll back the resolution.
  5. TCScheduling XIDall branches under the jurisdiction of the completion of the transaction commit or rollback request.

 

2.2. MT mode

Similarly the mode logic TCCrequires custom implementations prepare , commitand rollbacklogic for non-relational databases scenes

file

 

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)

file

3.1. Server-side deployment of Seata

file

DiscoverRegistration, Configconfiguration, and Storememory modules use default is fileonly suitable for stand-alone, when we installed were changed to use nacosand Mysqlto 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 filechanged here nacos; setting registry and config node typeto nacosmodify serverAddryour nacosnode 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

file

  • 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, by spring.cloud.alibaba.seata.tx-service-groupmodifying the configuration

  • Modify 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 nacosof the configuration can be seen in the list seataof configuration

file

 

3.1.5. Initialize the database

Execution conf/db_store.sqlof 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.confcopied to each service to go, you do not need to modify
file

 

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

SeataDistributed transaction is achieved through a proxy data source, you need to configure io.seata.rm.datasource.DataSourceProxythe Bean, and is the @Primarydefault 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-serviceto add @GlobalTransactionalannotations

@GlobalTransactional
public void placeOrder(String userId) {
    ......
}

 

3.3. Test

It provides two interface test

  1. Transaction Success: excluding treasury successfully> Create Order Success> deduction account balance success
    http: // localhost: 9090 / placeOrder
  2. 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

 
Scan code concern surprise!

file

Guess you like

Origin www.cnblogs.com/zlt2000/p/11525417.html