seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决

在项目中,常用的@Transactional事务注解属于本地事务,在分布式系统中,只能控制住自己服务的回滚,控制不了其它服务的回滚。

聊聊本地分布式事务

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第1张图片

一个请求链路中包含多个服务,某一个服务出现了异常,所有服务对数据库的操作都必须回滚,这样才不会出现什么问题。

 Spring Cloud Alibaba Seata 分布式事务

Seata控制分布式事务

1、每一个微服务对应的数据库必须先创建undo_log表

-- 注意此处0.3.0+ 增加唯一索引 ux_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;

2、安装事务协调器:seata-server:Releases · seata/seata · GitHub

3、Spring Boot整合Spring Cloud Alibaba seata

首先导入maven依赖


    com.alibaba.cloud
    spring-cloud-starter-alibaba-seata

然后查看导入的seata依赖是什么版本

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第2张图片

去 Releases · seata/seata · GitHub 下载对应版本

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第3张图片

下载完成后解压

 seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第4张图片

 registry.conf:注册中心相关配置

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 指定注册中心
  type = "nacos"

  nacos {
    serverAddr = "localhost:8848"
    namespace = "public"
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:1001/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3  指定seata的配置都在哪里
  type = "file"

  nacos {
    serverAddr = "localhost"
    namespace = "public"
    cluster = "default"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

 file.conf :事务日志存储位置

transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
service {
  #vgroup->rgroup
  vgroup_mapping.my_test_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
}

## transaction log store 事务日志存储在哪里
store {
  ## store mode: file、db
  mode = "file"

  ## file store
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "123456"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
lock {
  ## the lock store mode: local、remote
  mode = "remote"

  local {
    ## store locks in user's database
  }

  remote {
    ## store locks in the seata's server
  }
}
recovery {
  committing-retry-delay = 30
  asyn-committing-retry-delay = 30
  rollbacking-retry-delay = 30
  timeout-retry-delay = 30
}

transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
}

## metrics settings
metrics {
  enabled = false
  registry-type = "compact"
  # multi exporters use comma divided
  exporter-list = "prometheus"
  exporter-prometheus-port = 9898
}

运行seata

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第5张图片

 seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第6张图片

nacos上seata服务上线,表示启动成功 

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第7张图片

可以开始撸分布式事务代码了,但是撸代码之前还要配置一些东西

所有想要用到分布式事务的微服务都要使用seata DataSourceProxy代理自己的数据源

将MySeataConfig.class放在各个服务中

@Configuration
public class MySeataConfig {

    @Autowired
    DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties){

        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if(StringUtils.hasText(dataSourceProperties.getName())){
            dataSource.setPoolName(dataSourceProperties.getName());
        }
        return new DataSourceProxy(dataSource);
    }
}

将file.conf和registry.conf复制到各个服务的resources下

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第8张图片

file.conf

transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
service {
  #vgroup->rgroup
  vgroup_mapping.gulimall-order-fescar-service-group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
}

## transaction log store 事务日志存储在哪里
store {
  ## store mode: file、db
  mode = "file"

  ## file store
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "123456"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
lock {
  ## the lock store mode: local、remote
  mode = "remote"

  local {
    ## store locks in user's database
  }

  remote {
    ## store locks in the seata's server
  }
}
recovery {
  committing-retry-delay = 30
  asyn-committing-retry-delay = 30
  rollbacking-retry-delay = 30
  timeout-retry-delay = 30
}

transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
}

## metrics settings
metrics {
  enabled = false
  registry-type = "compact"
  # multi exporters use comma divided
  exporter-list = "prometheus"
  exporter-prometheus-port = 9898
}

registry.conf

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 指定注册中心
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "localhost:8848"
    namespace = "public"
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:1001/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3  指定seata的配置都在哪里
  type = "file"

  nacos {
    serverAddr = "localhost"
    namespace = "public"
    cluster = "default"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

file.conf里需要更改一个配置

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第9张图片

 seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第10张图片

启动类要加上@EnableAspectJAutoProxy(exposeProxy = true)

@EnableAspectJAutoProxy(exposeProxy = true)
@MapperScan("com.atguigu.gulimall.order.dao")
@EnableRabbit
@SpringBootApplication
@EnableRedisHttpSession
@EnableFeignClients(basePackages = "com.atguigu.gulimall.order.feign")
@EnableDiscoveryClient
public class GulimallOrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallOrderApplication.class, args);
    }

    @Bean
    public Logger.Level logger(){
        return Logger.Level.FULL;
    }

}

开始撸代码了,给分布式大事务入口处标注@GlobalTransactional全局事务注解

order服务的下单接口submitOrder 标上@GlobalTransactional全局事务注解

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第11张图片

 每一个远程的小事务用 @Transactional即可

仓库服务的锁定库存接口orderLockStock使用@Transactional事务注解seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第12张图片

 手动制造错误抛出异常

 

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第13张图片

总结

        业务场景是订单服务去库存服务中锁定对应订单的商品库存,库存锁定成功了,但是order服务报错了,这时候是需要库存服务和order服务都要回滚的,经过测试确实回滚成功了,seata真牛!

bug总结

在整合当中出现了几个bug,解决花了很多时间,很痛苦,在这里记录一下

问题一:

seata 0.7.1 不能使用MyBatis-Plus的saveBatch只能遍历一个List一个一个save到表中,这应该知识 0.7.1的Bug,后续高版本应该解决了

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第14张图片

问题二:

报错信息:io.seata.common.exception.FrameworkException: can not connect to services-server.

registry.conf文件中,如果使用nacos作为注册中心,那么要将application的名称也加上去,seata默认的名称是seata-server,填写seata-server即可。不填会报错服务注册不到seata中

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第15张图片 seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第16张图片

 

 

问题三:

seata版本对应问题,如下图

seata分布式事务讲解,SpringBoot项目整合seata全流程和常见错误解决_第17张图片

 

 

 

你可能感兴趣的:(Spring,boot,java,分布式,java,数据库,spring,boot,spring,cloud)