登陆

章鱼币竞猜-springboot集成分布式业务Seata

admin 2019-10-29 178人围观 ,发现0个评论

简介

github地址

spring-boot-starter-seata:https://github.com/itrickzhang/spring-boot-starter-seata

seata版别

server和client版别为0.4.1,Seata 一直在快速迭代在1.0 之前都有或许呈现协议不兼容 尽量运用版别号共同

阐明

现在供给的示例是针对运用dubbo的服务,那Spring Boot的项目怎么集成fescar呢?

快速开张骞端

---

运用事例

Business Service购买产品的业务逻辑。整个业务逻辑由3个微服务驱动:

  • Storage service: 扣除给定产品的库存量.
  • Order service: 依据收购需求创立订单.
  • Account service: 借记用户帐户上的余额.

恳求逻辑

fescar下载

下载地址: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(2章鱼币竞猜-springboot集成分布式业务Seata0) NOT NULL,
`xid` varchar(100) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_statu章鱼币竞猜-springboot集成分布式业务Seatas` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY K章鱼币竞猜-springboot集成分布式业务SeataEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

集成


org.springframework.boot
spring-boot-starter-seata
${project.version}

运用

运用注解@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;
}

demo运转

https://github.com/itrickzhang/spring-boot-starter-seata/tree/master/spring-boot-starter-seata-sample

  • Start AccountService
  • Start StorageService
  • Start OrderService
  • Run BusinessService for demo test

运转成果

发动demo

拜访demo

数据库数据

请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP