NullMoneyException
java.life.NullMoneyException

1.启动seata服务

1.1下载seata包

https://github.com/seata/seata/releases/tag/v1.4.2

1.2 修改配置文件

修改conf文件夹下 registry.conf文件

 

 

 1.3 创建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_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- 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 = utf8;

-- 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),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;
MySQL

1.4 导入nacos配置

通过脚本上传配置到Nacos
sh ${SEATAPATH}/script/config-center/nacos/nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t ce6c4cdf-c930-4338-bac8-ee47d939de53 -u nacos -w nacos

-h  nacos地址
-p  nacos端口
-g  分组
-t  命名空间ID(注意是ID,不是名称)
-u  用户名
-w  密码

sh命令在windows CMD命令行无法执行,可以右键使用 Git Bash命令行

 在nacos修改数据库相关配置

 

 

 

 1.5 启动服务

进入bin目录,执行

seata-server.bat -p 8096   //-p指定端口,默认端口是8091

 出现以下标志,即为启动成功

 

 

 

 2. 在springboot中配置seata

2.1引入seata依赖

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
   <exclusions>
      <exclusion>
         <groupId>io.seata</groupId>
         <artifactId>seata-spring-boot-starter</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/io.seata/seata-spring-boot-starter -->
<dependency>
   <groupId>io.seata</groupId>
   <artifactId>seata-spring-boot-starter</artifactId>
   <version>1.4.2</version>
</dependency>

2.2 在application.yml中配置seata的参数

seata:
  enabled: true
  application-id: workflow-service    #服务名
  tx-service-group: my_test_tx_group   #事务组
  enable-auto-data-source-proxy: true
  use-jdk-proxy: false
  registry:
    type: nacos
    nacos:   #nacos的地址和命名空间要和seata服务中的配置一致
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace: ce6c4cdf-c930-4338-bac8-ee47d939de53

2.3 在客户端数据库新增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       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) 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';

2.4 TM,RM 注册到TC成功

 

 

 

2.5 启用全局事务

import io.seata.spring.annotation.GlobalTransactional;

@GlobalTransactional
public String testSeata(String name) {
        //调用其他服务,修改数据库
        String str = providerFeignClient.index();
        //传入指定参数,异常回滚
        if(name.equals("shijt")) {   
            System.out.println(1 / 0);
        }
        return str;
}

观测数据库状态,目标表,undo_log表

 

posted on 2022-08-26 16:27  NullMoneyException  阅读(420)  评论(0编辑  收藏  举报