1、下载seata的demo , https://github.com/seata/seata-samples , 使用其中的springboot-dubbo-seata
2、下载nacos-server , https://github.com/alibaba/nacos/releases
暂不使用数据库和集群, 直接启动即可, 访问http://127.0.0.1:8848/nacos 用户名:nacos , 密码:nacos
3、下载seata-server , https://github.com/seata/seata/releases
修改配置文件:
registry.conf
registry {
# type配置为file
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "file"
.................
}
config {
# type配置为file
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
................
file {
name = "file.conf"
}
}
file.conf
#其他不用改动
store {
## store mode: file、db
mode = "file"
..........
4、数据表(MySQL数据库)
springboot-dubbo-seata\sql 下有SQL脚本: 创建数据库 seata , 执行脚本(4个表在一个数据库中)
/*
Navicat MySQL Data Transfer
Source Server : account
Source Server Version : 50614
Source Host : localhost:3306
Source Database : db_gts_fescar
Target Server Type : MYSQL
Target Server Version : 50614
File Encoding : 65001
Date: 2019-01-26 10:23:10
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_account
-- ----------------------------
DROP TABLE IF EXISTS `t_account`;
CREATE TABLE `t_account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`amount` double(14,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_account
-- ----------------------------
INSERT INTO `t_account` VALUES ('1', '1', '4000.00');
-- ----------------------------
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_no` varchar(255) DEFAULT NULL,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT '0',
`amount` double(14,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_order
-- ----------------------------
-- ----------------------------
-- Table structure for t_storage
-- ----------------------------
DROP TABLE IF EXISTS `t_storage`;
CREATE TABLE `t_storage` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `commodity_code` (`commodity_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_storage
-- ----------------------------
INSERT INTO `t_storage` VALUES ('1', 'C201901140001', '水杯', '1000');
-- ----------------------------
-- Table structure for undo_log
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of undo_log
-- ----------------------------
SET FOREIGN_KEY_CHECKS=1;
5、修改代码中的配置文件
1、application.properties 只需修改其中数据库部分 , nacos注册地址默认为127.0.0.1:8848
#====================================mysql config============================================
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=mysql
2、registry.conf type="file"
3、file.conf store --》 mode="file"
6、启动顺序
start samples-account、samples-order、samples-storage、samples-business
7、使用POSTMan测试
use postman to send a post request:http://localhost:8104/business/dubbo/buy
传JSON数据:
{
"userId":"1",
"commodityCode":"P190510529590122",
"name":"fan",
"count":2,
"amount":"100"
}这样访问会返回 : 失败,
修改commodityCode :C201901140001 , 就会成功了,
8、回滚测试
test the rollback request
enter samples-business , change BusinessServiceImpl, uncomment the following code :
```
if (!flag) {
throw new RuntimeException("测试抛异常后,分布式事务回滚!");
}
```