高可用部署seata环境 springcloud整合
依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
建表
AT模式客户端服务的数据库都需要建表undo_log
否则报错
java.sql.SQLSyntaxErrorException: Table ‘psr_enterprise_control_test.undo_log’ doesn’t exist
-- 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';
配置
application.yml
seata:
enabled: true
enable-auto-data-source-proxy: true
tx-service-group: my_test_tx_group # 与seata.service.vgroup-mapping一致
registry:
type: nacos # 与SEATA-SERVER配置一致
nacos:
application: seata-server # 与SEATA-SERVER配置一致
server-addr: ${PSR_NACOS:localhost:8848}
namespace: test # 与SEATA-SERVER配置一致
group: SEATA_GROUP # 与SEATA-SERVER配置一致
cluster: default
config:
type: nacos # 与seata配置中心相同
nacos:
server-addr: ${PSR_NACOS:localhost:8848}
group: SEATA_GROUP # 与SEATA-SERVER配置一致
namespace: test # 与SEATA-SERVER配置一致
service:
vgroup-mapping:
my_test_tx_group: default # 事务分组名
disable-global-transaction: false # 默认开启全局事务
client:
rm:
report-success-enable: false # 不启用日志可以提高性能
启用全局事务
@GlobalTransactional
异常处理
- 无法启动事务报错:
io.seata.common.exception.FrameworkException: No available service
at io.seata.core.rpc.netty.AbstractNettyRemotingClient.loadBalance(AbstractNettyRemotingClient.java:257) ~[seata-all-1.3.0.jar:1.3.0]
原因:无法通过NACOS注册中心找到SEATA-SERVER服务
处理:检查客户端配置中seata.registry和seata.config配置项目是否与SEATA-SERVER中配置一致