Docker + Nacos + seata1.3.0 installation and use configuration tutorial

Time:2021-9-22

I had a day before that. Although Seata is easy to use and super simple to use, the installation and configuration is really troublesome. I met various pits. Let’s get to the point. o(╥﹏╥)o

1、 Version

Note: if the versions do not match, there will also be various errors. You can match the versions according to the official website.


seata:1.3.0
alibaba.cloud:2.2.3.RELEASE
nacos:2.0.2

2、 Install docker and set up Seata server

2.1 download Seata image


docker pull seataio/seata-server:1.3.0

2.2 create registry.conf in the Linux directory. My path is / data / seat / registry.conf, and all subsequent creation is in this directory


cd /data
mkdir seate
vim registry.conf

two point three   The contents in registry.conf are as follows


registry {
  type = "nacos"
 
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }
}
 
config {
  type = "nacos"
 
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
  }
}

Note that registry and config need to be in the same group. I use Nacos in the registry. Note that the address of Nacos should be changed.

—–>>>Let me add here that you need to create a new database, Seata, and three new tables,


-- -------------------------------- 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(96),
    `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;

        Realize distributedEach business libraryAdd a table undo_ Log, otherwise an error will be reported,

CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_ Increment comment 'primary key',
  `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',
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';

two point four   Create the push configuration file VIM config.txt to push the configuration in the file to Nacos.


vim config.txt

service.vgroupMapping.btb_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://172.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
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

Note BTB_ tx_ The group needs to be consistent with the client. By the way, pay attention to the database driver. If it is more than 8, use my driver, and for 5.7, use com.mysql.jdbc.driver

2.5 create a push script, because the execution of the script needs to add a directory under config.txt


mkdir sh
cd sh
vim nacos-config.sh

The content is as follows: it is best not to have any modification


#!/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.
 
while getopts ":h:p:g:t:u:w:" opt
do
  case $opt in
  h)
    host=$OPTARG
    ;;
  p)
    port=$OPTARG
    ;;
  g)
    group=$OPTARG
    ;;
  t)
    tenant=$OPTARG
    ;;
  u)
    username=$OPTARG
    ;;
  w)
    password=$OPTARG
    ;;
  ?)
    echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
    exit 1
    ;;
  esac
done
 
if [[ -z ${host} ]]; then
    host=localhost
fi
if [[ -z ${port} ]]; then
    port=8848
fi
if [[ -z ${group} ]]; then
    group="SEATA_GROUP"
fi
if [[ -z ${tenant} ]]; then
    tenant=""
fi
if [[ -z ${username} ]]; then
    username=""
fi
if [[ -z ${password} ]]; then
    password=""
fi
 
nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"
 
echo "set nacosAddr=$nacosAddr"
echo "set group=$group"
 
failCount=0
tempLog=$(mktemp -u)
function addConfig() {
  curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$1&group=$group&content=$2&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  if [[ -z $(cat "${tempLog}") ]]; then
    echo " Please check the cluster status. "
    exit 1
  fi
  if [[ $(cat "${tempLog}") =~ "true" ]]; then
    echo "Set $1=$2 successfully "
  else
    echo "Set $1=$2 failure "
    (( failCount++ ))
  fi
}
 
count=0
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  (( count++ ))
	key=${line%%=*}
    value=${line#*=}
	addConfig "${key}" "${value}"
done
 
echo "========================================================================="
echo " Complete initialization parameters,  total-count:$count ,  failure-count:$failCount "
echo "========================================================================="
 
if [[ ${failCount} -eq 0 ]]; then
	echo " Init nacos config finished, please start seata-server. "
else
	echo " init nacos config fail. "
fi

2.6 execute the push script, followed by the IP address of Nacos. If the port is not 8848, you need to add -p 8884 your port


bash nacos-config.sh -h 127.0.0.1

2.7 create container, pay attention to Seata_ IP if it is an alicloud server, you need to write an external IP address


docker run -d --restart always --name seata-server -p 8091:8091 -e SEATA_IP=172.0.0.1 -e SEATA_CONFIG_NAME=file:/data/seata/registry -v /data/seata:/data/seata  seataio/seata-server:1.3.0

3、 Client (i.e. micro service, using Seata in the project)

three point one   Pom.xml import dependency

<!--  Seata distributed transaction -- >
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

Note that the original io.seata package must be removed here, and the package versions of the server and client must be consistent.

3.2 project configuration file


#seata
seata.application-id=${spring.application.name}
seata.tx-service-group=btb_tx_group
seata.config.type=nacos
seata.config.nacos.server-addr=172.0.0.1:8848
seata.config.nacos.group=SEATA_GROUP
seata.registry.type=nacos
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=172.0.0.1:8848
seata.registry.nacos.group=SEATA_GROUP

Note: BTB here_ tx_ Group should be consistent with the key after vgroupmapping on the server,

For example: service.vgroupmapping.btb_ tx_ group=default

3.3 use of notes


@GlobalTransactional

Extension:I use a database. An error is reported in the execution, saying that I lack a primary key, so I undo the table_ Log is added with an added primary key ID, and Undo is created above_ I added ID to the SQL of the log table, but the official gave it without ID. Please kindly be informed!!!!

Question:An error is reported for the singular source because I am a database and need to set the agent configuration as follows  , It refers to the configuration file according to your actual situation, otherwise an error will be reported

#Single data source
seata.enable-auto-data-source-proxy=true
#Multiple data sources
seata.enable-auto-data-source-proxy=false

This is the end of this article about the installation and use of docker + Nacos + Seata 1.3.0. For more information about the installation and use of docker + Nacos + Seata, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!