Skip to content

Commit

Permalink
增加 seata 服务
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Apr 2, 2020
1 parent d2d18b8 commit 8b1a03a
Show file tree
Hide file tree
Showing 35 changed files with 1,002 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dubbo:
provider:
timeout: 1000 # 【重要】远程服务调用超时时间,单位:毫秒。默认为 1000 毫秒,胖友可以根据自己业务修改
UserRpcService:
version: 1.0.
version: 1.0.0
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者
scan:
base-packages: cn.iocoder.springboot.lab30.rpc.service
6 changes: 3 additions & 3 deletions lab-52/lab-52-multiple-datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<artifactId>lab-52-multiple-datasource</artifactId>

<dependencies>
<!-- TODO web -->
<!-- 实现对 Spring MVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -30,7 +30,7 @@
<version>5.1.48</version>
</dependency>

<!-- TODO mybatis -->
<!-- 实现对 MyBatis 的自动化配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
Expand All @@ -44,7 +44,7 @@
<version>3.0.0</version>
</dependency>

<!-- TODO 分布式事务 -->
<!-- 实现对 Seata 的自动化配置 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.iocoder.springboot.lab52.seatademo.service;

/**
* 库存 Service
* 商品 Service
*/
public interface StorageService {
public interface ProductService {

/**
* 扣减库存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import org.springframework.transaction.annotation.Transactional;

@Service
public class AmountServiceImpl implements AccountService {
public class AccountServiceImpl implements AccountService {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private AccountDao accountDao;

@Override
@DS(value = "pay-ds")
@DS(value = "account-ds")
@Transactional(propagation = Propagation.REQUIRES_NEW) // 开启新事物
public void reduceBalance(Long userId, Integer price) throws Exception {
logger.info("[reduceBalance] 当前 XID: {}", RootContext.getXID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cn.iocoder.springboot.lab52.seatademo.entity.OrderDO;
import cn.iocoder.springboot.lab52.seatademo.service.OrderService;
import cn.iocoder.springboot.lab52.seatademo.service.AccountService;
import cn.iocoder.springboot.lab52.seatademo.service.StorageService;
import cn.iocoder.springboot.lab52.seatademo.service.ProductService;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
Expand All @@ -22,10 +22,10 @@ public class OrderServiceImpl implements OrderService {
private OrderDao orderDao;

@Autowired
private AccountService payService;
private AccountService accountService;

@Autowired
private StorageService storageService;
private ProductService productService;

@GlobalTransactional
@Override
Expand All @@ -36,10 +36,10 @@ public Integer createOrder(Long userId, Long productId, Integer price) throws Ex
logger.info("[createOrder] 当前 XID: {}", RootContext.getXID());

// 扣减库存
storageService.reduceStock(productId, amount);
productService.reduceStock(productId, amount);

// 扣减余额
payService.reduceBalance(userId, price);
accountService.reduceBalance(userId, price);

// 保存订单
OrderDO order = new OrderDO().setUserId(userId).setProductId(productId).setPayAmount(amount * price);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.iocoder.springboot.lab52.seatademo.service.impl;

import cn.iocoder.springboot.lab52.seatademo.dao.ProductDao;
import cn.iocoder.springboot.lab52.seatademo.service.StorageService;
import cn.iocoder.springboot.lab52.seatademo.service.ProductService;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
Expand All @@ -12,15 +12,15 @@
import org.springframework.transaction.annotation.Transactional;

@Service
public class StorageServiceImpl implements StorageService {
public class ProductServiceImpl implements ProductService {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private ProductDao productDao;

@Override
@DS(value = "storage-ds")
@DS(value = "product-ds")
@Transactional(propagation = Propagation.REQUIRES_NEW) // 开启新事物
public void reduceStock(Long productId, Integer amount) throws Exception {
logger.info("[reduceStock] 当前 XID: {}", RootContext.getXID());
Expand All @@ -33,11 +33,11 @@ public void reduceStock(Long productId, Integer amount) throws Exception {
int updateCount = productDao.reduceStock(productId, amount);
// 扣除成功
if (updateCount == 0) {
logger.warn("[reduceStock] 扣除 {} 库存成功", productId);
logger.warn("[reduceStock] 扣除 {} 库存失败", productId);
throw new Exception("库存不足");
}
// 扣除失败
logger.info("[reduceStock] 扣除 {} 库存失败", productId);
logger.info("[reduceStock] 扣除 {} 库存成功", productId);
}

private void checkStock(Long productId, Integer requiredAmount) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ spring:
username: root
password:
# 账户 pay 数据源配置
amount-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_pay?useSSL=false&useUnicode=true&characterEncoding=UTF-8
account-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
# 库存 storage 数据源配置
storage-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_storage?useSSL=false&useUnicode=true&characterEncoding=UTF-8
# 商品 product 数据源配置
product-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_product?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
Expand Down
83 changes: 83 additions & 0 deletions lab-52/lab-52-multiple-datasource/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Order
DROP DATABASE IF EXISTS seata_order;
CREATE DATABASE seata_order;

CREATE TABLE seata_order.orders
(
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) DEFAULT NULL,
product_id INT(11) DEFAULT NULL,
pay_amount DECIMAL(10, 0) DEFAULT NULL,
add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

CREATE TABLE seata_order.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

# Product
DROP DATABASE IF EXISTS seata_product;
CREATE DATABASE seata_product;

CREATE TABLE seata_product.product
(
id INT(11) NOT NULL AUTO_INCREMENT,
stock INT(11) DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_product.product (id, stock) VALUES (1, 10); # 插入一条产品的库存

CREATE TABLE seata_product.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

# Account
DROP DATABASE IF EXISTS seata_account;
CREATE DATABASE seata_account;

CREATE TABLE seata_account.account
(
id INT(11) NOT NULL AUTO_INCREMENT,
balance DOUBLE DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

CREATE TABLE seata_account.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_account.account (id, balance) VALUES (1, 10);

82 changes: 82 additions & 0 deletions lab-53/lab-53-seata-at-dubbo-demo/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Order
DROP DATABASE IF EXISTS seata_order;
CREATE DATABASE seata_order;

CREATE TABLE seata_order.orders
(
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) DEFAULT NULL,
product_id INT(11) DEFAULT NULL,
pay_amount DECIMAL(10, 0) DEFAULT NULL,
add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

CREATE TABLE seata_order.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

# Product
DROP DATABASE IF EXISTS seata_product;
CREATE DATABASE seata_product;

CREATE TABLE seata_product.product
(
id INT(11) NOT NULL AUTO_INCREMENT,
stock INT(11) DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_product.product (id, stock) VALUES (1, 10); # 插入一条产品的库存

CREATE TABLE seata_product.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

# Account
DROP DATABASE IF EXISTS seata_account;
CREATE DATABASE seata_account;

CREATE TABLE seata_account.account
(
id INT(11) NOT NULL AUTO_INCREMENT,
balance DOUBLE DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

CREATE TABLE seata_account.undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_account.account (id, balance) VALUES (1, 10);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lab-53-seata-at-dubbo-demo</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-53-seata-at-dubbo-demo-account-service-api</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.iocoder.springboot.lab53.accountservice.api;

/**
* 账户 Service
*/
public interface AccountService {

/**
* 扣除余额
*
* @param userId 用户编号
* @param price 扣减金额
* @throws Exception 失败时抛出异常
*/
void reduceBalance(Long userId, Integer price) throws Exception;

}
Loading

0 comments on commit 8b1a03a

Please sign in to comment.