下载已有开源代码(包含已经实现的接口,看效果即可)
1. 接口实现调用方式如下:
2. 源码下载地址
https://github.com/YunaiV/SpringBoot-Labs 里面的 labx-17 代码
# 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);
server:
port: 8083
spring:
application:
name: account-service
datasource:
url: jdbc:mysql://192.168.28.134:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
cloud:
# Nacos 作为注册中心的配置项
nacos:
discovery:
server-addr: 192.168.28.134:8848
# Seata 配置项,对应 SeataProperties 类
seata:
application-id: account-service # Seata 应用编号,默认为 ${spring.application.name}
tx-service-group: my_test_tx_group # Seata 事务组编号,用于 TC 集群名
# Seata 服务配置项,对应 ServiceProperties 类
config:
type: nacos
nacos:
group: SEATA_GROUP
namespace:
server-addr: 192.168.28.134:8848
service:
# 虚拟组和分组的映射
vgroup-mapping:
my_test_tx_group: default
# Seata 注册中心配置项,对应 RegistryProperties 类
registry:
type: nacos # 注册中心类型,默认为 file
nacos:
cluster: default # 使用的 Seata 分组
namespace: # Nacos 命名空间
serverAddr: 192.168.28.134:8848 # Nacos 服务地址
enabled: true
注意上面的seata配置里面, tx-service-group: my_test_tx_group , my_test_tx_group这里要和nacos中的值保持一致,对应
的nacos seata配置key为 :service.vgroupMapping.my_test_tx_group
不一致会报错。
http://www.iocoder.cn/Spring-Cloud-Alibaba/Seata/?self 下 3. AT 模式 + Feign
特此记录demo运行过程中碰到的问题和成功步骤。