赞
踩
目录
相关软件:seata1.4.0 nacos1.3.2 mysql5.7
链接: https://pan.baidu.com/s/1joOTPZerhAWtUf90Y-lG1Q 提取码: purt 复制这段内容后打开百度网盘手机App,操作更方便哦
下载并解压好后,看到以下文件夹,并添加 logs 文件夹,用来存放seata-server日志
进入 conf 文件夹
修改日志文件:将以下地方修改刚才的日志文件夹即可
修改seata配置文件:registry.conf
- registry {
- # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
- type = "nacos"
-
- nacos {
- application = "seata-server"
- serverAddr = "172.16.10.89:8848"
- namespace = "seata"
- group = "SEATA_GROUP"
- username = ""
- password = ""
- }
- }
-
- config {
- # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig
- type = "nacos"
- # 这里直接复制上的 nacos 即可,一般默认下面没有 application 这个项,要下上面的一样
- nacos {
- application = "seata-server"
- serverAddr = "172.16.10.89:8848"
- namespace = "seata"
- group = "SEATA_GROUP"
- username = ""
- password = ""
- }
- }
删除多余的配置,这里使用 nacos 其它的不需要。
注意:type:要改成 nacos
关于:file.conf 文件,由于采用了 naocs 则,file.conf 配置文件则不用管,其实就是把 type 改成 file
在nacos 中添加配置,这里不采用官网的方式,需要执行 .sh 文件来存入,那样的方式会产生一大堆配置文件(看得眼花)
Data ID: seata-server 要与上面配置中的 application 一样
Group: SEATA_GROUP 要与上面配置一样
要注意:namespace 也要一样
配置内容:
- 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
- # 分组事务定义
- service.vgroupMapping.my_test_tx_group=default
- # seata 地址
- service.default.grouplist=172.16.2.111: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.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
- # 配置类型: db file
- store.mode=db
- #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
- # 数据库配置 - start
- store.db.datasource=druid
- store.db.dbType=mysql
- store.db.driverClassName=com.mysql.jdbc.Driver
- store.db.url=jdbc:mysql://172.16.10.89:3306/seata?useUnicode=true
- store.db.user=seata
- store.db.password=nova2020
- 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
- # 数据库配置 - end
- # redis 配置
- #store.redis.host=127.0.0.1
- #store.redis.port=6379
- #store.redis.maxConn=10
- #store.redis.minConn=1
- #store.redis.database=0
- #store.redis.password=null
- #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
- log.exceptionRate=100
- transport.serialization=seata
- transport.compressor=none
- metrics.enabled=false
- metrics.registryType=compact
- metrics.exporterList=prometheus
- metrics.exporterPrometheusPort=9898
配置需要修改地方:
分组事务定义:只能修改service.vgroupMapping.后面的数据,必须以这个为前缀。
seata地址
store.mode: 要改为 db,这里使用 mysql,然后修改对应的 store.db 相关内容
创建在上的配置的数据库:seata
添加表:
- 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
-
- # -----------------------
- 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
- #------------------------
- CREATE TABLE `lock_table` (
- `row_key` varchar(128) NOT NULL,
- `xid` varchar(96) DEFAULT NULL,
- `transaction_id` mediumtext,
- `branch_id` mediumtext,
- `resource_id` varchar(256) DEFAULT NULL,
- `table_name` varchar(32) DEFAULT NULL,
- `pk` varchar(32) DEFAULT NULL,
- `gmt_create` datetime DEFAULT NULL,
- `gmt_modified` datetime DEFAULT NULL,
- PRIMARY KEY (`row_key`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
至此seata-server 端配置完成
进入seata的bin文件夹,windows 使用 seata-server.bat 进行启动
进入 nacos 看下启动成功与否
这里就不讲述这这个的整合的,百度一大把
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
- </dependency>
在启动配置中的添加事务分组和seata的启动配置:如上图所示,进入 bootstrap.yml 中
注: 是在 cloud 下与nacos平齐,alibaba.seata.tx-service-group 要与在nacos配置 seata-server 中定义的事务一样,否则会报 ip:port 异常
- server:
- port: 10013
- spring:
- application:
- name: alibaba_provider_class_seata
- cloud:
- nacos:
- discovery:
- server-addr: 172.16.10.89:8848
- config:
- server-addr: 172.16.10.89:8848
- file-extension: yaml
- # seata 事务组配置
- alibaba:
- seata:
- # 自定义组名称,需要与seata-server中配置的对应
- tx-service-group: my_test_tx_group
- # seata 配置
- seata:
- registry:
- type: nacos
- nacos:
- application: seata-server
- server-addr: 172.16.10.150:8848
- group : "SEATA_GROUP"
- namespace: "seata"
- username: ""
- password: ""
- config:
- type: nacos
- nacos:
- application: seata-server
- server-addr: 172.16.10.150:8848
- group: "SEATA_GROUP"
- namespace: "seata"
- username: ""
- password: ""
添加一张:
- 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=5 DEFAULT CHARSET=utf8
表说明:这里采用一个 mysql 数据库,但采用不同的数据源
seata数据库: 为 seata-server 的数据库,是步骤安装 seata-server 创建的数据库
seate_class: 是班级微服务的数据库
seate_teacher: 是老师微服务的数据库
注:seate_class 与 seate_teacher 是建库打错字了,本想打 seata 的,表呢也分得不合理,只是为了测试 seata 而弄的。
seate_class 与 seate_teacher 中都有一张 undo_log 表。
在项目中需要全局事务的文法中使用:@GlobalTransactional(timeoutMills = 300000, name = "dubbo-gts-seata-example")
示例:以下示例为测试代码,ClassService 下 TeacherService 是不同的微服务,各自使用各自的数据库
- // dubbo 注入业务处理
- @DubboReference
- ClassService classService;
- @DubboReference
- TeacherService teacherService;
- // 添加班级
- @RequestMapping("insertClass")
- // 开启全局事务
- @GlobalTransactional(timeoutMills = 300000, name = "dubbo-gts-seata-example")
- public ResultBase insertClass(@RequestBody ClassParam param)throws CustomException {
- // 业务逻辑
- classService.insertClass(param);// 添加班级
- //班级与老师
- TTeacherClass tc = new TTeacherClass();
- tc.setType(0);
- tc.setTeacherId(param.getTeacherId());
- tc.setClassId(1);
- //添加老师与班级的关系
- teacherService.insertThacherClass(tc);
- return ResultUtil.getBase();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。