> 文档中心 > Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)

Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)

目录

  • 1、下载
  • 2、解压安装
  • 3、配置数据源
  • 4、配置Nacos
    • 4.1 设置Nacos注册中心
    • 4.2 设置Nacos配置中心
  • 5、启动、停止
  • 6、自启配置
  • 7、整合使用
    • 7.1 pom
    • 7.2 undo_log建表
    • 7.3 yaml
    • 7.4 测试使用
  • 8、数据源支持及事务级别

1、下载

下载地址:https://github.com/seata/seata/releases,下载 seata-server-1.4.2.tar.gz安装包。

2、解压安装

创建安装目录:

mkdir /opt/cloud -p

将下载的seata-server-1.4.2.tar.gz拷贝到/opt/cloud目录下,并且解压得到seata目录。

tar -axvf  seata-server-1.4.2.tar.gz

3、配置数据源

Seata-Server端存储模式(store.mode)现有file、db、redis三种,file模式无需改动,直接启动即可。
注: file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高;

如果是seata分布式集群时推荐使用Redis或者DB模式,在此配置Mysql进行储存,步骤如下:

1、建立数据库:seata

2、建表,参考语句:https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql
Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)

3、导入mysql的jar包:
默认情况下seata-server没有导入mysql的驱动包,需要手动加入jar导入到lib目录下,比如将mysql-connector-java-8.0.18.jar拷贝到/opt/cloud/seata/seata-server-1.4.2/lib目录下。

4、修改file.conf:
修改file.conf中的mode为db,并且指定在db{}中指定mysql的配置信息,注意修改内容如下:

store {  ## store mode: file、db、redis  mode = "db"   .....  db{  datasource = "druid"    dbType = "mysql"    driverClassName = "com.mysql.cj.jdbc.Driver"    url = "jdbc:mysql://111.229.160.175:3316/seata?useUnicode=true&serverTimezone=Asia/Shanghai"    user = "root"    password = "123456"    .....    .....  }  .....}

4、配置Nacos

4.1 设置Nacos注册中心

配置registry.conf文件:

vim /opt/cloud/seata/seata-server-1.4.2/conf/registry.conf

核心内容如下:

registry {type = "nacos"nacos {application = "seata-server"serverAddr = "111.229.160.175:8848"    group = "SEATA_GROUP"namespace = "13485083-0e72-47f7-af56-18b16a79ab56" //命名空间cluster = "default" // 配置cluster名称 username = "nacos"    password = "nacos"}........}........config {type = "nacos"nacos { serverAddr ="111.229.160.175:8848" namespace = "13485083-0e72-47f7-af56-18b16a79ab56" group = "SEATA_GROUP" username = "nacos" password = "nacos"    // 默认值 dataId = "seataServer.properties"}........}....

4.2 设置Nacos配置中心

从v1.4.2版本开始,已支持从一个Nacos dataId中获取所有配置信息,你只需要额外添加一个dataId配置项。

在nacos新建配置,此处dataId为seataServer.properties。注意建立的nameSpacegroup要与registry.conf文件中的一致。

配置内容,参考:https://github.com/seata/seata/blob/develop/script/config-center/config.txt中的内容,并按实际需要进行修改,主要修改内容如下(其他保持不变):

# 配置事务组# default_tx_group 事务分组名称可以自定义为项目名称,比如:seata_demo# default表示registry.conf中配置的cluster名称service.vgroupMapping.default_tx_group=default....store.mode=db........#与file.conf配置的mysql数据源一致store.db.datasource=druidstore.db.dbType=mysqlstore.db.driverClassName=com.mysql.cj.jdbc.Driverstore.db.url=jdbc:mysql://111.229.160.175:3316/seata?useUnicode=true&serverTimezone=Asia/Shanghaistore.db.user=rootstore.db.password=123456........

效果:
Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)

5、启动、停止

注意:云服务器安全组或者服务防火墙需要开启 8091端口

seata启动时,默认的JVM内存参数为:-Xmx2048m -Xms2048m -Xmn1024m,如果有需要可以根据服务器的情况进行修改。

启动命令:

# -h: 注册到注册中心的ip# -p: Server rpc 监听端口(默认8091)# -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突sh seata-server.sh -h 111.14.18.14 -p 8091 -n 1  >seata.log 2>1 & echo $! > pidfile.txt

为了方便快捷的实现启动与停止以及观察日志信息,通过创建sh脚本实现,如下:

1、创建启动脚本:

vim /opt/cloud/seata/seata-server-1.4.2/bin/start.sh

内容:

#!/bin/shexport LANG="en_US.UTF-8"cd /opt/cloud/seata/seata-server-1.4.2/binrunMessage=`ps aux | grep \`cat pidfile.txt\``projectStartCommand="sh seata-server.sh"if [[ $runMessage == *$projectStartCommand* ]]then    echo "Application has starting ,restarting..."    kill -9 `cat pidfile.txt`    nohup sh seata-server.sh -h 162.14.115.18 -p 8091 -n 1  >seata.log 2>1 & echo $! > pidfile.txtelse    echo "Application has stopped ,starting..."    nohup sh seata-server.sh -h 162.14.115.18 -p 8091 -n 1  >seata.log 2>1 & echo $! > pidfile.txtfi

2、创建停止脚本:

vim /opt/cloud/seata/seata-server-1.4.2/bin/stop.sh

内容:

#!/bin/shcd /opt/cloud/seata/seata-server-1.4.2/binPID=$(cat pidfile.txt)if [ ${PID} ]; then    echo 'Application is stpping...'    echo kill $PID DONE    kill $PIDelse    echo 'Application is already stopped...'fi

3、修改脚本权限:

chmod 777  stop.shchmod 777  start.sh

4、启动、停止:

# 启动sh start.sh# 停止sh stop.sh

效果:
启动成功后注册到了Nacos
在这里插入图片描述

6、自启配置

修改seata-server.sh文件,指定JAVA_HOME路径
手动指定seata-server.shJAVACMD的值,加入以下语句:

# 指定实际的jdk路径JAVACMD=/usr/local/java/jdk1.8.0_131/bin/java

在这里插入图片描述

创建service:

vim /usr/lib/systemd/system/seata.service

内容:

[Unit]Description=seata[Service]Type=forkingExecStart=/opt/cloud/seata/bin/seata-server-1.4.2/start.shExecStop=/opt/cloud/seata/bin/seata-server-1.4.2/stop.shPrivateTmp=true[Install]WantedBy=multi-user.target

加入自启:

systemctl daemon-reloadsystemctl enable seata.service

启动、停止:

# 启动systemctl start seata# 停止systemctl stop seata

7、整合使用

7.1 pom

<dependency>    <groupId>com.alibaba.cloud</groupId>    <artifactId>spring-cloud-starter-alibaba-seata</artifactId></dependency>

注意:
在引入pom需要注意SpringCloudAlibaba的版本,版本对应关系查询,比如:
在这里插入图片描述
如果SpringCloudAlibabaSeata的默认版本不对应时,可以单独引入seata依赖,比如:

<dependency>     <groupId>com.alibaba.cloud</groupId>     <artifactId>spring-cloud-starter-alibaba-seata</artifactId>     <exclusions>    <exclusion>      <artifactId>seata-spring-boot-starter</artifactId>      <groupId>io.seata</groupId>  </exclusion>     </exclusions> </dependency>  <dependency>     <groupId>io.seata</groupId>     <artifactId>seata-spring-boot-starter</artifactId>     <version>1.4.2</version> </dependency>

7.2 undo_log建表

在微服务客户端连接的数据库中建立undo_log表,语句如下:

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';

7.3 yaml

seata:  enabled: true  application-id: ${spring.application.name}  # 事务组的名称,对应service.vgroupMapping.default_tx_group=xxx中配置的default_tx_group  tx-service-group: default_tx_group  # 配置事务组与集群的对应关系  service:    vgroup-mapping:      # default_tx_group为事务组的名称,default为集群名称(与registry.conf中的一致)      default_tx_group: default    disable-global-transaction: false  registry:    type: nacos    nacos:      application: seata-server      server-addr: 112.15.114.18:8848      group: SEATA_GROUP      namespace: 64ed9ca7-d705-4655-b4e4-f824e420a12a      username: nacos      password: nacos      # registry.conf中,配置cluster名称      cluster: default  config:    type: nacos    nacos:      server-addr: 112.15.114.18:8848      group: SEATA_GROUP      namespace: 64ed9ca7-d705-4655-b4e4-f824e420a12a      username: nacos      password: nacos      # nacos配置中心配置的dataId      data-id: seataServer.properties

7.4 测试使用

全局事务注解:

@GlobalTransactional

在事务发起的微服务方法上加上注解@GlobalTransactional表示全局事务中的一个TM。
注意:异常不能被捕获需要抛出,事务才会发起回滚

比如:

@RestController@RequestMapping("test")public class TestController {    @GetMapping("/insert")    @GlobalTransactional    public void selectUserWageByUserId() {............// 如果RootContext.getXID()不为空,则表示seata生效System.out.println("RootContext.getXID():" + RootContext.getXID());    }}

效果:
可以看到在undo_log以及seata服务global_tablebranch_tablelock_table中存在seata执行分布式事务时的数据。

Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)
Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)
Linux部署seata-1.4.2整合SpringCloud使用(Nacos实现配置与注册中心)

8、数据源支持及事务级别

隔离级别:
Seata的隔离级别默认为读未提交

数据源支持情况如下:

  • AT模式支持的数据库有:MySQL、Oracle、PostgreSQL、 TiDB、MariaDB。
  • TCC模式不依赖数据源(1.4.2版本及之前),1.4.2版本之后增加了TCC防悬挂措施,需要数据源支持。
  • Saga模式不依赖数据源。
  • XA模式只支持实现了XA协议的数据库。Seata支持MySQL、Oracle、PostgreSQL和MariaDB。

参考:https://seata.io/zh-cn/docs/user/datasource.html