若依微服务 + seata1.5.2版本分布式事务(安装配置nacos+部署)

若依官方使用的1.4.0版本seata,版本较低配置相对更麻烦一些

一、seata服务端下载,下载方式介绍两种入口,如下:

1、找到对应版本,下载 binary 即可。 下载包名为:seata-server-1.5.2.zip

2. github上下载   Releases · seata/seata · GitHub 

找到对应的1.5.2版本,每个版本下都有一个缩放的Assets,点击下载即可。

二、解压、修改SEATA配置(并修改nacos配置)

 1. 解压zip,linux解压后目录一致(windows和linux都可以使用)

2、seata配置nacos,进入 conf目录下 cd \seata-server-1.5.2\seata\conf ,修改 application.yml 配置文件内容。

内容如下,可直接复制使用:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace:              #此处不填写,将会默认使用DEFAULT_GROUP
      group: SEATA_GROUP
      username: nacos         #nacos的账户(请自行修改)
      password: nacos         #nacos的密码(请自行修改)
      data-id: seataServer.yml #对应文章后边在nacos中添加的配置
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:              #此处不填写,将会默认使用DEFAULT_GROUP
      cluster: default
      username: nacos         #nacos的账户(请自行修改)
      password: nacos         #nacos的密码(请自行修改)
  #store:
    # support: file 、 db 、 redis
   # mode: file
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

 3、集成Nacos配置中心

 nacos上创建seataServer.yml配置文件(此处的数据库后边有教程,需要添加一个seata专用的库)

请注意此处

driverClassName:com.mysql.jdbc.Driver (配置过程中有可能踩坑)

此处请注意使用的MySQL版本

5.x版本请使用 com.mysql.jdbc.Driver  

8.x版本请使用 com.mysql.cj.jdbc.Driver

service:
  vgroupMapping:
    ruoyi-system-group: default
store:
  db:
    datasource: druid
    dbType: mysql
    driverClassName: com.mysql.jdbc.Driver #此处请注意 5.x版本请使用com.mysql.jdbc.Driver  8以上版本请使用 com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/ry-seata?useUnicode=true    #创建的库(后边有教程)
    user: root   #数据库账号(自行修改)
    password: root   #数据库密码(自行修改)
    globalTable: global_table
    lockTable: lock_table
    branchTable: branch_table
    maxConn: 30
    maxWait: 5000
    minConn: 5
    queryLimit: 100
  mode: db

 

4、nacos配置 给需要用到分布式事务的模块配置信息

添加service.vgroupMapping.ruoyi-xxxx-group配置信息(多个服务模块都需要用分布式事务的话,请添加多个这样的配置,每个配置名称需对应服务模块名称)

service.vgroupMapping.ruoyi-xxxx-group (ruoyi-xxxx-group 中的ruoyi-xxxx请修改为对应的服务名)

内容:default

5、nacos修改服务模块配置(需要使用分布式事务的模块配置都需要修改):

 

三、配置数据库(mysql)

1、 由于seata使用mysql作为db高可用数据库,故需要在mysql创建一个ry-seata库,并导入数据库脚本。

-- -------------------------------- 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 = utf8mb4;

-- 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 = utf8mb4;

-- 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 = utf8mb4;

-- 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 = utf8mb4 COMMENT ='AT transaction mode undo table';

2、需要使用分布式事务项目模块对应的数据库中添加表(多个数据库的话,每个库中都需要加表)

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=25 DEFAULT CHARSET=utf8;

四、启动seata

打开setae的bin文件夹

1、windows系统:直接点击seata-server.bat即可启动seata

2、Linux系统:

        sh seata-server.sh -h xx.xx.x.xxx -p 8091

        注意上面的 -h :指该服务器ip,不指定的话,不然注册到nacos的会是不想要的本地ip(127.xx.xx.xx)

启动完成后可以浏览日志,或者去nacos中服务列表查看seata 服务是否已成功注册。如下图:

注意:seata启动会占用两个端口,8091 和 7091 。 其中 8091 是注册到nacos中的服务端端口,7091是其客户端端口。7091对应页面如下图:

访问地址:xx.xx.x.xxx:7091    账号:   seata/seata

五、微服务中需要使用分布式事务的模块添加事务注解 

 1、主服务添加的事务注解(调用其他模块的主服务方)

@Transactional
@GlobalTransactional(rollbackFor = Exception.class) // 重点 第一个开启事务的需要添加seata全局事务注解

  (rollbackFor = Exception.class):自定义的异常也可进行事务回滚(去除的话,自定义异常将会不进行事务回滚)

 2、被调用服务模块添加分布式事务注解

/** * 事务传播特性设置为 REQUIRES_NEW 开启新的事务 重要!!!!一定要使用REQUIRES_NEW */

 @Transactional(propagation = Propagation.REQUIRES_NEW)

以上内容就是所有配置教程了,到此结束!

​​​​​​​以下内容作为参考:

注意:如果代码运行时出现以下异常,则是序列化问题

json decode exception, Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"@class":"io.seata.rm.datasource.undo.BranchUndoLog","xid":"192.168.1.5:8091:298880381343518720","branchId":298880382744416257,"sqlUndoLogs":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.undo.sqlUndoLog","sqlType":"UPDATE","tableName":"stock_tbl","beforeImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.dataso"[truncated 12232 bytes]; line: 1,column: 3987] (through reference chain: io.seata.rm.datasource.undo.BranchUndoLog["sqlUndoLogs"]->java.util.ArrayList[1]->io.seata.rm.datasource.undo.sqlUndoLog["afterImage"]->io.seata.rm.datasource.sql.struct.TableRecords["rows"]->java.util.ArrayList[0]->io.seata.rm.datasource.sql.struct.Row["fields"]->java.util.ArrayList[6]->io.seata.rm.datasource.sql.struct.Field["value"])

Seata 认采用的序列化方式是 jackson,报错信息显示jackson 反序列化失败

解决方案:降低mysql-connector-java版本(项目模块的pom)

调整mysql-connector-java版本 <= 8.0.22;

<dependency>
    <groupId>MysqL</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>

在该版本下,数据库的datetime 被解析为java.sql.Timestamp,因此也就不存在java.time.LocalDateTime无法反序列化的问题了。

这种方案最简单、快速,推荐;

解决方案可参考(多种解决方案):Seata json decode exception, Cannot construct instance of `java.time.LocalDateTime` 报错原因/解决方案最全汇总版 - 编程之家

Seata配置参考文章:

Seata1.5.2安装配置(nacos)+部署_seata安装和部署_不知道取啥昵称的博客-CSDN博客

分布式事务 seata 最全入门教程_seata教程_handsomeshao的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/yyongsheng/article/details/131248364