Springboot微服务中引入Seata
Posted lovoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot微服务中引入Seata相关的知识,希望对你有一定的参考价值。
参考:http://seata.io/zh-cn/docs/overview/what-is-seata.html
Seata 是什么?
Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
处理流程图
具体实现
1、微服务图:
2、每个微服务数据库都必须创建undo_log表
CREATE TABLE`undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
`ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
3、安装事务协调器seata-server
下载地址:https://download.csdn.net/download/lovoo/20258859?spm=1001.2014.3001.5501
异常处理参考:https://blog.csdn.net/lovoo/article/details/118713371?spm=1001.2014.3001.5501
4、配置seata-server
4.1,解压,
4.2,修改registry.conf文件,将type改成nacos
4.3,以管理员身份打开bin/seata-server.bat
4.4,打开nacos中心,可以看到seata-server注册成功
5、Springboot项目中引入seata
5.1,pom.xml引入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
5.2,在方法体上加上
@GlobalTransactional
@Transactional
@Override
public SubmitOrderResponseVo submitOrder(OrderSubmitVo orderSubmitVo) {
}
5.3,对数据源进行seata包装
@Configuration
public class MySeataConfig {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties){
DruidDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
return new DataSourceProxy(dataSource);
}
}
5.4,每个配置文件中都添加seata-server/conf目录下的file.conf和registry.conf文件
5.5,在application.yml中添加
mall-order为微服务名称 即
spring:
application:
name: mall-order
cloud:
alibaba:
seata:
tx-service-group: mall-order-fescar-service-group
QQ群:722865146
分布式商城下载:https://gitee.com/charlinchenlin/wysmall
以上是关于Springboot微服务中引入Seata的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud Alibaba | 微服务分布式事务之Seata