• Docker安装Seata


    部署环境:

    Seata版本1.4.0
    docker
    mysql 5.7
    注册中心:zookeeper

    说明:其他配置中心的配置步骤一样,只是配置文件内容个别改变

    参考:

    官方部署手册
    https://linewell.yuque.com/oill7w/newbie/bkgkq0
    https://seata.io/zh-cn/docs/ops/deploy-by-docker-142.html

    标题部署步骤:

    1、先启动zookeeper

    docker run -d -p 2181:2181 \
    -v /home/docker/zookeeper/data:/data \
    -v /home/docker/zookeeper/logs:/logs \
    -v /home/docker/zookeeper/datalog:/datalog \
    --name zookeeper \
    --restart=always \
    zookeeper:latest
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、Seata镜像下载

    docker pull seataio/seata-server:1.4.0
    
    • 1

    3、数据库新建seata相关表

    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    -- ----------------------------
    -- Table structure for undo_log
    -- ----------------------------
    DROP TABLE IF EXISTS `undo_log`;
    CREATE TABLE `undo_log`  (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `branch_id` bigint(20) NOT NULL,
      `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `rollback_info` longblob NOT NULL,
      `log_status` int(11) NOT NULL,
      `log_created` datetime(0) NOT NULL,
      `log_modified` datetime(0) NOT NULL,
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'seata操作记录表' ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Table structure for branch_table
    -- ----------------------------
    DROP TABLE IF EXISTS `branch_table`;
    CREATE TABLE `branch_table`  (
      `branch_id` bigint(20) NOT NULL,
      `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `transaction_id` bigint(20) NULL DEFAULT NULL,
      `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `status` tinyint(4) NULL DEFAULT NULL,
      `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `gmt_create` datetime(6) NULL DEFAULT NULL,
      `gmt_modified` datetime(6) NULL DEFAULT NULL,
      PRIMARY KEY (`branch_id`) USING BTREE,
      INDEX `idx_xid`(`xid`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'seata分支事务表' ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Table structure for global_table
    -- ----------------------------
    DROP TABLE IF EXISTS `global_table`;
    CREATE TABLE `global_table`  (
      `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `transaction_id` bigint(20) NULL DEFAULT NULL,
      `status` tinyint(4) NOT NULL,
      `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `transaction_service_group` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `timeout` int(11) NULL DEFAULT NULL,
      `begin_time` bigint(20) NULL DEFAULT NULL,
      `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `gmt_create` datetime(0) NULL DEFAULT NULL,
      `gmt_modified` datetime(0) NULL DEFAULT NULL,
      PRIMARY KEY (`xid`) USING BTREE,
      INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
      INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'seata全局事务表' ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Table structure for lock_table
    -- ----------------------------
    DROP TABLE IF EXISTS `lock_table`;
    CREATE TABLE `lock_table`  (
      `row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
      `xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `transaction_id` bigint(20) NULL DEFAULT NULL,
      `branch_id` bigint(20) NOT NULL,
      `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `gmt_create` datetime(0) NULL DEFAULT NULL,
      `gmt_modified` datetime(0) NULL DEFAULT NULL,
      PRIMARY KEY (`row_key`) USING BTREE,
      INDEX `idx_branch_id`(`branch_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'seata锁表' ROW_FORMAT = Dynamic;
    
    SET FOREIGN_KEY_CHECKS = 1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    4、运行seata

    (1)不配置启动seata

    docker run -d --name seata-server -p 8091:8091 seataio/seata-server:1.4.0	
    
    • 1

    (2)将配置文件拷贝出来:

    docker cp seata-server:/seata-server/resources /home/docker/seata/config
    
    • 1

    (3)编辑配置文件file.conf和registry.conf

    	# 切换到外部文件夹下
    	cd /home/docker/seata/config/resources
    	
    	# 配置 file.conf,存储方式为db,并配置数据库,注意 driverClassName在mysql8.x是com.mysql.cj.jdbc.Driver
    	vi file.conf
    	
    	# 保存退出	
    	:wq
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    # 配置registry.conf,配置注册类型为zk,并配置zk注册中心地址
     vi registry.conf
    
    # 保存退出
    :wq
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/90c18b5f587f4012a5ee6de93d3816de.png

    (4)挂载配置文件重新启动seata

    # 停止容器
    docker stop seata-server
    
    # 删除容器
    docker rm seata-server
    
    # 重新启动容器,这次是为了挂载外面的配置文件
    docker run -d \
    --name seata-server \
    -p 8091:8091 \
    -e SEATA_IP=192.168.8.49 \
    -v /home/docker/seata/config/resources:/seata-server/resources \
    seataio/seata-server:1.4.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5、zookeeper 初始化seata

    (1)配置脚本

    官网下载对应版本的包,里面有各中注册中心的初始化seata脚本

    https://github.com/seata/seata/releases/tag/v1.4.0
    
    • 1

    在这里插入图片描述
    在这里插入图片描述
    或者自己新建,步骤如下

    # 切换到zk的/data目录下
    cd /home/docker/zookeeper/data
    
    # 配置文件config.txt
    vi config.txt
    
    # 新建zk文件夹
    mkdir zk
    cd zk/
    # 配置脚本 vi zk-config.sh,内容如下
    vi zk-config.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    config.txt内容如下:
    transport.type=TCP
    transport.server=NIO
    transport.heartbeat=true
    transport.enableClientBatchSendRequest=false
    transport.threadFactory.bossThreadPrefix=NettyBoss
    transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
    transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
    transport.threadFactory.shareBossWorker=false
    transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
    transport.threadFactory.clientSelectorThreadSize=1
    transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
    transport.threadFactory.bossThreadSize=1
    transport.threadFactory.workerThreadSize=default
    transport.shutdown.wait=3
    service.vgroupMapping.my_test_tx_group=default
    service.default.grouplist=127.0.0.1:8091
    service.enableDegrade=false
    service.disableGlobalTransaction=false
    client.rm.asyncCommitBufferLimit=10000
    client.rm.lock.retryInterval=10
    client.rm.lock.retryTimes=30
    client.rm.lock.retryPolicyBranchRollbackOnConflict=true
    client.rm.reportRetryCount=5
    client.rm.tableMetaCheckEnable=false
    client.rm.sqlParserType=druid
    client.rm.reportSuccessEnable=false
    client.rm.sagaBranchRegisterEnable=false
    client.tm.commitRetryCount=5
    client.tm.rollbackRetryCount=5
    client.tm.defaultGlobalTransactionTimeout=60000
    client.tm.degradeCheck=false
    client.tm.degradeCheckAllowTimes=10
    client.tm.degradeCheckPeriod=2000
    store.mode=file
    store.file.dir=file_store/data
    store.file.maxBranchSessionSize=16384
    store.file.maxGlobalSessionSize=512
    store.file.fileWriteBufferCacheSize=16384
    store.file.flushDiskMode=async
    store.file.sessionReloadReadSize=100
    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
    store.redis.host=127.0.0.1
    store.redis.port=6379
    store.redis.maxConn=10
    store.redis.minConn=1
    store.redis.database=0
    store.redis.password=null
    store.redis.queryLimit=100
    server.recovery.committingRetryPeriod=1000
    server.recovery.asynCommittingRetryPeriod=1000
    server.recovery.rollbackingRetryPeriod=1000
    server.recovery.timeoutRetryPeriod=1000
    server.maxCommitRetryTimeout=-1
    server.maxRollbackRetryTimeout=-1
    server.rollbackRetryTimeoutUnlockEnable=false
    client.undo.dataValidation=true
    client.undo.logSerialization=jackson
    client.undo.onlyCareUpdateColumns=true
    server.undo.logSaveDays=7
    server.undo.logDeletePeriod=86400000
    client.undo.logTable=undo_log
    client.log.exceptionRate=100
    transport.serialization=seata
    transport.compressor=none
    metrics.enabled=false
    metrics.registryType=compact
    metrics.exporterList=prometheus
    metrics.exporterPrometheusPort=9898
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    zk-config.sh内容如下:
    #!/usr/bin/env bash
    # Copyright 1999-2019 Seata.io Group.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at、
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    
    # The purpose is to sync the local configuration(config.txt) to zk.
    # This script need to rely on zk.
    
    
    while getopts ":h:p:z:" opt
    do
      case $opt in
      h)
        host=$OPTARG
        ;;
      p)
        port=$OPTARG
        ;;
      z)
        zkHome=$OPTARG
        ;;
      ?)
        echo " USAGE OPTION: $0 [-h host] [-p port] [-z zkHome] "
        exit 1
        ;;
      esac
    done
    
    if [[ -z ${host} ]]; then
        host=localhost
    fi
    if [[ -z ${port} ]]; then
        port=2181
    fi
    if [[ -z ${zkHome} ]]; then
        echo " zk home is empty, please usage option: [-z zkHome] "
        exit 1
    fi
    
    zkAddr=$host:$port
    
    root="/seata"
    tempLog=$(mktemp -u)
    
    echo "ZK address is $zkAddr"
    echo "ZK home is $zkHome"
    echo "ZK config root node is $root"
    
    function check_node() {
    	"$2"/bin/zkCli.sh -server "$1" ls ${root} >/dev/null 2>"${tempLog}"
    }
    
    function create_node() {
    	"$2"/bin/zkCli.sh -server "$1" create ${root} "" >/dev/null
    }
    
    function create_subNode() {
    	"$2"/bin/zkCli.sh -server "$1" create "${root}/$3" "$4" >/dev/null
    }
    
    function delete_node() {
    	"$2"/bin/zkCli.sh -server $1 rmr ${root} "" >/dev/null
    }
    
    check_node "${zkAddr}" "${zkHome}"
    
    if [[ $(cat "${tempLog}") =~ "No such file or directory" ]]; then
    	echo " ZK home is error, please enter correct zk home! "
    	exit 1
    elif [[ $(cat "${tempLog}") =~ "Exception" ]]; then
    	echo " Exception error, please check zk cluster status or if the zk address is entered correctly! "
    	exit 1
    elif [[ $(cat "${tempLog}") =~ "Node does not exist" ]]; then
    	create_node "${zkAddr}" "${zkHome}"
    else
    	read -p "${root} node already exists, now delete ${root} node in zk, y/n: " result
    	if [[ ${result} == "y" ]]; then
    		echo "Delete ${root} node..."
    		delete_node "${zkAddr}" "${zkHome}"
    		create_node "${zkAddr}" "${zkHome}"
    	else
    		exit 0
    	fi
    fi
    
    for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
    	key=${line%%=*}
    	value=${line#*=}
    	echo "Set" "${key}" "=" "${value}"
    	create_subNode "${zkAddr}" "${zkHome}" "${key}" "${value}"
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    (2)配置config.txt,如下图

    group的配置在springboot项目中要配置,名称需要和图中 demo_tx_group保持一致
    注意 driverClassName在mysql8.x是com.mysql.cj.jdbc.Driver
    在这里插入图片描述

    (3)在zk容器内部执行脚本

    # 进入zookeeper容器
    docker exec -it zookeeper bash 
    
    # 切换到/data/zk目录下
    cd /data/zk
    
    # 修改脚本为可执行文件
    chmod +x zk-config.sh
    # 执行脚本
    ./zk-config.sh -h 192.168.206.185 -p 2181 -z /apache-zookeeper-3.7.0-bin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    STM32-高级定时器
    Mysql:事务
    Java高级---Spring Boot---1引言
    redis中数据类型命令整理
    【linux基础(六)】Linux中的开发工具(中)--gcc/g++
    领域驱动模型DDD(四)——Eventuate Tram Saga源码讲解
    用cpolar发布Ubuntu上的网页(2)
    计算机毕业设计(51)java小程序毕设作品之教室图书馆座位预约小程序系统
    葡聚糖-炔基|Alkyne-葡聚糖|提供氨基/羧酸/马来酰亚胺/N-羟基琥珀酰亚胺/叠氮/炔基/巯基功能化葡聚糖
    一个支持IPFS的电子邮件——SKIFF
  • 原文地址:https://blog.csdn.net/qiaodaima0/article/details/125537942