springboot+ seata+nacos

一、seata服务端配置

1、在 Seata Release 下载最新版的 Seata Server 并解压得到如下目录:

.
├──bin
├──conf
├──file_store
└──lib

2、 修改 conf/registry.conf 配置,
目前seata支持如下的file、nacos 、apollo、zk、consul的注册中心和配置中心。这里我们以nacos 为例。
将 type 改为 nacos

registry {
   type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "192.168.1.237:8848"
    group = "SEATA_GROUP"
    namespace = "xxxxxxxxxxxxxxxxxx"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  type = "nacos"

  nacos {
    serverAddr = "192.168.1.237:8848"
    namespace = "xxxxxxxxxxxxxxxxxxxxxxxx"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
    dataId = "seataServer.yml"
  }
}
  1. serverAddr = “192.168.10.200:8848” :nacos 的地址
  2. namespace = “”:nacos的命名空间默认为""
  3. cluster = “default” :集群设置未默认 default

3 .1、方式一:修改 conf/nacos-config.txt配置
注意:如果你的seata是1.1之后的版本请按照如下导入方式操作
下载https://github.com/seata/seata/blob/develop/script/config-center/目录下的config.txt文件放入到seata-server目录下的conf中,可更改文件名为nacos-config.txt。
下载https://github.com/seata/seata/tree/develop/script/config-center/nacos/目录下的nacos-config.py文件(window可用python命令执行,linux也可执行,前提先有安装python)或者nacos-config.sh文件(window下可放在git命令行执行,linux可执行文件)

linux执行sh报错:$’\r’: 未找到命令的解决:

背景
  执行.sh脚本时出现$’\r’: 未找到命令,
原因
  是因为命令直接从windows 复制过来导致的
解决
  yum install dos2unix
  dos2unix **.sh 进行转换
  再次执行即可

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=true
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
service.vgroupMapping.multimedia-service-seata-service-group=default
service.vgroupMapping.system-service-seata-service-group=default
service.vgroupMapping.clientapi-service-seata-service-group=default
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.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.152:3306/seata?useUnicode=true
store.db.user=root
store.db.password=123456
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.lock.mode=file
store.session.mode=file
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
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
server.distributedLockExpireTime=10000
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

配置的详细说明参考官网:https://seata.io/zh-cn/docs/user/configurations.html

这里主要修改了如下几项:

store.mode :存储模式 默认file 这里我修改为db 模式 ,并且需要三个表global_table、branch_table和lock_table
store.db.driver-class-name: 0.8.0版本默认没有,会报错。添加了 com.mysql.jdbc.Driver
store.db.datasource=druid:数据源 druid
store.db.db-type=mysql : 存储数据库的类型为mysql
store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true : 修改为自己的数据库url、port、数据库名称
store.db.user=root:数据库的账号
store.db.password=123456 :数据库的密码
service.vgroupMapping.multimedia-service-seata-service-group=default
service.vgroupMapping.system-service-seata-service-group=default
service.vgroupMapping.clientapi-service-seata-service-group=default
client.support.spring.datasource.autoproxy=true 开启数据源自动代理

将 Seata 配置添加到 Nacos 中
使用命令如下

sh nacos-config.sh nacos地址

成功后会提示

init nacos config finished, please start seata-server

2.2.5 启动 Seata Server

sh seata-server.sh -p 8091 -h 192.168.1.237

3 .2、方式二:增加 seataServer.yml配置

seataServer.yml

store:
  mode: db
  db:
    datasource : druid
    dbType: mysql
    driverClassName: com.mysql.jdbc.Driver
    url : jdbc:mysql://127.0.0.1:3306/seata?characterEncoding=UTF-8&useUnicode=true&rewriteBatchedStatements=true
    user : root
    password : root
    minConn : 5
    maxConn : 10
    globalTable : global_table
    branchTable : branch_table
    lockTable : lock_table
    queryLimit : 100
    maxWait : 5000

4、 每个业务库中必须包含 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  DEFAULT CHARSET=utf8;

db模式下的所需的三个表的数据库脚本

global_table的表结构

CREATE TABLE `global_table` (
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `status` tinyint(4) NOT NULL,
  `application_id` varchar(64) DEFAULT NULL,
  `transaction_service_group` varchar(64) DEFAULT NULL,
  `transaction_name` varchar(64) DEFAULT NULL,
  `timeout` int(11) DEFAULT NULL,
  `begin_time` bigint(20) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`xid`),
  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


branch_table的表结构

CREATE TABLE `branch_table` (
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `resource_group_id` varchar(32) DEFAULT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `lock_key` varchar(128) DEFAULT NULL,
  `branch_type` varchar(8) DEFAULT NULL,
  `status` tinyint(4) DEFAULT NULL,
  `client_id` varchar(64) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



lock_table的表结构

create table `lock_table` (
  `row_key` varchar(128) not null,
  `xid` varchar(96),
  `transaction_id` long ,
  `branch_id` long,
  `resource_id` varchar(256) ,
  `table_name` varchar(32) ,
  `pk` varchar(32) ,
  `gmt_create` datetime ,
  `gmt_modified` datetime,
  primary key(`row_key`)
);

二、项目中使用

1、加入依赖包

根目录下pom文件增加
<properties>
    <io.seata.version>1.4.1</io.seata.version>
</properties>

<dependencyManagement>
		<dependencies>
            <dependency>
            	<groupId>io.seata</groupId>
            	<artifactId>seata-spring-boot-starter</artifactId>
            	<version>${io.seata.version}</version>
            </dependency>
    </dependencies>
</dependencyManagement>
core包下pom文件增加
<dependency>
			<groupId>io.seata</groupId>
			<artifactId>seata-spring-boot-starter</artifactId>
			<version>${io.seata.version}</version>
</dependency>

ps:使用shardingsphere依赖版本需升级为4.1.1

2、nacos增加对应项目的配置

# seata 分布式事务配置
seata:
  application-id: system-server
  tx-service-group: system-service-seata-service-group
  config:
    type: nacos
    nacos:
      server-addr: 192.168.1.237:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
  registry:
    type: nacos
    nacos:
      server-addr: 192.168.1.237:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      application: seata-server

group 和 application 需和seata-server端的配置一致

tx-service-group(事务分组)一定要设置
service.vgroupMapping.事务分组配置项,取得配置项的值就是TC集群的名称(和服务端cluster配置对应) 拿到集群名称程序通过一定的前后缀+集群名称去构造服务名,各配置中心的服务名实现不同。拿到服务名去相应的注册中心去拉取相应服务名的服务列表,获得后端真实的TC服务列表。

事务分组可以作为资源的逻辑隔离单位,当发生故障时可以快速failover。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值