保姆级教程:Spring Cloud 集成 Seata 分布式事务

点击上方“芋道源码”,选择“设为星标

管她前浪,还是后浪?

能浪的浪,才是好浪!

每天 10:33 更新文章,每天掉亿点点头发...

源码精品专栏

 

来源:wangbinguang.blog.csdn.net/

article/details/128935863


环境搭建

Nacos搭建

最新版本快速搭建 使用Mysql模式

Nacos直接启动即可。控制台默认账号密码是nacos/nacos,Mysql账户密码有两个 root/rootnacos/nacos

654b26449dadfcb3148e8aeb7ff93eef.png
Seata搭建

Seata版本1.5.0 快速搭建

Seata1.5.0版本直接是一个SpringBoot项目,下载后修改application.yml 文件中注册中心、配置中心、存储模式配置。参考resources/application.example.yml 文件 ,修改后如下

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${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
      namespace:
      group: SEATA_GROUP
      cluster: default
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
  store:
    # support: file 、 db 、 redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
      user: root
      password: root
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000

> 基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
>
> * 项目地址:<https://github.com/YunaiV/ruoyi-vue-pro>
> * 视频教程:<https://doc.iocoder.cn/video/>

#  server:

> 基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
>
> * 项目地址:<https://github.com/YunaiV/yudao-cloud>
> * 视频教程:<https://doc.iocoder.cn/video/>

#    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,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

创建seata数据库,执行脚本建表

-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `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_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

启动seata-server,控制台登录页面如下,账号密码为seata/seata

5516dc534d9c21a0b0b625422ae2b846.png

项目搭建

业务背景

用户购买商品的业务逻辑。整个业务逻辑由3个微服务提供支持:

  • 仓储服务: 对给定的商品扣除仓储数量。

  • 订单服务: 根据采购需求创建订单。

  • 帐户服务: 从用户帐户中扣除余额。

架构
ef540420e851061d7ba8571336acb5b8.png
业务表创建
-- ----------------------------
-- Table structure for t_account
-- ----------------------------
DROP TABLE IF EXISTS `t_account`;
CREATE TABLE `t_account`
(
    `id`      int(11) NOT NULL AUTO_INCREMENT,
    `user_id` varchar(255) DEFAULT NULL,
    `amount`  double(14, 2
) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_account
-- ----------------------------
INSERT INTO `t_account`
VALUES ('1', '1', '4000.00');

-- ----------------------------
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order`
(
    `id`             int(11) NOT NULL AUTO_INCREMENT,
    `order_no`       varchar(255) DEFAULT NULL,
    `user_id`        varchar(255) DEFAULT NULL,
    `commodity_code` varchar(255) DEFAULT NULL,
    `count`          int(11) DEFAULT '0',
    `amount`         double(14, 2
) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_order
-- ----------------------------

-- ----------------------------
-- Table structure for t_stock
-- ----------------------------
DROP TABLE IF EXISTS `t_stock`;
CREATE TABLE `t_stock`
(
    `id`             int(11) NOT NULL AUTO_INCREMENT,
    `commodity_code` varchar(255) DEFAULT NULL,
    `name`           varchar(255) DEFAULT NULL,
    `count`          int(11) DEFAULT '0',
    PRIMARY KEY (`id`),
    UNIQUE KEY `commodity_code` (`commodity_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_stock
-- ----------------------------
INSERT INTO `t_stock`
VALUES ('1', 'C201901140001', '水杯', '1000');

-- ----------------------------
-- Table structure for undo_log
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
-- ----------------------------
DROP TABLE IF EXISTS `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,
    PRIMARY KEY (`id`),
    UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of undo_log
-- ----------------------------
SET
FOREIGN_KEY_CHECKS=1;
服务创建
业务服务

以order服务为例,引入依赖、配置参数、提供创建订单接口。

pom.xml文件中引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

application.properties配置参数

server.port=81
spring.application.name=order
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata_samples?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

提供创建订单接口

@RequestMapping("/add")
 public void add(String userId, String commodityCode, Integer count, BigDecimal amount) {
     Order order = new Order();
     order.setOrderNo(UUID.randomUUID().toString());
     order.setUserId(userId);
     order.setAmount(amount);
     order.setCommodityCode(commodityCode);
     order.setCount(count);
     orderService.save(order);
 }
聚合服务

business服务远程调用仓储、订单、帐户服务,完成下单流程。

1.pom.xml文件中引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

2.application.properties配置参数

server.port=80
spring.application.name=business
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata_samples?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
service.disableGlobalTransaction=false
# 连接超时时间
ribbon.ConnectTimeout=3000
# 响应超时时间
ribbon.ReadTimeout=5000

3.声明account、stock、order的feign接口。

@FeignClient(value = "account")
public interface AccountFeign {
    @RequestMapping("/account/reduce")
    public void reduce(@RequestParam("userId") String userId, @RequestParam("amount") BigDecimal amount);

}
@FeignClient(value = "order")
public interface OrderFeign {
    @RequestMapping("/order/add")
    public void add(@RequestParam("userId") String userId, @RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count, @RequestParam("amount") BigDecimal amount);

}
@FeignClient(value = "stock")
public interface StockFeign {
    @RequestMapping("/stock/deduct")
    public void deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);

}

4.全局事务开启,调用feign接口

@Autowired
private OrderFeign orderFeign;

@Autowired
private StockFeign stockFeign;

@Autowired
private AccountFeign accountFeign;

@GlobalTransactional
@RequestMapping("/toOrder")
public void toOrder(String userId, String commodityCode, Integer count, BigDecimal amount) {
    accountFeign.reduce(userId, amount);
    stockFeign.deduct(commodityCode, count);
    orderFeign.add(userId, commodityCode, count, amount);
}

测试验证

当前资金账户4000,库存1000,模拟用户购买商品2000个,消费4000,业务调用后,数据库数据状态应该如下:

  • 用户资金满足4000,数据库更新用户资金为0。

  • 商品库存不满足2000个,异常。

  • business服务提交全局回滚。

  • 资金服务回滚操作,更新资金为4000。

验证:

1.浏览器输入地址请求访问

  • http://127.0.0.1/business/toOrder?userId=1&commodityCode=C201901140001&count=2000&amount=4000

2.account、stock服务日志观察。

1794278a095beb223cf59df96aeab9db.png 6dfd25db409ed678410ae43380f1ba76.png

3.数据库数据依然为原始状态。

97e62422c83bb0d6ec67699acca3117d.png

注意事项

1.Seata1.5版本的mysql驱动是5.7,需要为8,在libs文件夹删除mysql-connector-java-5.xx.jar,替换mysql-connector-java-8.xx.jar即可

2.Spring Boot &Spring Cloud&Spring Cloud Alibaba版本兼容问题

Spring Cloud Alibaba版本说明

ec734f5c3085eba6b558d9ed2d715162.png

3.druid和数据驱动版本兼容

通过druid仓库版本查看各依赖版本说明

43e927d37e88a0a2f1b7ce5c91cc86fe.png
代码仓库
  • https://gitee.com/codeWBG/springcloud_alibaba



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢

12e4b014d4e160702c4fe50551f1efe2.png

已在知识星球更新源码解析如下:

8653f09916b42bd91494e4246b9e7b55.jpeg

f3b26b55087e7b0ff4060861b528e4b3.jpeg

14607d120b3c6fd52bfc6bdf9b438584.jpeg

8a8dda04fdab34714662866c55d5f0a3.jpeg

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
### 回答1: Spring Cloud集成Seata是为了实现分布式事务的解决方案。Seata是一个开源的分布式事务解决方案,它提供了高可用性、高性能、易扩展的分布式事务服务。Spring Cloud集成Seata可以帮助我们在分布式系统中实现数据的一致性和可靠性,从而提高系统的稳定性和可靠性。在集成Seata时,我们需要配置Seata的注册中心、配置文件、数据源等信息,然后在代码中使用Seata提供的API来实现分布式事务的控制。通过Spring Cloud集成Seata,我们可以轻松地实现分布式事务的管理和控制,从而提高系统的可靠性和稳定性。 ### 回答2: Spring CloudSeata集成主要包括以下几个方面。 首先,在pom.xml文件中添加Seata的依赖项,包括seata-all、seata-spring-boot-starter和seata-spring-boot-starter-data-redis。 接下来,在配置文件中配置Seata的相关属性。需要配置seata.server.ip(Seata Server的IP地址)和seata.server.port(Seata Server的端口号),并将spring.cloud.alibaba.seata.tx-service-group设置为seata的事务组名称。 然后,在主启动类上添加@EnableAutoDataSourceProxy和@EnableFeignClients注解,开启Seata的数据源代理和Feign客户端的支持。 接着,在需要进行分布式事务管理的方法上添加@GlobalTransactional注解,指定该方法需要进行全局事务管理。 最后,在操作数据库的方法上添加@Compensable注解,用于标记该方法属于可补偿的业务操作。 通过以上步骤,就完成了Spring CloudSeata集成。在分布式事务的管理上,Seata会根据@GlobalTransactional注解来开启和提交事务,并根据@Compensable注解来进行补偿操作。此外,Seata还提供了一些额外的功能,如幂等性校验、分布式锁等,可以进一步优化分布式事务的管理和性能。 总之,Spring CloudSeata集成可以有效地解决分布式事务的一致性问题,保证系统的数据一致性和可靠性。 ### 回答3: Spring Cloud是一套用于构建分布式系统的开源框架,而Seata则是一种高性能易用的分布式事务解决方案。当我们需要在Spring Cloud集成Seata时,需要进行以下步骤: 1. 创建一个Spring Cloud项目:首先需要创建一个Spring Cloud项目,可以选择使用Spring Boot来搭建。确保项目中已经引入了Spring Cloud的相关依赖。 2. 引入Seata依赖:在项目的pom.xml文件中添加Seata的依赖。可以通过Maven或Gradle来管理项目的依赖。 3. 配置Seata相关配置文件:在项目的资源文件夹下创建一个名为"seata"的文件夹,在该文件夹下创建一个名为"registry.conf"的文件。在该文件中,配置Seata的注册中心信息和事务日志存储信息等。 4. 配置Spring CloudSeata的整合:在项目的配置文件中,配置Spring CloudSeata的整合。可以配置Seata的事务管理器、数据源代理以及相关的事务拦截器等。 5. 启动Seata服务:运行Seata的服务端,确保其正常运行。可以使用Seata提供的脚本来启动服务。 6. 编写业务代码:在项目中编写需要进行分布式事务管理的业务代码。可以使用Seata提供的注解来标注事务的边界。 7. 测试分布式事务:运行项目,并测试分布式事务的功能是否正常。通过观察日志和数据库的变化来确认分布式事务的一致性和隔离性是否得到了保证。 总的来说,集成SeataSpring Cloud需要添加依赖、配置相关文件、整合Spring CloudSeata以及编写业务代码等步骤。这样,我们就可以在Spring Cloud项目中实现分布式事务的管理,提高系统的可靠性和一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值