基于Seata的分布式事务方案

在Seata中,有4种分布式事务实现方案

XA、AT、TCC、Saga

其中XA利用了数据库的分布式事务特性,AT相当于框架去控制事务回滚。TCC手写三个方法,saga手写两个方法。

AT的性能和编写比较折中,是最常用的一种。TCC一些视频教程中介绍了单表单字段加中间表的方式存储过程数据,对于一次操作多个主子表数据的示例目前实现还是太繁琐了。

根据官方实例和相关视频简单写了下代码。

对于配置,能用默认的用默认,有示例配置的优先抄示例配置

1.加依赖

复制代码
        <!-- 版本对应关系
       Alibaba 2021.0.5 对应 Seata 1.6.1
       https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
       这里依赖不是 spring-cloud-alibaba-seata
       -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2021.0.5.0</version>
        </dependency>

这里alibaba版本是2021.0.5所以spring-cloud-starter-alibaba-seata也用这个版本,注意版本对应

2.写配置

复制代码
seata:
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      cluster: default

  #事务组
  tx-service-group:  default_tx_group
  service:
    #事务组和集群名称映射
    vgroup-mapping:
      default_tx_group: default
  data-source-proxy-mode: AT

注意,这里需要先配置好nacos和seata服务端,seata服务端早期是json配置的,新版的是yaml配置

seata服务端配置application.yml

复制代码
#  Copyright 1999-2019 Seata.io Group.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${log.home:${user.home}/logs/seata}
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata
seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: file
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
      cluster: default
      username:
      password:
      context-path:
  store:
    # support: file 、 db 、 redis
    mode: file
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login

3.写代码

将主调用的方法注解@Transactional改为@GlobalTransactional,其他微服务方法无需更改。

完整代码见顶部

相关推荐
一个尚在学习的计算机小白17 分钟前
RabbitMQ
分布式·rabbitmq
奥尔特星云大使42 分钟前
MySQL分布式架构:MyCat详解
数据库·分布式·mysql·mycat·高可用
安当加密4 小时前
智能合约在分布式密钥管理系统中的应用
分布式·智能合约
失散134 小时前
分布式专题——41 RocketMQ集群高级特性
java·分布式·架构·rocketmq
失散134 小时前
分布式专题——42 MQ常见问题梳理
java·分布式·架构
安当加密4 小时前
基于区块链的分布式密钥管理系统:构建去中心化、高可信的密码基础设施
分布式·去中心化·区块链
亿牛云爬虫专家4 小时前
优化分布式采集的数据同步:一致性、去重与冲突解决的那些坑与招
分布式·爬虫·数据采集·爬虫代理·代理ip·数据同步·房地产
心随雨下7 小时前
Redis中Geospatial 实际应用指南
数据库·redis·分布式·缓存
想你依然心痛9 小时前
Spark大数据分析与实战笔记(第六章 Kafka分布式发布订阅消息系统-01)
笔记·分布式·spark
故事很腻i9 小时前
安装kafka-2.5.1
分布式·kafka