Seata搭建与分布式事务入门

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Seata搭建与分布式事务入门

在单体架构下,我们大多使用的是单体数据库,通过数据库的ACID特性支持,实现了本地事务。但是在微服务架构下复杂的业务关系中,分布式事务是不可避免的问题之一。Seata是Spring Cloud Alibaba分布式事务解决方案中间件,解决了微服务场景下面临的分布式事务问题。本文介绍如何通过搭建Seata环境,并通过其AT模式,实现分布式事务。

本文中使用的环境版本:

image.png

1、Seata服务端z下载

下载seata-server-1.4.0:

https://github.com/seata/seata/releases/tag/v1.4.0

同时下载seata-server-0.0.9,后续需要其中的配置文件和脚本:

https://github.com/seata/seata/releases/tag/v0.9.0

2、Seata服务file.conf,registry.conf配置

在conf目录下,先把配置文件备份后再进行更改

修改file.conf,mode选择数据库模式,并配置数据库连接信息

 ## store mode: file、db、redis
  mode = "db"
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "xxxxx"
    password = "xxxxxx"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

修改registry.conf,使用nacos作为注册和配置中心。可以在nacos中创建一个命名空间,把生成的命名空间的值拷过来

 registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
    nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "202274f4-218e-42bf-9251-e996df6340f8"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "202274f4-218e-42bf-9251-e996df6340f8"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }

3、导入Seate参数配置到nacos配置中心

首先,把seata-server-0.9的 nacos-config.txt 和nacos-config.sh脚本拷贝到1.4版本的 seata/conf下。我们需要把nacos-config.txt的参数通过脚本nacos-config.sh导入到nacos配置中心,之后微服务项目也从nacos配置中心读取配置。这样就不用像老版本那样,需要把两个配置文件拷到微服务项目的resourcce目录下了。

修改nacos-config.txt,首先修改数据库配置:

store.mode=db
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=hydra
store.db.password=123456

0.9中的参数格式还是使用横线模式,在1.4中规范有所变动,需要把横线变成驼峰,启动需要改动的参数有:

store.db.db-type=mysql
store.db.driver-class-name=com.mysql.jdbc.Driver

需要改成驼峰:

store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver

并且,如果使用的是mysql8.0以上的版本,需要改一下驱动的名称。

其他参数的具体意义可以查看官方文档:https://seata.io/zh-cn/docs/user/configurations.html ,并按照上面的规则进行修改。额外需要注意参数的参数是:service.vgroupMapping

service.vgroupMapping.my_test_tx_group=default

官方解释为事务群组,具体使用多少个事务群体没有明确指出。但通过查看文档和部分开源项目发现,大多都采用将key值设置为服务端的服务名,有多少个微服务就添加多少行。在接下来的demo中要使用两个微服务作为示例,因此添加:

service.vgroupMapping.order-service-group=default
service.vgroupMapping.stock-service-group=default

使用gitbash运行nacos-config.sh脚本,参数是nacos的ip:

sh nacos-config.sh 127.0.0.1

这样执行完成后参数默认是存在nacos config的public命名空间下,可以在nacos创建一个seata的命名空间,把所有参数拷贝过去,方便进行区分。

4、建表:

在seata数据库中新建表branch_table, global_table, lock_table,在业务数据库中新建表undo_log,用于回滚,这些脚本也是seata-server-0.9中也可以直接找到。

-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `global_table` (
  `xid` varchar(128)  not null,
  `transaction_id` bigint,
  `status` tinyint not null,
  `application_id` varchar(32),
  `transaction_service_group` varchar(32),
  `transaction_name` varchar(128),
  `timeout` int,
  `begin_time` bigint,
  `application_data` varchar(2000),
  `gmt_create` datetime,
  `gmt_modified` datetime,
  primary key (`xid`),
  key `idx_gmt_modified_status` (`gmt_modified`, `status`),
  key `idx_transaction_id` (`transaction_id`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
  `branch_id` bigint not null,
  `xid` varchar(128) not null,
  `transaction_id` bigint ,
  `resource_group_id` varchar(32),
  `resource_id` varchar(256) ,
  `lock_key` varchar(128) ,
  `branch_type` varchar(8) ,
  `status` tinyint,
  `client_id` varchar(64),
  `application_data` varchar(2000),
  `gmt_create` datetime,
  `gmt_modified` datetime,
  primary key (`branch_id`),
  key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
  `row_key` varchar(128) not null,
  `xid` varchar(96),
  `transaction_id` long ,
  `branch_id` long,
  `resource_id` varchar(256) ,
  `table_name` varchar(32) ,
  `pk` varchar(36) ,
  `gmt_create` datetime ,
  `gmt_modified` datetime,
  primary key(`row_key`)
);
-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
drop table `undo_log`;
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) 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;

5、启动

在启动nacos-server的情况下,点击 seata/bin/seata-server.bat启动seata。

6、微服务改造

在微服务中引入seata的依赖:

<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>

如果微服务中使用的是druid连接池,可以把已有的druid依赖删除,在seata-spring-boot-starter-1.3.0中已经引入了druid-1.1.12。

修改每一个微服务yml,配置nacos和seata,tx-service-group就是nacos-config.txt 中service.vgroupMapping的key,我们这里使用微服务的名称加上group后缀

server:
  port: 8763
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
  datasource:
    url: jdbc:mysql://localhost:3306/tenant?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: hydra
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 8
      min-idle: 1
      max-active: 10
      max-wait: 60000
seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: ${spring.application.name}-group
  enable-auto-data-source-proxy: true
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: 202274f4-218e-42bf-9251-e996df6340f8
      group: SEATA_GROUP
      username: nacos
      password: nacos
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace: 202274f4-218e-42bf-9251-e996df6340f8
      group: SEATA_GROUP
      username: nacos
      password: nacos

配置seata的数据库代理,在使用mybatis-plus时的配置方式如下:

@Configuration
public class DataSourceProxyConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        // 订单服务中引入了mybatis-plus,所以要使用特殊的SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        // 代理数据源
        sqlSessionFactoryBean.setDataSource(new DataSourceProxy(dataSource));
        // 生成SqlSessionFactory
        return sqlSessionFactoryBean.getObject();
    }
}

调用测试,以OrderService调用StockService为例,在被调用的service方法上加上@Transactional注解

StockService提供接口:

@Service
public class StockService {
    @Autowired
    private StockMapper stockMapper;
    @Transactional
    public String reduce(){
        System.out.println("减库存");
        Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<Stock>().eq(Stock::getId, 1));
        System.out.println(stock.toString());
        stock.setQuantity(stock.getQuantity()-1);
        int result = stockMapper.updateById(stock);
        if (result==1){
            throw new RuntimeException("异常测试,准备rollBack");
        }
        return "stock reduce success";
    }
}

OrderService调用StockService的服务:

使用了FeignClient调用StockService,并在发起事务的方法上加上@GlobalTransactional注解:

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StockClient stockClient;
    @GlobalTransactional
    public String buy(){
        Order order=new Order();
        order.setId(1L).setMoney(20D);
        int result = orderMapper.insert(order);
        if (result==1){
            System.out.println("插入订单成功");
        }
        return stockClient.reduce();
    }
}

调用OrderService的接口,会在StockService中抛出异常,reduce方法中的本地事务先执行回滚。再查看日志,在order表上执行了回滚操作:

image.png

在上面的日志中,打印出了全局事务的xid、分支的branchId、以及seata使用的模式,在使用AT模式的二阶段提交完成后,显示回滚状态为回滚完成。查看业务数据库的und_log表,已经插入了回滚记录

image.png

这样,就以Seata中默认的AT模式实现了分布式事务。在该模式下,可以应对大多数的业务场景,并且基本可以做到无业务入侵,对于程序员来说,只需要添加注解,不需要做其他的业务功能改造,就可以以无感知的方式就可以实现分布式事务的解决。

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
Java 数据库
在Java中使用Seata框架实现分布式事务的详细步骤
通过以上步骤,利用 Seata 框架可以实现较为简单的分布式事务处理。在实际应用中,还需要根据具体业务需求进行更详细的配置和处理。同时,要注意处理各种异常情况,以确保分布式事务的正确执行。
|
3月前
|
存储 Java 关系型数据库
在Spring Boot中整合Seata框架实现分布式事务
可以在 Spring Boot 中成功整合 Seata 框架,实现分布式事务的管理和处理。在实际应用中,还需要根据具体的业务需求和技术架构进行进一步的优化和调整。同时,要注意处理各种可能出现的问题,以保障分布式事务的顺利执行。
197 53
|
29天前
|
Java 关系型数据库 数据库
微服务SpringCloud分布式事务之Seata
SpringCloud+SpringCloudAlibaba的Seata实现分布式事务,步骤超详细,附带视频教程
50 1
|
2月前
|
消息中间件 SQL 中间件
大厂都在用的分布式事务方案,Seata+RocketMQ带你打破10万QPS瓶颈
分布式事务涉及跨多个数据库或服务的操作,确保数据一致性。本地事务通过数据库直接支持ACID特性,而分布式事务则需解决跨服务协调难、高并发压力及性能与一致性权衡等问题。常见的解决方案包括两阶段提交(2PC)、Seata提供的AT和TCC模式、以及基于消息队列的最终一致性方案。这些方法各有优劣,适用于不同业务场景,选择合适的方案需综合考虑业务需求、系统规模和技术团队能力。
281 7
|
3月前
|
数据库
如何在Seata框架中配置分布式事务的隔离级别?
总的来说,配置分布式事务的隔离级别是实现分布式事务管理的重要环节之一,需要认真对待和仔细调整,以满足业务的需求和性能要求。你还可以进一步深入研究和实践 Seata 框架的配置和使用,以更好地应对各种分布式事务场景的挑战。
140 63
|
3月前
|
消息中间件 运维 数据库
Seata框架和其他分布式事务框架有什么区别
Seata框架和其他分布式事务框架有什么区别
47 1
|
4月前
|
消息中间件 关系型数据库 Java
‘分布式事务‘ 圣经:从入门到精通,架构师尼恩最新、最全详解 (50+图文4万字全面总结 )
本文 是 基于尼恩之前写的一篇 分布式事务的文章 升级而来 , 尼恩之前写的 分布式事务的文章, 在全网阅读量 100万次以上 , 被很多培训机构 作为 顶级教程。 此文修改了 老版本的 一个大bug , 大家不要再看老版本啦。
|
5月前
|
SQL NoSQL 数据库
SpringCloud基础6——分布式事务,Seata
分布式事务、ACID原则、CAP定理、Seata、Seata的四种分布式方案:XA、AT、TCC、SAGA模式
SpringCloud基础6——分布式事务,Seata
|
5月前
|
Dubbo Java 应用服务中间件
分布式-dubbo的入门
分布式-dubbo的入门
|
9月前
|
存储 关系型数据库 MySQL
基于Seata实现分布式事务
通过以上步骤,你可以使用 Seata 实现分布式事务,确保在微服务架构中的事务一致性。Seata 支持多种语言和框架,能够满足不同业务场景的需求。欢迎关注威哥爱编程,一起学习成长。
213 1