seata 1.4安装与配置

1. 下载:https://github.com/seata/seata/releases

解压,注意看下里面有个README-zh.md,相关的一些脚本可以在这个文件的链接找到,因为不同的seata版本,脚本可能有差异,需要注意下。我这里把1.4.0的这个文件内容补充在下面:

脚本说明

client

存放用于客户端的配置和SQL

  • at: AT模式下的 undo_log 建表语句
  • conf: 客户端的配置文件
  • saga: SAGA 模式下所需表的建表语句
  • spring: SpringBoot 应用支持的配置文件

server

存放server侧所需SQL和部署脚本

  • db: server 侧的保存模式为 db 时所需表的建表语句
  • docker-compose: server 侧通过 docker-compose 部署的脚本
  • helm: server 侧通过 Helm 部署的脚本
  • kubernetes: server 侧通过 Kubernetes 部署的脚本

config-center

用于存放各种配置中心的初始化脚本,执行时都会读取 config.txt配置文件,并写入配置中心

  • nacos: 用于向 Nacos 中添加配置
  • zk: 用于向 Zookeeper 中添加配置,脚本依赖 Zookeeper 的相关脚本,需要手动下载;ZooKeeper相关的配置可以写在 zk-params.txt 中,也可以在执行的时候输入
  • apollo: 向 Apollo 中添加配置,Apollo 的地址端口等可以写在 apollo-params.txt,也可以在执行的时候输入
  • etcd3: 用于向 Etcd3 中添加配置
  • consul: 用于向 consul 中添加配置

我按这个文档,找了下相关的脚本地址:https://github.com/seata/seata/tree/develop/script/client

这里我把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';

2. 修改registry.conf的注册中心和配置中心为nacos

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""
    cluster = "default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
    apolloAccesskeySecret = ""
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

3. 配置好之后,启动seata

# nohup sh seata-server.sh -p 8091 -h 127.0.0.1 -m file > seata.log &

-p 指定启动seata server的端口号。
-h 指定seata server所绑定的主机。
-m 事务日志、事务执行信息存储的方式,目前支持file(文件方式)、db(数据库方式,建表语句请查看config/db_store.sql、config/db_undo_log.sql)

这里如果是公网连接seata的话,需要将127.0.0.1换成公网IP。

启动之后可以在nacos的控制台上看到注册的服务:

在这里插入图片描述

补充几点:

1. 如果你的seata使用的模式是db模式,

在这里插入图片描述

请注意,像使用云主机的话,数据库的连接名类似如下:

jdbc:mysql://rm-8vbqa4stkv73491rkso.mysql.zhangbei.rds.aliyuncs.com:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

否则的话,启动会报错。

启动方式改为-m db,具体如下:

# nohup sh seata-server.sh -p 8091 -h 106.13.210.114 -m db > seata.log &

当然这样会报错,在这之前咱们需要将seata的配置导入到nacos中:

进入seata/conf目录下,创建一个nacos-config.sh文件:

# touch nacos-config.sh

将https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh的内容编辑进如nacos-config.sh

然后到seata目录创建config.txt,注意跟nacos-config.sh不在同一个目录

# touch config.txt

将如下内容写入:

service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

store.mode=file
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
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

最后再执行导入:

[root@instance-0terk04p conf]# ls
file.conf  file.conf.example  logback.xml  META-INF  nacos-config.sh  README.md  README-zh.md  registry.conf
[root@instance-0terk04p conf]# sh nacos-config.sh 127.0.0.1
set nacosAddr=localhost:8848
set group=SEATA_GROUP
Set service.vgroupMapping.my_test_tx_group=default successfully 
Set service.default.grouplist=127.0.0.1:8091 successfully 
Set service.enableDegrade=false successfully 
Set service.disableGlobalTransaction=false successfully 
Set store.mode=file successfully 
Set store.db.datasource=druid successfully 
Set store.db.dbType=mysql successfully 
Set store.db.driverClassName=com.mysql.cj.jdbc.Driver successfully 
Set store.db.url=jdbc:mysql://rm-8vbqa4stkv73491rkso.mysql.zhangbei.rds.aliyuncs.com:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 successfully 
Set store.db.user=dongda successfully 
Set store.db.password=$Fuxiaoxue123 successfully 
Set store.db.minConn=5 successfully 
Set store.db.maxConn=30 successfully 
Set store.db.globalTable=global_table successfully 
Set store.db.branchTable=branch_table successfully 
Set store.db.queryLimit=100 successfully 
Set store.db.lockTable=lock_table successfully 
Set store.db.maxWait=5000 successfully 
=========================================================================
 Complete initialization parameters,  total-count:18 ,  failure-count:0 
=========================================================================
 Init nacos config finished, please start seata-server. 
[root@instance-0terk04p conf]# 

导入之后,可以在nacos看到配置信息:


图片.png

然后再把seata重启下

下一节我们来介绍分布式事务如何使用seata

你可能感兴趣的:(seata 1.4安装与配置)