From c0b6a625a7719ba05f020cccb75532840a236112 Mon Sep 17 00:00:00 2001 From: yuluo <1481556636@qq.com> Date: Sat, 9 Dec 2023 21:20:00 +0800 Subject: [PATCH] feat: adapte 1.8.0 version in sca-2022.x --- spring-cloud-alibaba-examples/pom.xml | 1 - .../src/main/resources/application.yml | 2 + .../src/main/resources/application.yml | 2 + .../src/main/resources/application.yml | 2 + .../seata-example/readme-zh.md | 149 +++++++++----- .../seata-example/readme.md | 193 +++++++++++------- .../seata-example/seata-server/pom.xml | 43 ---- .../seata/SeataServerApplication.java | 32 --- .../src/main/resources/application.yml | 91 --------- .../src/main/resources/application.yml | 2 + 10 files changed, 221 insertions(+), 296 deletions(-) delete mode 100644 spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml delete mode 100644 spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java delete mode 100644 spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 6ba4d27ad..0a5a568b4 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -30,7 +30,6 @@ seata-example/order-service seata-example/storage-service seata-example/account-service - seata-example/seata-server rocketmq-example/rocketmq-comprehensive-example rocketmq-example/rocketmq-orderly-consume-example rocketmq-example/rocketmq-broadcast-example/rocketmq-broadcast-producer-example diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml index 32b8a49a6..078dd4f7f 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml +++ b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml @@ -15,6 +15,8 @@ spring: nacos: discovery: server-addr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' application: name: account-service main: diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml index 15436919a..c6cb0d6ef 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml @@ -6,6 +6,8 @@ spring: nacos: discovery: server-addr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' loadbalancer: ribbon: enabled:true diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml index 310e77cdb..fe55d8462 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml +++ b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml @@ -14,6 +14,8 @@ spring: nacos: discovery: server-addr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' application: name: order-service main: diff --git a/spring-cloud-alibaba-examples/seata-example/readme-zh.md b/spring-cloud-alibaba-examples/seata-example/readme-zh.md index a39ec527b..5e0fd45f4 100644 --- a/spring-cloud-alibaba-examples/seata-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/seata-example/readme-zh.md @@ -2,41 +2,19 @@ ## 项目说明 +本项目演示如何使用 Seata Starter 完成 Spring Cloud Alibaba 应用的分布式事务接入。 -本项目演示如何使用 Seata Starter 完成 Spring Cloud 应用的分布式事务接入。 - -[Seata](https://github.com/seata/seata) 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题。 - - +[Seata](https://github.com/seata/seata) 是阿里巴巴开源的分布式事务中间件,以高效并且对业务 0 侵入的方式,解决微服务场景下面临的分布式事务问题。 ## 准备工作 -在运行此示例之前,你需要先完成如下几步准备工作: +在运行此示例之前,需要完成以下几步准备工作: -1. 配置数据库 +### 1. 配置数据库 -2. 创建 UNDO_LOG 表 +> **注意**: 实际上,Seata 支持不同的应用使用完全不相干的数据库,但是这里为了简单地演示 Seata 如何在 Spring Cloud 应用中使用,所以选择了 Mysql 数据库。 -3. 创建 示例中 业务所需要的数据库表 - -4. 创建示例中Nacos data-id: `seata.properties` , Group: `SEATA_GROUP`(seata 1.5.1 默认分组) ,导入 [Nacos 配置](https://github.com/seata/seata/blob/1.5.0/script/config-center/config.txt) - 在seata.properties中增加示例中需要的如下[事务群组配置](https://seata.io/zh-cn/docs/user/configurations.html) -``` - 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. 启动 Seata Server - Seata 1.5.1 开始支持seata控制台 本地访问控制台地址:http://127.0.0.1:7091 - 通过seata控制台可以观察正在执行的事务信息和全局锁信息,事务执行结束即删除相关信息。 -### 配置数据库 - -首先,你需要有一个支持 InnoDB 引擎的 MySQL 数据库。 - -**注意**: 实际上,Seata 支持不同的应用使用完全不相干的数据库,但是这里为了简单地演示一个原理,所以我们选择了只使用一个数据库。 - -将 `account-server`、`order-service`、`storage-service` 这三个应用中的 resources 目录下的 `application.yml` 文件中的如下配置修改成你运行环境中的实际配置。 +将 `account-server`、`order-service`、`storage-service` 这三个应用中 resources 目录下的 `application.yml` 文件中的以下配置修改成本地环境中的数据库配置。 ``` base: @@ -49,11 +27,11 @@ base: password: your mysql server password ``` -### 创建 undo_log 表 +#### 创建 undo_log 表 Seata AT 模式 需要使用到 undo_log 表。 -``` $sql +```sql -- 注意此处0.3.0+ 增加唯一索引 ux_undo_log CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, @@ -69,9 +47,11 @@ CREATE TABLE `undo_log` ( UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ``` -### 导入 seata-server db模式所需要的数据库表 -在数据库中初始化[global_table、branch_table、lock_table、distributed_lock](https://github.com/seata/seata/blob/1.5.0/script/server/db/mysql.sql) -```$sql + +#### 导入 seata-server db 模式所需要的数据库表 +在数据库中初始化[global_table、branch_table、lock_table、distributed_lock](https://github.com/seata/seata/blob/1.8.0/script/server/db/mysql.sql) + +```sql -- -------------------------------- The script used when storeMode is 'db' -------------------------------- -- the table to store GlobalSession data CREATE TABLE IF NOT EXISTS `global_table` @@ -146,9 +126,10 @@ INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryComm INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0); INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0); ``` -### 创建 示例中 业务所需要的数据库表 -```$sql +#### 创建应用示例中业务所需要的数据库表 + +```sql DROP TABLE IF EXISTS `storage_tbl`; CREATE TABLE `storage_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -179,30 +160,87 @@ CREATE TABLE `account_tbl` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` -### 启动 Seata Server 这里介绍SpringBoot 和下载server两种方式 +### 2. 配置 Nacos + +> Spring Cloud Alibaba 适配了 Nacos 2.2.3 版本,在本示例中,使用 Nacos 2.2.3 作为 Seata 的配置中心组件。 -1.运行 seata-server 启动Seata server -示例中采用nacos 作为配置,注册中心 存储模式为:db 采用mysql +创建 Seata 的 Nacos 配置: data-id: `seata.properties` , Group: `SEATA_GROUP` (seata 1.8.0 默认分组) ,导入 [Seata Config](https://github.com/seata/seata/blob/1.8.0/script/config-center/config.txt) -2.或点击这个页面 [Seata 官网Github](https://github.com/seata/seata/releases),下载最新版本的 Seata Server 端. -进入解压之后的 bin 目录,执行如下命令来启动, 所有启动参数为可选项。 +在 `seata.properties` 配置文件中增加应用示例中需要的以下配置项:[事务群组配置](https://seata.io/zh-cn/docs/user/configurations.html) -```$shell -sh seata-server.sh -p $LISTEN_PORT -m $MODE(file or db) -h $HOST -e $ENV +```properties + 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 ``` --p seata-server 监听服务端口号 --m 存储模式,可选值:file、db。file 用于单点模式,db用于ha模式,当使用db存储模式,需要修改配置中store配置节点的数据库配置,同时在数据库中初始化[global_table、branch_table和 -lock_table](https://github.com/seata/seata/blob/1.5.0/script/server/db/mysql.sql) --h 用于解决seata-server和业务侧跨网络问题,其配置的host值直接显示到注册中心的服务可用地址host,当跨网络时这里需要配置为公网IP或NATIP,若都在同一局域网则无需配置 --e 用于解决多环境配置中心隔离问题 -采用如下命令来启动 Seata Server - -```$shell -sh seata-server.sh -p 8091 -m file + +### 3. 启动 Seata-server + +> Seata 1.5.1 开始支持控制台 本地访问控制台地址:http://127.0.0.1:7091,通过 Seata 内置的控制台可以观察正在执行的事务信息和全局锁信息,事务执行结束即删除相关信息。 + +#### 1. 下载 + +点击下载 [Seata 1.8.0](https://github.com/seata/seata/releases/download/v1.8.0/seata-server-1.8.0.zip) 版本。 + +#### 2. 配置 Seata-server + +修改 `seata-server-1.8.0\conf\application.yml` 配置文件中的以下配置项: + +- 注释 `group: SEATA_GROUP` +- 添加 Nacos 用户名和密码 + +```yml +seata: + # nacos配置 + config: + type: nacos + nacos: + server-addr: 127.0.0.1:8848 + namespace: + # group: SEATA_GROUP + username: nacos + password: nacos + context-path: + data-id: seataServer.properties + ##if use MSE Nacos with auth, mutex with username/password attribute + #access-key: + #secret-key: + registry: + # nacos配置 + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + # group: SEATA_GROUP + namespace: + cluster: default + username: nacos + password: nacos ``` -**注意** 如果你修改了endpoint且注册中心使用默认file类型,那么记得需要在各个示例工程中的 `file.conf` 文件中,修改 grouplist 的值(当registry.conf 中registry.type 或 config.type 为file 时会读取内部的file节点中的文件名,若type不为file将直接从配置类型的对应元数据的注册配置中心读取数据),推荐大家使用 nacos 作为配置注册中心。 +> **注意:** +> Nacos 2.2.3 开启鉴权,需要配置 `username` 和 `password` 属性,否则登陆失败。更多 Nacos 2.2.3 版本相关配置,参考 `nacos-example`。 +> **Seata-server 启动时的 Nacos 服务注册分组需要和示例应用中的分组保持一致,否则出现无法找到 seata-server 的错误!** +> 更多 Seata-server 以 Nacos 作为配置中心的配置请参考:https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose/#nacos-db + +### 3. 启动 Seata-server +Windows: + +```cmd +./seata-server.bat +``` + +Linux/Mac + +```shell +sh seata-server.sh +``` + +更多配置启动参数请参考:https://seata.io/zh-cn/docs/user/quickstart/#%E6%AD%A5%E9%AA%A4-4-%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1 + +**注意** 如果你修改了endpoint且注册中心使用默认file类型,那么记得需要在各个示例工程中的 `file.conf` 文件中,修改 grouplist 的值(当registry.conf 中registry.type 或 config.type 为file 时会读取内部的file节点中的文件名,若type不为file将直接从配置类型的对应元数据的注册配置中心读取数据),推荐大家使用 nacos 作为配置注册中心。 ## 运行示例 @@ -210,13 +248,18 @@ sh seata-server.sh -p 8091 -m file 启动示例后,通过 HTTP 的 GET 方法访问如下 URL,可以分别验证在 `business-service` 中 通过 RestTemplate 和 FeignClient 调用其他服务的场景。 -```$xslt +```shell http://127.0.0.1:18081/seata/feign http://127.0.0.1:18081/seata/rest ``` +调用服务接口时,可能出现两种返回 + +1. SUCCESS:调用接口服务成功; +2. 500 异常,business-service mock 异常。 + ## 如何验证分布式事务成功? ### Xid 信息是否成功传递 diff --git a/spring-cloud-alibaba-examples/seata-example/readme.md b/spring-cloud-alibaba-examples/seata-example/readme.md index 6b983e518..6deaf2dd0 100644 --- a/spring-cloud-alibaba-examples/seata-example/readme.md +++ b/spring-cloud-alibaba-examples/seata-example/readme.md @@ -1,43 +1,20 @@ # Seata Example -## Project Instruction - - -This project demonstrates how to use Seata starter to complete the distributed transaction access of spring cloud applications. - -[Seata](https://github.com/seata/seata) It is Alibaba open source distributed transaction middleware, which solves the distributed transaction problem in the microservice scenario in an efficient and non-invasive way. +## Project description +This project demonstrates how to use Seata Starter to complete the distributed transaction access of Spring Cloud Alibaba application. +[Seata](https://github.com/seata/seata) It is Alibaba's open source distributed transaction middleware, which solves the distributed transaction problems faced by micro-service scenarios in an efficient and zero-intrusion way. ## Preparations -Before running this example, you need to complete the following steps: - -1. Configure the database - -2. Create UNDO_ LOG table - -3. Create the database tables needed by the business in the example - -4. Create the Nacos configuration in the example, data id: `seata.properties` , Group: `SEATA_ Group` (Seata 1.5.1 default group) configuration import [nacos configuration](https://github.com/seata/seata/blob/1.5.0/script/config-center/config.txt) - At seata Add the following [transaction group configuration](https://seata.io/zh-cn/docs/user/configurations.html) required in the example to properties -``` - 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 - Since 1.5.1, Seata 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. - -### Configuration database +Before you run this sample, you need to complete the following steps: -First, you need a MySQL database that supports the InnoDB engine. +### 1. Configure the database -**NOTE**: In fact, Seata supports different applications that use totally unrelated databases, but here we chose to use only one database for a simple demonstration of one principle. +> Seata **Notice** actually supports disparate databases for different applications, but Mysql was chosen here for a simple demonstration of how Seata can be used in a Spring Cloud application. -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. +Modify the following configuration in the files under the `application.yml` resources directory in the three applications `account-server`, `order-service`, `storage-service` to the database configuration in the local environment. ``` base: @@ -48,15 +25,14 @@ base: port: your mysql server listening port username: your mysql server username password: your mysql server password - ``` -### Create undo_ Log table +#### Create the undo _ log table -Seata AT Mode Need to use undo_ Log table. +Seata AT mode requires the undo_log table. -``` $sql --- Notice here that 0.3.0+ increases the unique index ux_ Undo_ Log +```sql +-- Note that 0.3.0+ adds unique index ux_undo_log here CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, @@ -71,9 +47,11 @@ CREATE TABLE `undo_log` ( UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ``` -### Database tables needed to import seata-server DB schema -Initialize in database [global_table、branch_table、lock_table、distributed_lock](https://github.com/seata/seata/blob/1.5.0/script/server/db/mysql.sql) -```$sql + +#### Import the database tables required by the seata-server db schema +Initializing [global_table、branch_table、lock_table、distributed_lock](https://github.com/seata/seata/blob/1.8.0/script/server/db/mysql.sql) in the database + +```sql -- -------------------------------- The script used when storeMode is 'db' -------------------------------- -- the table to store GlobalSession data CREATE TABLE IF NOT EXISTS `global_table` @@ -148,9 +126,10 @@ INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryComm INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0); INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0); ``` -### Create the database tables needed by the business in the example -```$sql +#### Create the database tables required by the business in the application sample + +```sql DROP TABLE IF EXISTS `storage_tbl`; CREATE TABLE `storage_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -181,69 +160,131 @@ CREATE TABLE `account_tbl` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` -### Start Seata Server This describes SpringBoot and download server in two ways +### 2. Configure Nacos -1.Run seata-server to start Seata server -The example uses Nacos as the configuration and the registry storage mode is: DB uses MySQL +> Spring Cloud Alibaba is adapted with Nacos 2.2.3. In this example, Nacos 2.2.3 is used as the configuration center component of Seata. -2. Or click on this page GitHub, the official website of [Seata](https://github.com/seata/seata/releases ), download the latest version of Sata Server. -Enter the bin directory after unzipping and execute the following command to start with all the startup parameters optional. +Create Nacos configuration for Seata: data-id: `seata.properties`, Group: `SEATA_GROUP` (default grouping for seata 1.8.0), import -```$shell -sh seata-server.sh -p $LISTEN_PORT -m $MODE(file or db) -h $HOST -e $ENV +Add the following configuration items required in the application example to the `seata.properties` configuration file: [事务群组配置](https://seata.io/zh-cn/docs/user/configurations.html) + +```properties + 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 ``` --p seata-server listening service port number --m storage mode, optional values: file, db. File is for single-point mode and DB is for HA mode. When using DB storage mode, you need to modify the database configuration of the store configuration node in the configuration and initialize [global_table, branch_table, and -Lock_ Table](https://github.com/seata/seata/blob/1.5.0/script/server/db/mysql.sql ) --h is used to solve seata-server and business side cross-network problems. The configured host value is displayed directly to the registry service available address host, which needs to be configured as public network IP or NATIP when cross-network. If both are in the same local area network, no configuration is required --e for multi-environment configuration center isolation -Start Seata Server with the following command - -```$shell -sh seata-server.sh -p 8091 -m file + +### 3. Start Seata-server + +> Seata 1.5.1 supports console local access. Console address: http://127.0.0.1:7091, you can view the information about the transaction being executed and the global lock information through the built-in console of Seata. When the transaction is finished, the relevant information will be deleted. + +#### 1. Download + +Click Download [Seata 1.8.0](https://github.com/seata/seata/releases/download/v1.8.0/seata-server-1.8.0.zip) Version. + +#### 2. Configure Seata-server + +Modify `seata-server-1.8.0\conf\application.yml` the following configuration items in the configuration file: + +- Comment `group: SEATA_GROUP` +- Add Nacos username and password + +```yml +seata: + # nacos configuration + config: + type: nacos + nacos: + server-addr: 127.0.0.1:8848 + namespace: + # group: SEATA_GROUP + username: nacos + password: nacos + context-path: + data-id: seataServer.properties + ##if use MSE Nacos with auth, mutex with username/password attribute + #access-key: + #secret-key: + registry: + # nacos configuration + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + # group: SEATA_GROUP + namespace: + cluster: default + username: nacos + password: nacos ``` -**Note** If you modified the endpoint and the registry uses the default file type, remember the file you need in each of the sample projects. In the conf`file, modify the value of grouplist (when registry.type or config.type in registry.conf is file, the file name in the internal file node is read; if type is not file, the data is read directly from the registry configuration center for the corresponding metadata of the configuration type), Nacos is recommended as the configuration registry. +> **Notice** +> Nacos 2.2.3 enables authentication. Configuration `username` and `password` properties are required, otherwise login fails. For more Nacos 2.2.3 related configurations, refer to `nacos-example`. +> **The Nacos service registration group when seata-server is started must be consistent with the group in the sample application, otherwise an error that seata-server cannot be found will occur!** +> For more information about the configuration of Seata-server with Nacos as the configuration center, please refer to https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose/#nacos-db. -## Run Example +### 3. Start Seata-server -Run the Main functions of the three applications `account-server`, `order-service`, `storage-service` and `business-service`, respectively, to start the example. +Windows: + +```cmd +./seata-server.bat +``` -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. +Linux/Mac -```$xslt +```shell +sh seata-server.sh +``` + +For more configuration startup parameters, please refer to https://seata.io/zh-cn/docs/user/quickstart/#%E6%AD%A5%E9%AA%A4-4-%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1. + +**Notice** If you change the endpoint and the registry uses the default file type, remember that in the `file.conf` file in each sample project, Modify the value of grouplist (when the registry. Type or config. Type in the registry. Conf is file, the file name in the internal file node will be read. If the type is not file, the data will be directly read from the registration configuration center of the corresponding metadata of the configuration type. It is recommended to use nacos as the configuration registration center. + +## Run the sample + +Start the sample by running `account-server` the Main functions of the, `order-service`, `storage-service`, and `business-service` applications separately. + +After starting the sample, access the following URL through the GET method of HTTP to verify `business-service` the scenarios of calling other services through RestTemplate and FeignClient in respectively. + +```shell http://127.0.0.1:18081/seata/feign http://127.0.0.1:18081/seata/rest - ``` -## How do I verify the success of a distributed transaction? +When a service interface is invoked, two types of returns are possible + +1. SUCCESS: calling interface service succeeded; +2. 500 exception, business-service mock exception. + +## How do I verify that a distributed transaction is successful? -### Whether Xid information was successfully transmitted +### Xid information passed successfully -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 `account-server` the Controllers of, `order-service`, and `storage-service` services, the first logic to be executed is to output the Xid information in the RootContext. If the correct Xid information is output, that is, it changes every time. And that Xid of all the services in the same invocation are the same. Then it indicates that the passing and restoring of Seata's Xid is normal. -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. +### Whether the data in the database is consistent -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. +In this example, we simulate a scenario in which a user purchases goods. The Storage Service is responsible for deducting the inventory quantity, the Order Service is responsible for saving the order, and the Account service is responsible for deducting the balance of the user's account. -If the distributed transaction is valid, then the following equation should be true +To demonstrate the sample, we use Random. NextBoolean () to randomly throw exceptions in Order Service and AccountService, simulating a scenario where exceptions randomly occur during service invocation. +If a distributed transaction is in effect, then the following equation should hold -- User Original Amount (1000) = User Existing Amount + Goods Unit Price (2) * Order Quantity * Goods Quantity per Order (2) +- User's original amount (1000) = user's existing amount + unit price of goods (2) *Number of orders* quantity of goods per order (2) -- Initial Quantity of Goods (100) = Existing Quantity of Goods + Order Quantity * Quantity of Goods per Order (2) +- Initial quantity of goods (100) = Quantity on hand 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. +- Service providers that provide services through Spring MVC can automatically restore the Seata context when they receive an HTTP request with Seata information in the header. -- Support for automatic delivery of Seata context when service callers invoke through RestTemplate. +- Support the automatic passing of the Seata context when the service caller invokes through the RestTemplate. -- Supports automatic delivery of the Seata context when a service caller invokes through a FeignClient. +- Support the automatic passing of the Seata context when the service caller calls through FeignClient. -- Supports scenarios where both SeataClient and Hystrix are used. +- Scenarios where SeataClient and Hystrix are used together are supported. -- Supports scenarios used by both SeataClient and entinel. +- Scenarios where SeataClient and Sentinel are used together are supported. diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml b/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml deleted file mode 100644 index 62fbc5123..000000000 --- a/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - org.springframework.boot - spring-boot-starter-parent - 2.6.11 - - - 4.0.0 - seata-server - Spring Cloud Starter Alibaba Seata Example - Seata Server - jar - - - - io.seata - seata-server - 1.5.0 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - true - - 8 - 8 - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java deleted file mode 100644 index ce087487c..000000000 --- a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2013-2023 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.cloud.examples.seata; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author ZHANGQIAN - */ -@SpringBootApplication(scanBasePackages = { "io.seata" }) -public class SeataServerApplication { - - public static void main(String[] args) { - SpringApplication.run(io.seata.server.ServerApplication.class, args); - } - -} diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml deleted file mode 100644 index 539c29152..000000000 --- a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml +++ /dev/null @@ -1,91 +0,0 @@ -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 - -server: - port: 7091 - -spring: - application: - name: seata-server - -logging: - config: classpath:logback-spring.xml - file: - path: ${user.home}/logs/seata - extend: - logstash-appender: - destination: 127.0.0.1:4560 - kafka-appender: - bootstrap-servers: 127.0.0.1:9092 - topic: logback_to_logstash - -console: - user: - username: 'seata' - password: 'seata' - -seata: - config: - # support: nacos 、 consul 、 apollo 、 zk 、 etcd3 - type: nacos - nacos: - server-addr: 127.0.0.1:8848 - username: nacos - password: nacos - ##if use MSE Nacos with auth, mutex with username/password attribute - #access-key: "" - #secret-key: "" - data-id: seata.properties - registry: - # support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa - type: nacos - preferred-networks: 30.240.* - nacos: - application: seata-server - server-addr: 127.0.0.1:8848 - cluster: default - username: nacos - password: nacos - group: DEFAULT_GROUP - store: - # support: file 、 db 、 redis - mode: db - session: - mode: file - lock: - mode: file - file: - dir: sessionStore - max-branch-session-size: 16384 - max-global-session-size: 512 - file-write-buffer-cache-size: 16384 - session-reload-read-size: 100 - flush-disk-mode: async - db: - datasource: druid - db-type: mysql - driver-class-name: com.mysql.jdbc.Driver - url: jdbc:mysql://${base.config.mdb.hostname}:${base.config.mdb.port}/${base.config.mdb.dbname}?rewriteBatchedStatements=true - user: ${base.config.mdb.username} - password: ${base.config.mdb.password} - min-conn: 5 - max-conn: 100 - global-table: global_table - branch-table: branch_table - lock-table: lock_table - distributed-lock-table: distributed_lock - query-limit: 100 - max-wait: 5000 -# server: -# service-port: 8091 #If not configured, the default is '${server.port} + 1000' - security: - secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017 - tokenValidityInMilliseconds: 1800000 - ignore: - urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml index 6ae0e6e27..15388e8cc 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml @@ -15,6 +15,8 @@ spring: nacos: discovery: server-addr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' application: name: storage-service main: