Seata1.4集成Springboot + Nacos(三):client端环境集成

本文版本使用:seata 1.4.0、nacos 1.3.2、springboot 2.1.1、mysql 5.7,其中springboot、nacos和mysql是安装和使用网上一搜便是,不再赘述。

一、创建数据库表

在各个业务数据库里增加UNDO_LOG表,如有demo-web库和demo-goods库,则俩库里都要创建这个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(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;

二、配置maven依赖

<dependency>
  <groupId>io.seata</groupId>
  <artifactId>seata-spring-boot-starter</artifactId>
  <version>1.4.0</version>
</dependency>
<dependency>
  <groupId>com.alibaba.nacos</groupId>
  <artifactId>nacos-client</artifactId>
  <version>1.3.2</version>
</dependency>

三、在nacos中新建client配置

在nacos命名空间“demo-web”和“demo-goods”都新建dataId=seata-client的配置:

client端参数配置说明:

dataId=seata-client

group=MIDDLEWARE_GROUP

配置文件demo-web:

seata:
  enabled: true
  application-id: demo-web
  tx-service-group: demo-web-seata-service-group # 要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应
  service:
    vgroup-mapping:
      demo-web-seata-service-group: default
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: "127.0.0.1:8848"
      group : "SEATA_GROUP"
      namespace: "c38bf03a-2ee2-4ee6-834a-2db99dbec528"
  config:
    type: nacos
    nacos:
      server-addr: "127.0.0.1:8848"
      group : "SEATA_GROUP"
      namespace: "c38bf03a-2ee2-4ee6-834a-2db99dbec528"

配置文件songpin-goods:

seata:
  enabled: true
  application-id: demo-goods-seata
  tx-service-group: demo-goods-seata-service-group # 要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应
  service:
    vgroup-mapping:
      demo-goods-seata-service-group: default
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: "127.0.0.1:8848"
      group : "SEATA_GROUP"
      namespace: "c38bf03a-2ee2-4ee6-834a-2db99dbec528"
  config:
    type: nacos
    nacos:
      server-addr: "127.0.0.1:8848"
      group : "SEATA_GROUP"
      namespace: "c38bf03a-2ee2-4ee6-834a-2db99dbec528"

四、业务代码支持

在需要分布式事务的最外层业务员代码加上注解(原本地事务注解@Transactional就不要了,换成如下注解):

@GlobalTransactional(rollbackFor = Exception.class)

至此,服务端与客户端的开发对接工作就完成啦,接下来就是启动测试看看效果!

踩坑:

1.应用启动后一直有错误log:

can not get cluster name in registry config service.vgroupMapping.demo-web-seata-service-group, please make sure registry config correct

解决:就是因为client端nacos的相关配置错误了,仔细检查ip、端口、命名空间之类的

官方部署指南: 官方部署脚本: 参考博客:
经验分享 程序员 微信小程序 职场和发展