Get started with distributed transactions

更新时间:
复制 MD 格式

Use the basic sample project to quickly get started with Global Transaction Service (GTS) and implement distributed transactions.

Introduction to the basic sample

The sample project simulates a fund transfer application. It includes two data sources:

  • Account A data source: Stores the funds for account A.
  • Account B data source: Stores the funds for account B.

GTS manages distributed transactions to ensure consistency between account A and account B.

Step 1: Download the sample project

For more information about the sample project, see gts-sample-simple.

Note For information about the old version of the getting started sample, see Introduction to the old sample.

Step 2: Build the sample project

  1. Initialize the database.
    1. This sample requires a MySQL database. You can set up a MySQL environment or use Alibaba Cloud ApsaraDB RDS.
    2. Create two databases for the sample. Then, run the init-a.sql and init-b.sql scripts from the project to create the required tables.
  2. Build the project.
    1. This project uses Maven for building. Ensure that you have an active Maven environment.
    2. Go to the root directory of the sample project and run the build.bat (for Windows) or build.sh (for Linux) script.
    After the script runs successfully, the GTS software development kit (SDK) packages are generated in the common/lib folder. The packages include the following:
    • Main client package: txc-client-${gts.sdk.version}.jar
    • Native support package for Spring Cloud: txc-client-springcloud-${gts.sdk.version}.jar

Step 3: Import the sample project into an IDE

To make it easier to run and understand the project, import it into an Integrated Development Environment (IDE).

Step 4: Run the sample project

Run the Main method of the Application class in the sample project. Check the log output in the console and review the code logic.

Key configurations and runtime mechanism

In the application.properties file, the parameters that start with the spring.cloud.txc prefix are key configurations for GTS. These parameters are described below:

  • Each service in the GTS global transaction call chain requires these configurations. This includes services such as Business, Storage, Order, and Account in the sample project.
  • spring.cloud.txc.txcAppName: Specifies a globally unique application name for each service.
  • spring.cloud.txc.txcServerGroup: The name of the GTS service instance, also known as the transaction group.
    • If you run the application locally and access the GTS service over the public network, use the public instance txc_test_public.1129****3855****.QD and the spring.cloud.txc.url configuration.
    • When running on an ECS instance in a production environment, use the full name of your GTS service instance and the spring.cloud.txc.accessKey and spring.cloud.txc.accessKey configurations.
  • spring.cloud.txc.url: The ingest endpoint for accessing the GTS service over the public network. This is a static field: https://test-cs-gts.aliyuncs.com. This configuration is required only if you access the service over the public network from a local environment.
  • spring.cloud.txc.accessKey and spring.cloud.txc.secretKey: If you run the application on an ECS instance, you must configure the AccessKey ID and AccessKey secret for authentication. By default, use the AccessKey ID and AccessKey secret of the user who purchased the GTS service instance. This user corresponds to the Account ID in the middle of the full instance name. You can also use the AccessKey ID and AccessKey secret of an authorized Resource Access Management (RAM) user. For more information, see Grant permissions on a transaction group to a RAM user.