Seata安装与使用

Seata

1.seata

seata是阿里巴巴的一个分布式事务框架,seata的at模式主要有3个对象和一个id

TC 事务协调者
TM 事务发起者
RM 事务资源
xid 全局id
#执行步骤
1.tm向tc提出申请创建一个事务,tc会创建一个全局的xid
2.rm向tc注册,将其纳入xid所管辖的全局事务中
3.xid在微服务的链路中传播
4.tm向tc发起全局事务的提交或者回滚
5.tc控制xid下的所有的事务提交或者回滚

2.seata的安装(1.4.2)

下载安装包,然后解压

3.seata事务日志持久化

1.准备脚本(需要2种脚本)

1.seata所在库的脚本(用于控制事务)
1.mysql
-- -------------------------------- 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(96),
    `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;
2.oracle

CREATE TABLE global_table
(
    xid                       VARCHAR2(128) NOT NULL,
    transaction_id            NUMBER(19),
    status                    NUMBER(3)     NOT NULL,
    application_id            VARCHAR2(32),
    transaction_service_group VARCHAR2(32),
    transaction_name          VARCHAR2(128),
    timeout                   NUMBER(10),
    begin_time                NUMBER(19),
    application_data          VARCHAR2(2000),
    gmt_create                TIMESTAMP(0),
    gmt_modified              TIMESTAMP(0),
    PRIMARY KEY (xid)
);

CREATE INDEX idx_gmt_modified_status ON global_table (gmt_modified, status);
CREATE INDEX idx_transaction_id ON global_table (transaction_id);

-- the table to store BranchSession data
CREATE TABLE branch_table
(
    branch_id         NUMBER(19)    NOT NULL,
    xid               VARCHAR2(128) NOT NULL,
    transaction_id    NUMBER(19),
    resource_group_id VARCHAR2(32),
    resource_id       VARCHAR2(256),
    branch_type       VARCHAR2(8),
    status            NUMBER(3),
    client_id         VARCHAR2(64),
    application_data  VARCHAR2(2000),
    gmt_create        TIMESTAMP(6),
    gmt_modified      TIMESTAMP(6),
    PRIMARY KEY (branch_id)
);

CREATE INDEX idx_xid ON branch_table (xid);

-- the table to store lock data
CREATE TABLE lock_table
(
    row_key        VARCHAR2(128) NOT NULL,
    xid            VARCHAR2(96),
    transaction_id NUMBER(19),
    branch_id      NUMBER(19)    NOT NULL,
    resource_id    VARCHAR2(256),
    table_name     VARCHAR2(32),
    pk             VARCHAR2(36),
    gmt_create     TIMESTAMP(0),
    gmt_modified   TIMESTAMP(0),
    PRIMARY KEY (row_key)
);

CREATE INDEX idx_branch_id ON lock_table (branch_id);
2.业务库脚本(回滚日志表)
1.mysql
-- 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';
2.oracle
CREATE TABLE undo_log
(
    id            NUMBER(19)    NOT NULL,
    branch_id     NUMBER(19)    NOT NULL,
    xid           VARCHAR2(100) NOT NULL,
    context       VARCHAR2(128) NOT NULL,
    rollback_info BLOB          NOT NULL,
    log_status    NUMBER(10)    NOT NULL,
    log_created   TIMESTAMP(0)  NOT NULL,
    log_modified  TIMESTAMP(0)  NOT NULL,
    PRIMARY KEY (id),
    CONSTRAINT ux_undo_log UNIQUE (xid, branch_id)
);

COMMENT ON TABLE undo_log IS 'AT transaction mode undo table';

-- Generate ID using sequence and trigger
CREATE SEQUENCE UNDO_LOG_SEQ START WITH 1 INCREMENT BY 1;

2.修改seata配置文件

file.confconf目录下

1.新增配置
#1.4.2没有service这个配置,可以自己添加上
service {
	vgroupMapping.my_test_tx_group = "default" #指定分组
	default.grouplist = "127.0.0.1:8091"
	enableDegrade = false
	disable = false
	max.commit.retry.timeout = "-1"
	max.rollback.retry.timeout = "-1"
}
2.修改事务日志存储方式

注意:用的哪个数据库记得去lib/jdbclib这两个路径下放入对应的驱动(默认有mysql8和mysql)

store {
  ## store mode: file、db、redis 配置文件默认用file,这里修改为db数据库
  mode = "db" 
  ## rsa decryption public key
  publicKey = ""
  ## file store property
  ## 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 = "oracle"
    ##mysql的驱动
    driverClassName = "oracle.jdbc.OracleDriver" 
    ## 如果你用的mysql 在url后要加上rewriteBatchedStatements=true这个配置
    url = "jdbc:oracle:thin:@127.0.0.1:1521/fztygl"
    user = "username"
    password = "password"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}
3.业务代码配置

A服务中进行插入操作,然后调用B服务的插入操作

1.A服务
1.pom中添加对应依赖
<!--version 自己选-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <version>2.2.5.RELEASE</version> 
</dependency>
2.在对应的方法上加上@GlobalTransactional(rollbackFor = Exception.class)注解
3AB服务的yml配置
#这的配置和seata服务里面的配置一样,就是这改成了yml形式
seata:
  service:
    vgroup-mapping:
      my_test_tx_group: default  #这写default
    grouplist:
      default: 10.85.60.2:8091 #所以这是default 这两个值保持一致就行
    enable-degrade: false
    disable-global-transaction: false
2.B服务

步骤跟A服务一致, 但不需要第二步

4.seata配置nacos注册中心

1.修改seata配置文件

registry.confconf目录下

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos" #默认为file 修改为nacos
  nacos {
    application = "seata-server" #随便写
    serverAddr = "10.85.60.2:8848"
    group = "SEATA_GROUP" #随便写
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

2.AB服务的yml配置

#这的配置和seata服务里面的配置一样,就是这改成了yml形式
seata:
  registry:
    type: nacos
    nacos:
      application: seata-server 
      server-addr: 10.85.60.2:8848
      group: "SEATA_GROUP"
      namespace: ""
      username: "nacos"
      password: "nacos"

5.seata配置nacos配置中心

1.修改seata配置文件

registry.confconf目录下

config {
  # file 、nacos 、eureka、zk
  type = "nacos" #默认为file 修改为nacos
  nacos {
    application = "seata-server" #随便写
    serverAddr = "10.85.60.2:8848"
    group = "SEATA_GROUP" #随便写
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

2.AB服务的yml配置

seata:
#  registry: 
#    type: nacos
#    nacos:
#    application: seata-server
#    server-addr: 10.85.60.2:8848
#    group: "SEATA_GROUP"
#    namespace: ""
#    username: "nacos"
#    password: "nacos"
  config:
    type: nacos 
    nacos:
      server-addr: 10.85.60.2:8848
      group: "SEATA_GROUP"
      namespace: ""
      username: "nacos"
      password: "nacos"

3.修改seata服务上的config.txt文件

下载的seata1.4.2没有这个文件 需要从github中下载对应的配置路径,放在seata解压包内 跟bin同级即可

github.com/seata/blob/1.4.2/script/config-center/config.txt

只修改我下面写todo的地方即可,其他无需配置

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
## TODO配置自己服务事务组 跟seata的file.conf保持一致
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
## TODO配置事务日志存储 跟seata的file.conf保持一致 默认是file
store.mode=db

store.publicKey=
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100

store.db.datasource=druid
store.db.dbType=oracle
store.db.driverClassName=oracle.jdbc.OracleDriver
store.db.url=jdbc:oracle:thin:@127.0.0.1:1521/fztygl
store.db.user=username
store.db.password=password
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

##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&rewriteBatchedStatements=true 如果使用mysql连接加上后面rew...
##store.db.user=username
##store.db.password=password
##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

store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

4.将第三步的配置运行

官方提供了一个sh脚本,运行脚本将其加入到seata配置中,脚本地址如下:

github.com/seata/blob/1.4.2/script/config-center/nacos/nacos-config.sh

1.将脚本放到conf目录下

因为该脚本是执行它上级目录下的config.txt文件

2.执行脚本

sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -u nacos -w nacos
#参数解释
#-h 为nacos的地址 默认localhost
#-p 为nacos的端口号 默认是8848
#-g 配置的nacos上的分组 默认是 SEATA_GROUP
#-t nacos的namespace的id 默认是''
#-u nacos账号 默认是''
#-w nacos密码 默认是''
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值