spring-boot-starter-seata:https://github.com/itrickzhang/spring-boot-starter-seata
server和client版本为0.4.1,Seata 一直在快速迭代在1.0 之前都有可能出现协议不兼容 尽量使用版本号一致
目前提供的示例是针对使用dubbo的服务,那Spring Boot的项目如何集成fescar呢?
Business Service购买商品的业务逻辑。整个业务逻辑由3个微服务驱动:
Storage service: 扣除给定商品的库存量.
Order service: 根据采购需求创建订单.
Account service: 借记用户帐户上的余额.
下载地址:https://github.com/alibaba/fescar/releases
DROP TABLE IF EXISTS `storage_tbl`;
CREATE TABLE `storage_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
`money` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `account_tbl`;
CREATE TABLE `account_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`money` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 注意此处0.3.0+ 增加唯一索引 ux_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=1 DEFAULT CHARSET=utf8;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-seata</artifactId>
<version>${project.version}</version>
</dependency>
使用注解@GlobalTransactional
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@RequestMapping(value = "/fescar/feign", method = RequestMethod.GET, produces = "application/json")
public String feign() {
LOGGER.info("business Service Begin ... xid: " + RootContext.getXID());
String result = storageService.storage(COMMODITY_CODE, ORDER_COUNT);
if (!SUCCESS.equals(result)) {
throw new RuntimeException();
}
result = orderService.order(USER_ID, COMMODITY_CODE, ORDER_COUNT);
if (!SUCCESS.equals(result)) {
throw new RuntimeException();
}
return SUCCESS;
}
spring-boot-starter-seata-sample
Start AccountService
Start StorageService
Start OrderService
Run BusinessService for demo test
启动demo
访问demo
数据库数据
我的公众号