springCloud整合Seata实现分布式事物

已经很久没有进行技术点总结了,今天有时间整理分享分布式事物相关内容一下。。。。

本文以SpringCloud Alibaba + Nacos微服务框架作为项目演示:

参考博客:https://blog.csdn.net/thinkingcao/category_9716788.html

一、分布式事务解决方案

https://blog.csdn.net/Thinkingcao/article/details/109320174

二、seata简介

https://blog.csdn.net/Thinkingcao/article/details/109323232

三、springcloud集成Seata

https://thinkingcao.blog.csdn.net/article/details/109320221

1、Seata环境搭建

主要步骤如下:

a.下载seata-server-1.4.0和seata-1.4.0源码

https://github.com/seata/seata/releases

b.创建undo_log日志表

进入seata-1.4.0源码:

在项目的参与全局事务的数据库中加入undo_log这张表。undo_log表脚本根据自身数据库类型来选择。

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(100) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

c.创建seata事务相关表

进入seata-1.4.0源码:

目前支持mysqloraclepostgresql这三种数据库,上述三种脚本是针对SeataSever端在协调处理分布式事务时所需要的3张表,提供了不同数据库的global_table表、branch_table表、lock_table表创建脚本,根据自身数据库执行对应的sql脚本执行即可。

mysql数据库中:创建名为seata的库,并执行mysql.sql脚本,将会生成三张表。

url=jdbc:mysql://127.0.0.1:3306/seata

d.修改seata-server的registry.conf、file.conf配置文件

进入seata-server-1.4.0中:

配置registry.conf:注册中心为nacos,配置nacos相关属性参数。

配置file.config:设置DB模式相关参数。

e.配置seata相关参数,提交至nacos配置中心

config,txt内容如下

service.vgroupMapping.my_test_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
使用nacos-config.sh脚本将config.txt配置提交到nacos配置中心去


# 导入语法,t:命名空间,g:分组
sh nacos-config.sh -h 127.0.0.1 -p 8848 -t 5e22d16b-da6c-4f3f-8f3a-41cb501f18e5 -g SEATA_GROUP

使用Git-Bash运行:sh nacos-config.sh

查看ncaos配置中心;

seata环境配置完成。。。。

2、项目工程 整合seata

a.引入pom依赖

springCloud项目:

<!-- 分布式事务seata包 -->
<!--seata begin-->
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
   <version>2.1.3.RELEASE</version>
   <exclusions>
     <exclusion>
        <groupId>io.seata</groupId>
        <artifactId>seata-spring-boot-starter</artifactId>
     </exclusion>   
   </exclusions>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>
<!--seata end-->
 

dubbo项目:

<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>


b.项目中配置seata参数

#seata分布式事务配置(AT模式)
seata.enabled=true
seata.application-id=${spring.application.name}
#客户端和服务端在同一个事务组
seata.tx-service-group=my_test_tx_group
seata.enable-auto-data-source-proxy=true
seata.service.vgroup-mapping.my_test_tx_group=default
seata.config.type=nacos
#public是命名是命名空间名称,导致疯狂拉去配置,需删除namespace=public
#seata.config.nacos.namespace=public
seata.config.nacos.serverAddr=127.0.0.1:8848
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.username=nacos
seata.config.nacos.password=nacos
seata.registry.type=nacos
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=127.0.0.1:8848
seata.registry.nacos.group=SEATA_GROUP
seata.registry.nacos.namespace=public
seata.registry.nacos.username=nacos
seata.registry.nacos.password=nacos
seata.registry.nacos.cluster=default

c.启动服务

运行seata-server.bat脚本,启动seata-server服务,注册提供者、消费者服务,在全局事务调用者使用@GlobalTransactional实现分布式事物

分布式事务注解:@GlobalTransactional

ps:注意点

1.注意在使用seata,启动服务时会出现以下错误

不要慌,有解决方案。。。

原因:由于在seata服务中的自动配置类使用了代理数据源的类,而且交给spring管理,但是在spring容器中之前已经有了一个数据源类,而且两者的名字一样,所以,我们需要在application.properties中允许替换原本就在spring容器中的数据源类,使用我们seata服务中的代理数据源类

添加配置:

spring.main.allow-bean-definition-overriding=true

2.启动seata-server服务出现如下错误:

原因:数据库驱动使用不正确,本地使用了mysql8驱动,需要更换配置

以上涉及到db相关配置更换以下内容:

    driverClassName = "com.mysql.cj.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值