Skip to content

[Summer code 2022] saga-xa-sample-refactor #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: sample-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<version>1.1.0</version>
<name>seata-samples ${project.version}</name>
<properties>
<seata.version>1.4.2</seata.version>
<seata.version>1.5.2</seata.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down Expand Up @@ -63,28 +63,29 @@
<javax.annotation-api.version>1.3.1</javax.annotation-api.version>
</properties>
<modules>
<module>at-sample</module>
<module>dubbo</module>
<!-- <module>springboot</module> -->
<!-- <module>nacos</module> -->
<!-- <module>springboot-dubbo-seata</module> -->
<!-- <module>tcc</module> -->
<!-- <module>springcloud-jpa-seata</module> -->
<!-- <module>nutzboot-dubbo-fescar</module> -->
<!-- <module>ha</module> -->
<!-- <module>springcloud-eureka-seata</module> -->
<!-- <module>multiple-datasource</module> -->
<!-- <module>springboot-mybatis</module> -->
<!-- <module>springcloud-nacos-seata</module> -->
<!-- <module>api</module> -->
<!-- <module>springboot-shardingsphere-seata</module> -->
<!-- <module>multiple-datasource-mybatis-plus</module> -->
<!-- <module>saga</module> -->
<!-- <module>spring-cloud-alibaba-samples</module> -->
<!-- <module>seata-spring-boot-starter-samples</module> -->
<!-- <module>springboot-dubbo-seata-zk</module> -->
<!-- <module>seata-xa</module> -->
<!-- <module>seata-samples-jit</module> -->
<!-- <module>springcloud-seata-sharding-jdbc-mybatis-plus-samples</module> -->
<module>springboot</module>
<module>nacos</module>
<module>springboot-dubbo-seata</module>
<module>tcc-sample</module>
<module>springcloud-jpa-seata</module>
<module>nutzboot-dubbo-fescar</module>
<module>ha</module>
<module>springcloud-eureka-seata</module>
<module>multiple-datasource</module>
<module>springboot-mybatis</module>
<module>springcloud-nacos-seata</module>
<module>api</module>
<module>springboot-shardingsphere-seata</module>
<module>multiple-datasource-mybatis-plus</module>
<module>saga-sample</module>
<module>spring-cloud-alibaba-samples</module>
<module>seata-spring-boot-starter-samples</module>
<module>springboot-dubbo-seata-zk</module>
<module>xa-sample</module>
<module>seata-samples-jit</module>
<module>springcloud-seata-sharding-jdbc-mybatis-plus-samples</module>
</modules>

<dependencies>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-saga</artifactId>
<artifactId>saga-sample</artifactId>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-saga</artifactId>
<artifactId>saga-sample</artifactId>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
4 changes: 2 additions & 2 deletions saga/pom.xml → saga-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-samples-saga</artifactId>
<artifactId>saga-sample</artifactId>
<version>1.1.0</version>
<packaging>pom</packaging>
<name>seata-samples-saga ${project.version}</name>
<name>saga-sample ${project.version}</name>
<modules>
<module>local-saga-sample</module>
<module>sofarpc-saga-sample</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-saga</artifactId>
<artifactId>saga-sample</artifactId>
<version>1.1.0</version>
</parent>

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-tcc</artifactId>
<artifactId>tcc-sample</artifactId>
<version>1.1.0</version>
</parent>

Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-tcc</artifactId>
<artifactId>tcc-sample</artifactId>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-tcc</artifactId>
<artifactId>tcc-sample</artifactId>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
4 changes: 2 additions & 2 deletions tcc/pom.xml → tcc-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-samples-tcc</artifactId>
<artifactId>tcc-sample</artifactId>
<version>1.1.0</version>
<packaging>pom</packaging>
<name>seata-samples-tcc ${project.version}</name>
<name>tcc-sample ${project.version}</name>
<modules>
<module>dubbo-tcc-sample</module>
<module>local-tcc-sample</module>
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-samples-tcc</artifactId>
<artifactId>tcc-sample</artifactId>
<version>1.1.0</version>
</parent>

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "stock-xa", url = "127.0.0.1:8081")
public interface StockFeignClient {
@FeignClient(name = "inventory-xa", url = "127.0.0.1:8081")
public interface InventoryFeignClient {

@GetMapping("/deduct")
String deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") int count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.seata.core.context.RootContext;
import io.seata.sample.TestDatas;
import io.seata.sample.feign.OrderFeignClient;
import io.seata.sample.feign.StockFeignClient;
import io.seata.sample.feign.InventoryFeignClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,7 +24,7 @@ public class BusinessService {
public static final String FAIL = "FAIL";

@Autowired
private StockFeignClient stockFeignClient;
private InventoryFeignClient inventoryFeignClient;
@Autowired
private OrderFeignClient orderFeignClient;
@Autowired
Expand All @@ -35,7 +35,7 @@ public void purchase(String userId, String commodityCode, int orderCount, boolea
String xid = RootContext.getXID();
LOGGER.info("New Transaction Begins: " + xid);

String result = stockFeignClient.deduct(commodityCode, orderCount);
String result = inventoryFeignClient.deduct(commodityCode, orderCount);

if (!SUCCESS.equals(result)) {
throw new RuntimeException("库存服务调用失败,事务回滚!");
Expand All @@ -56,10 +56,10 @@ public void purchase(String userId, String commodityCode, int orderCount, boolea
public void initData() {
jdbcTemplate.update("delete from account_tbl");
jdbcTemplate.update("delete from order_tbl");
jdbcTemplate.update("delete from stock_tbl");
jdbcTemplate.update("delete from inventory_tbl");
jdbcTemplate.update("insert into account_tbl(user_id,money) values('" + TestDatas.USER_ID + "','10000') ");
jdbcTemplate.update(
"insert into stock_tbl(commodity_code,count) values('" + TestDatas.COMMODITY_CODE + "','100') ");
"insert into inventory_tbl(commodity_code,count) values('" + TestDatas.COMMODITY_CODE + "','100') ");
}

public boolean validData(String userId, String commodityCode) {
Expand All @@ -69,8 +69,8 @@ public boolean validData(String userId, String commodityCode) {
return false;
}

Map stockMap = jdbcTemplate.queryForMap("select * from stock_tbl where commodity_code='" + commodityCode + "'");
if (Integer.parseInt(stockMap.get("count").toString()) < 0) {
Map inventoryMap = jdbcTemplate.queryForMap("select * from inventory_tbl where commodity_code='" + commodityCode + "'");
if (Integer.parseInt(inventoryMap.get("count").toString()) < 0) {
// 库存被扣减为负:库存不足
return false;
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion seata-xa/stock-xa/pom.xml → xa-sample/inventory-xa/pom.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>stock-xa</artifactId>
<artifactId>inventory-xa</artifactId>
<packaging>jar</packaging>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StockXAApplication {
public class InventoryXAApplication {

public static void main(String[] args) {
SpringApplication.run(StockXAApplication.class, args);
SpringApplication.run(InventoryXAApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class StockXADataSourceConfiguration {
public class InventoryXADataSourceConfiguration {

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package io.seata.sample.controller;

import io.seata.sample.service.StockService;
import io.seata.sample.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static io.seata.sample.service.StockService.FAIL;
import static io.seata.sample.service.StockService.SUCCESS;
import static io.seata.sample.service.InventoryService.FAIL;
import static io.seata.sample.service.InventoryService.SUCCESS;

@RestController
public class StockController {
public class InventoryController {

@Autowired
private StockService stockService;
private InventoryService inventoryService;

@RequestMapping(value = "/deduct", method = RequestMethod.GET, produces = "application/json")
public String deduct(String commodityCode, int count) {
try {
stockService.deduct(commodityCode, count);
inventoryService.deduct(commodityCode, count);
} catch (Exception exx) {
exx.printStackTrace();
return FAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.stereotype.Service;

@Service
public class StockService {
public class InventoryService {

private static final Logger LOGGER = LoggerFactory.getLogger(StockService.class);
private static final Logger LOGGER = LoggerFactory.getLogger(InventoryService.class);

public static final String SUCCESS = "SUCCESS";
public static final String FAIL = "FAIL";
Expand All @@ -20,8 +20,8 @@ public class StockService {

public void deduct(String commodityCode, int count) {
String xid = RootContext.getXID();
LOGGER.info("deduct stock balance in transaction: " + xid);
jdbcTemplate.update("update stock_tbl set count = count - ? where commodity_code = ?",
LOGGER.info("deduct inventory balance in transaction: " + xid);
jdbcTemplate.update("update inventory_tbl set count = count - ? where commodity_code = ?",
new Object[] {count, commodityCode});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.application.name=stock-xa
spring.application.name=inventory-xa
spring.cloud.alibaba.seata.txServiceGroup=my_test_tx_group
server.port=8081
spring.datasource.url=jdbc:mysql://rm-2zetd9474ydd1g5955o.mysql.rds.aliyuncs.com:3306/fescar?useSSL=false&serverTimezone=UTC
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion seata-xa/pom.xml → xa-sample/pom.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<modules>
<module>account-xa</module>
<module>order-xa</module>
<module>stock-xa</module>
<module>inventory-xa</module>
<module>business-xa</module>
</modules>

Expand Down
4 changes: 2 additions & 2 deletions seata-xa/sql/all_in_one.sql → xa-sample/sql/all_in_one.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DROP TABLE IF EXISTS `stock_tbl`;
CREATE TABLE `stock_tbl`
DROP TABLE IF EXISTS `inventory_tbl`;
CREATE TABLE `inventory_tbl`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
Expand Down