CentOS环境下搭建seata(二进制、MySQL)

  seata官网:https://seata.io/zh-cn/。

  我在这里部署的nacos版本是2.0.4,采用的存储模式为MySQL。注意:seata有三种存储模式:file、db、redis,如果是单节点,并且存储模式采用file,那么直接解压启动即可;存储模式采用redis,则需要seata-server 1.3版本及以上。

  Spring Cloud Alibaba版本对应组件版本说明,详细参考官网文档:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E。

(1).初始化数据库

  官方的数据库初始化脚本地址:https://github.com/seata/seata/tree/master/script/server/db。

  官方提供了三种数据库脚本,我这里列举一下Mysql数据库的脚本:

-- -------------------------------- 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_status_gmt_modified` (`status` , `gmt_modified`),
    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(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

(2).上传压缩包,修改配置文件

  解压压缩包

[root@youxi1 ~]# mkdir /usr/local/seata-server
[root@youxi1 ~]# tar zxvf seata-server-1.4.2.zip -C /usr/local/seata-server

  修改配置文件

[root@youxi1 ~]# vi /usr/local/seata-server/seata-server-1.4.2/conf/file.conf
//修改第四行将mode = "file"改为mode = "db"
mode = "db"

//根据
 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"  //根据自己的数据库修改,我这里就是用的mysql
 driverClassName = "com.mysql.cj.jdbc.Driver"  //驱动程序类名,根据实际情况更改
 ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
//jdbc连接数据库的url,根据实际情况修改
url = "jdbc:mysql://[数据库IP地址]:[数据库端口]/[数据库库名]?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncod 
ing=utf-8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowMultiQueries=true"
 user = "[用户名]"  //数据库登录的用户名
 password = "[密码]"  //数据库登录的密码
 minConn = 5
 maxConn = 100
 globalTable = "global_table"
 branchTable = "branch_table"
 lockTable = "lock_table"
 queryLimit = 100
 maxWait = 5000
 }

(3).启动并测试

[root@youxi1 ~]# nohup sh /usr/local/seata-server/seata-server-1.4.2/bin/seata-server.sh -p 18091 -h 127.0.0.1 -m db &
[root@youxi1 ~]$ ps -ef | grep seata
root 6705 22353 4 17:13 pts/0 00:00:02 /usr/bin/java -server -Xmx2048m -Xms2048m -Xmn1024m -Xss512k -XX:SurvivorRatio=10 -XX:
MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=1024m -XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSiz
ePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/seata-server/seata-server-1.4.2/logs/java_heapdump.hprof
 -XX:+DisableExplicitGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=7
5 -Xloggc:/usr/local/seata-server/seata-server-1.4.2/logs/seata_gc.log -verbose:gc -Dio.netty.leakDetectionLevel=advanced -Dl
ogback.color.disable-for-bat=true -classpath /usr/local/seata-server/seata-server-1.4.2/conf:/usr/local/seata-server/seata-se
rver-1.4.2/lib/* -Dapp.name=seata-server -Dapp.pid=6705 -Dapp.repo=/usr/local/seata-server/seata-server-1.4.2/lib -Dapp.home=
/usr/local/seata-server/seata-server-1.4.2 -Dbasedir=/usr/local/seata-server/seata-server-1.4.2 io.seata.server.Server -p 180
91 -h 127.0.0.1 -m db
root 6860 22353 0 17:14 pts/0 00:00:00 grep --color=auto seata

  

  启动时,默认端口为8091。我这里由于启动时报“java.net.BindException: Address already in use”,所以指定了下端口,使其使用18091。但我实际查看下来8091是tcp6占用了,并且PID是个减号(-),不知道为什么。

 

参考资料:https://blog.csdn.net/weixin_44745528/article/details/127333615

     https://blog.csdn.net/m0_52208135/article/details/128821419

 

posted @ 2023-06-14 17:24  苦逼运维  阅读(270)  评论(0编辑  收藏  举报