Seata1.4.2+Nacos搭建使用(Linux)

下载seata1.4.2

下载地址:https://github.com/seata/seata/releases
这里省略上传Linux,解压等过程,本人seata安装目录为 /usr/local/seata/seata-server-1.4.2

创建seata需要的数据库表

sql文件下载:https://github.com/seata/seata/blob/v1.4.2/script/server/db/mysql.sql

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

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

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `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 = utf8;

将config.txt中的配置导入Nacos

下载一键导入脚本:https://github.com/seata/seata/blob/v1.4.2/script/config-center/nacos/nacos-config.sh

nacos-config.sh脚本中会去找config.txt的位置,如果你的config.conf的位置和nacos-config.sh中的位置不一致,要么改nacos-config.sh文件,要么更换config.conf的位置。

下载conifg.txt:https://github.com/seata/seata/blob/v1.4.2/script/config-center/config.txt

config.txt中配置很多,大部分都是默认配置,没有必要全部都导入Nacos,已下是精简版本:

service.vgroupMapping.default_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

导入成功,naocs上会有很多配置:
在这里插入图片描述

1.4.2后支持把所有配置放在一个dataId(seataServer.properties)中,且若上面散装配置和下图的整合配置都配,下图方式的优先级要高于上面散装配置

在这里插入图片描述

修改file.conf文件

该文件是事务日志的相关配置,本人是记录在数据库的

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    # 这三张表就是上面需要创建的数据库表
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

修改registry.conf文件

该文件是配置 注册中心和配置中心的,本人用的都是Nacos

# 注册中心配置
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa、custom
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "224435a3-a0e5-4045-9aaf-b80c03bae620"
    username = "nacos"
    password = "123456"
    cluster = "default"
  }
}
## 配置中心配置
config {
  # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig、custom
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "224435a3-a0e5-4045-9aaf-b80c03bae620"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "123456"
	dataId = "seataServer.properties"
  }
}

启动seata

本人习惯自己创建一个启动文件:

#!/bin/sh
export LANG="en_US.UTF-8"
cd /usr/local/seata/seata-server-1.4.2/bin
runMessage=`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 127.0.0.1 -p 8091 -n 1  >nohup.out 2>1 & echo $! > pidfile.txt
else
    echo "Application has stopped ,starting..."
    nohup sh seata-server.sh -h 127.0.0.1 -p 8091 -n 1  >nohup.out 2>1 & echo $! > pidfile.txt
fi

本人习惯自己创建一个停止文件:

#!/bin/sh
cd /usr/local/seata/seata-server-1.4.2/bin
PID=$(cat pidfile.txt)
if [ ${PID} ];
then
    echo 'Application is stpping...'
    echo kill $PID DONE
    kill $PID
    echo 'Application is already stopped...'
else
    echo 'Application is already stopped...'
fi

启动成功,nacos上会有服务实例:
在这里插入图片描述

spring cloud整合seata1.4.2

<!-- 分布式事务解决方案 -->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>
<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>

yml文件中配置

seata:
  enabled: true
  # 启用自动代理数据源 多数据源的时候关闭
  enable-auto-data-source-proxy: true
  application-id: ${spring.application.name}
  # 与nacos事务分组配置项保持一致
  tx-service-group: default_tx_group
  # 注册中心
  registry:  
    type: nacos
    nacos:
      # seata-server注册在nacos中的服务名
      application: seata-server
      # nacos的地址端口 
      server-addr: 49.234.102.234:8848
      # seata-server在nacos的分组
      group: SEATA_GROUP
      # seata-server在nacos的命名空间ID
      namespace: 224435a3-a0e5-4045-9aaf-b80c03bae620
      userName: "nacos"
      password: "123456"
  # 配置中心
  config:  
    type: nacos
    nacos:
      server-addr: 49.234.102.234:8848
      group: SEATA_GROUP
      # 与配置项命名空间ID保持一致
      namespace: 224435a3-a0e5-4045-9aaf-b80c03bae620
      userName: "nacos"
      password: "123456"
      # 与配置项Data Id保持一致
      dataId: seataServer.properties

代码

在这里插入图片描述

启动后观察日志

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux上安装Seata 1.4.2并将其注册到Nacos,您可以按照以下步骤操作: 1. 首先,确保您的Linux系统已经安装了Java开发工具包(JDK)。您可以在终端中运行`java -version`命令来检查是否已经安装。如果没有安装,请根据您的系统版本安装适当的JDK。 2. 下载Seata 1.4.2的发布版本。您可以在Seata的GitHub仓库(https://github.com/seata/seata/releases)中找到可用的版本。使用wget命令或通过浏览器下载到您的Linux系统上。 3. 解压下载的Seata发布包。在终端中,使用以下命令将该文件解压到您选择的目录: ```shell tar -zxvf seata-server-1.4.2.tar.gz ``` 4. 进入解压后的Seata目录: ```shell cd seata-server-1.4.2 ``` 5. 在Seata目录下,编辑`conf/registry.conf`文件,配置Nacos注册中心的地址和端口。找到以下配置项: ```properties registry { type = "nacos" nacos { serverAddr = "localhost:8848" namespace = "" } } ``` 将`serverAddr`配置为您Nacos的地址和端口。如果Nacos使用了命名空间,请在`namespace`中填入相应的值。 6. 配置Seata事务日志存储方式。在`conf/registry.conf`文件中,找到以下配置项: ```properties config { ... type = "file" ... } ``` 您可以将`type`配置为其他支持的存储方式,例如MySQL等。根据您的需求进行相应的配置。 7. 配置其他必要的参数。根据您的需求,您可以在`conf/nacos-config.txt`文件中设置Seata的其他参数,如事务日志表名、数据源等。 8. 启动Seata服务器。在终端中,使用以下命令启动Seata服务器: ```shell sh bin/seata-server.sh -p 8091 -m file ``` `-p`指定Seata服务器的端口号,`-m`指定事务日志存储方式。 9. 确认Seata是否成功注册到Nacos。打开浏览器,访问Nacos的管理界面(默认地址为http://localhost:8848/nacos)。在左侧导航栏中,选择“服务管理”,您应该能够看到Seata已经成功注册到Nacos。 现在,您已经成功地在Linux上安装了Seata 1.4.2并将其注册到Nacos。您可以根据自己的需求进行进一步的配置和使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值