== Spring Cloud Alibaba Seata

=== Introducing Seata

Seata is a distributed transaction framework to ensure the data consistency of each service under the microservice architecture.


=== How to use seata


To include Seata in your project, use the starter with group ID `com.alibaba.cloud` and artifact ID `spring-cloud-starter-alibaba-seata`.

[source,xml]
----
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
----

The project provides relevant https://github.com/alibaba/spring-cloud-alibaba/tree/2021.x/spring-cloud-alibaba-examples/seata-example[Example], before running it, you need to complete the following steps:

1. Ensure that `nacos-server` is running locally on the `8848` port.
2. Configuration database, will application in the resources directory of the `account-server`, `order-service`, `storage-service` three applications. The following configuration in the yml file is modified to the actual configuration in your running environment.

    base:
        config:
            mdb:
                hostname: 127.0.0.1 #your mysql server ip address
                dbname: seata #your database name for test
                port: 3306 #your mysql server listening port
                username: root #your mysql server username
                password: root #your mysql server password

3. Create the database tables, you can refer to  `all.sql` under `spring-cloud-alibaba-examples/seata-example` for a quick operation.
    - According to the selected transaction mode, create a https://github.com/seata/seata/tree/develop/script/client[transaction log table]. For example, the default is AT mode, then enter at/db and select the corresponding database script executes
    - Create a https://github.com/seata/seata/tree/develop/script/server/db[status record table] related to seata transactions, including `global_table` , `branch_table` , `lock_table` , `distributed_lock`.
    - Create the database tables required by `spring-cloud-alibaba-examples/seata-example` .
4. Create the Nacos configuration in `spring-cloud-alibaba-examples/seata-example` .

    data id: seata.properties,
    Group: SEATA_ Group(Seata 1.5.1 default group)

    Configuration Content:
    service.vgroupMapping.order-service-tx-group=default
    service.vgroupMapping.account-service-tx-group=default
    service.vgroupMapping.business-service-tx-group=default
    service.vgroupMapping.storage-service-tx-group=default

5. Start Seata Server This describes SpringBoot and download server in two ways.
    - Run seata-server from `spring-cloud-alibaba-examples/seata-example` to start Seata server.
    - Run https://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html[seata-server.jar] provided by official website to start Seata server.
6. Start the sub-services under the `spring-cloud-alibaba-examples/seata-example` including `account-service` , `order-service` , `storage-service` , and finally start the global transaction control service `business-service`。
7. After launching the example, the following URLs are accessed through the GET method of HTTP to validate scenarios where other services are invoked through RestTemplate and FeignClient in `business-service` respectively.

    http://127.0.0.1:18081/seata/feign
    http://127.0.0.1:18081/seata/rest

=== Seata Dashboard

- Seata 1.5.1 supports Seata console local access console address: `http://127.0.0.1:7091`。
- Through the Seata console, you can observe the executing transaction information and global lock information, and delete the relevant information when the transaction is completed.


=== How do I verify the success of a distributed transaction?
==== Whether Xid information was successfully transmitted

In the Controller of the three services `account-server`, `order-service` and `storage-service`, the first logic executed is to output the Xid information in the RootContext. If you see that the correct Xid information is output, it changes every time and the Xid of all services in the same call is consistent. This indicates that the transfer and restore of Seata's Xid are normal.

==== Consistency of data in database

In this example, we simulate a scenario where a user purchases goods, StorageService is responsible for deducting the inventory quantity, OrderService is responsible for saving the order, and AccountService is responsible for deducting the user account balance.

To demonstrate the sample, we used Random in OrderService and AcountService. NextBoolean () randomly throws exceptions, simulating a scenario in which exceptions occur randomly when a service is invoked.

If the distributed transaction is valid, then the following equation should be true

- User Original Amount (1000) = User Existing Amount + Goods Unit Price (2) * Order Quantity * Goods Quantity per Order (2)
- Initial Quantity of Goods (100) = Existing Quantity of Goods + Order Quantity * Quantity of Goods per Order (2)

=== Support points for Spring Cloud

- Service providers that provide services through Spring MVC can automatically restore the Seata context when they receive HTTP requests with Seata information in the header.
- Support for automatic delivery of Seata context when service callers invoke through RestTemplate.
- Supports automatic delivery of the Seata context when a service caller invokes through a FeignClient.
- Supports scenarios where both SeataClient and Hystrix are used.
- Supports scenarios used by both SeataClient and Sentinel.