分布式事务Seata -- Seata、Dubbo和Nacos来保证业务数据的一致性

目录

1.重点是seata server的配置与启动:

1.1 将seata-server注册到nacos -- 服务注册

1.2 nacos既能做为注册中心,又能做为配置管理中心

1.3 seata-server的事务log存储相关配置:

1.4 启动seata-server

2.seata-server的client端配置:

3.启动微服务并测试


运行seata 实例: https://github.com/seata/seata-samples/tree/master/nacos

  • nacos版本:1.4.2
  • seata-server版本:1.4.2

按照user guide的步骤,一步一步的完成Spring、Seata、Dubbo和Nacos 集成

1.重点是seata server的配置与启动:

1.1 将seata-server注册到nacos -- 服务注册

修改seata-server中的conf目录下的registry.conf文件,选择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 = ""
    cluster = "default"
    username = ""
    password = ""
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
..................................................

1.2 nacos既能做为注册中心,又能做为配置管理中心

还是修改seata-server中的conf目录下的registry.conf文件,选择nacos做为配置中心,集中管理各个微服务的配置信息。

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
    #dataId = "seataServer.properties"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
.............................................

将seata-server的配置导入到nacos

参考文章:https://blog.csdn.net/qq853632587/article/details/111644295 按照里面的步骤,将seata-server配置导入到nacos。下面是成功之后的截图

1.3 seata-server的事务log存储相关配置:

修改seata-server中的conf目录下的文件file.conf,选择使用db去存储事务log

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## 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"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

1.4 启动seata-server

seata-server.bat -h 127.0.0.1 -m db

启动成功之后,seata-server做为一个微服务,根据上面的配置,被注册到nacos。

2.seata-server的client端配置:

谁是seata-server的client? 当然是我们的应用程序,我们的应用程序中依赖这个jar包 -- seata-all

    <dependencies>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <!--  <version>${seata.version}</version> -->
            <version>1.3.0</version>
        </dependency>

 client端也是需要进行配置的,具体的需要在file.conf和registry.conf文件中进行配置

NOTE: serverAddr和namespace与Server端一致,clusterName与Server端cluster一致

3.启动微服务并测试

需要将seata的java客户端的版本换成1.3.0,不然会报错儿,具体解决问题的过程,请参考文章:https://blog.csdn.net/wdquan19851029/article/details/116751027

    <dependencies>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <!--  <version>${seata.version}</version> -->
            <version>1.3.0</version>
    </dependency>

启动完成可在 Nacos 控制台服务列表 看到启动完成的三个 provider

最后,启动 DubboBusinessTester 进行测试

1.显示抛出异常,全局事务回滚

在BusinessServiceImpl.java -> purchase(......)中,直接抛出了异常,这样就会导致全局事务回滚,证明了seata分布式事务管理的成功。

    @Override
    @GlobalTransactional(timeoutMills = 300000, name = "dubbo-demo-tx")
    public void purchase(String userId, String commodityCode, int orderCount) {
        LOGGER.info("purchase begin ... xid: " + RootContext.getXID());
        storageService.deduct(commodityCode, orderCount);
        orderService.create(userId, commodityCode, orderCount);
        throw new RuntimeException("xxx"); //抛出异常,导致seata全局事务回滚
    }

2.去掉异常,正常运行结果如下:

订单创建完成:

货物库存由100变成98:

资金账户的钱由999变成599:

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值