分布式系统的多数据库,实现分布式事务回滚(1.7.0 seata整合2.0.4nacos)

正文

1、解决的应用场景是分布式事务,每个服务有独立的数据库。

2、例如:A服务的数据库是A1,B服务的数据库是B2,A服务通过feign接口调用B服务,B涉及提交数据到B2,业务是在B提交数据之后,在A服务内报错。

所以,希望B能回滚事务。这就是跨库的数据回滚

下载

seata下载地址

seata的配置

1、创建一个数据库,把seata的表生成出来


2、修改seata配置文件

注意有其他的需求可以参考example文件,我这里直接展示本地修改后的

复制代码
#  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: ${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:
  #data-source-proxy-mode: XA
  #tx-service-group: default
  #service:
    #vgroup-mapping: # 事务组与cluster的映射关系
    #  default_tx_group: DEFAULT
    #grouplist:
    #  DEFAULT: 127.0.0.1:8091
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group : "WEIMEIZI_GROUP"
      namespace: 
      username: "nacos"
      password: "nacos"
      #data-id: seataServer.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: "WEIMEIZI_GROUP"
      namespace:
      cluster: default
      username: "nacos"
      password: "nacos"
  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: 10
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 1000
      max-wait: 5000
#  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、修改nacos配置文件

新增:

复制代码
seata:
  data-source-proxy-mode: XA
  tx-service-group: default
  service.vgroupMapping.default: default
  service.default.grouplist: 127.0.0.1:8091

注意如果你用的是其他版本的seata,可能service.vgroupMapping.default和 service.default.grouplist在其他版本命名不一样,报错就修改

4、涉及到分布式事务的数据库都要创建undo_log

这张表在seata官方包里是没有的,如果不创建,代码会报错,找不到表

复制代码
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log`  (
  `branch_id` bigint(20) NOT NULL COMMENT '分支事务ID',
  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '全局事务ID',
  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '上下文',
  `rollback_info` longblob NOT NULL COMMENT '回滚信息',
  `log_status` int(11) NOT NULL COMMENT '状态,0正常,1全局已完成',
  `log_created` datetime(6) NOT NULL COMMENT '创建时间',
  `log_modified` datetime(6) NOT NULL COMMENT '修改时间',
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'AT transaction mode undo table' ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

服务注册

满足1、2、3步后,启动

就会发现nacos注册上了seata

项目的代码

主要是两个注解:

@EnableAutoDataSourceProxy

@GlobalTransactional(rollbackFor = Exception.class)

A服务的方法:

B服务的方法:

B服务的方法正常执行并提交数据到B库,但是A服务报错了,所以B服务得回滚数据

A、B的Application都要加上@EnableAutoDataSourceProxy

注意 :引入的pom文件:

两个服务的pom都要引入seata,注意:有个包叫做seata-all,这个包不要一起引入进去,否则会报:数据源错误

复制代码
<!-- Seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.7.0</version>
        </dependency>

结语

事务回滚成功是根据XID,以下是成功回滚的截图

A服务的控制台输出:

B服务的控制台输出:

备注

如果因为nacos的配置文件过大,导致netty报错,那么,就修改netty的内存,文件是application.yml

复制代码
#服务器端口
server:
  port: 8200
  netty:
    max-chunk-size: 1545270062
    max-initial-line-length: 1545270062
    h2c-max-content-length: 1545270062
相关推荐
小鸡脚来咯44 分钟前
RabbitMQ入门
分布式·rabbitmq
qq_463944862 小时前
【Spark征服之路-2.2-安装部署Spark(二)】
大数据·分布式·spark
敖云岚3 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
LUCIAZZZ3 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding3 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
正在努力Coding3 小时前
kafka(windows)
分布式·kafka
小Tomkk4 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台4 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go5 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局5 小时前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql