Docker+nacos+seata1.3.0 installation and usage configuration tutorial

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata is easy to use and super simple to use, the installation and configuration is really troublesome, and I encountered various pitfalls. Let's get to the point. o(╥﹏╥)o

1. Version

Note: If the versions do not match, various errors will occur. 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. Docker installation and construction of 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/seate/registry.conf. All subsequent creations are in this directory.

cd /data
mkdir seate
vim registry.conf

2.3 The contents of 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 as the registration center. Note that the address of nacos needs to be changed.

----->>>Here I'd like to add a sentence. 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;

To implement distributed business database, you need to add a table undo_log to each database , 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';

2.4 Create a 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 that btb_tx_group needs to be consistent with the client. By the way, pay attention to the database driver. If it is 8 or above, use my driver. If it is 5.7, use com.mysql.jdbc.Driver

2.5 Create a push script. Because the execution script needs to be in the next layer of config.txt, add a directory

mkdir sh
cd sh
vim nacos-config.sh

The content is as follows: It is best not to make any changes

#!/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 nacos IP address. If the port is not 8848, you need to add -p 8884 to set your port.

bash nacos-config.sh -h 127.0.0.1

2.7 Create a container. Note that if SEATA_IP is an Alibaba Cloud server, you need to write the external network 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 (that is, microservice, using seata in the project)

3.1 pom.xml introduces dependencies

<!-- Seata distributed transactions -->
        <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, and the package versions of the server and client must be consistent.

3.2 Configure project configuration files

#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: The btb_tx_group here should be consistent with the key after the vgroupMapping on the server.

For example: service.vgroupMapping.btb_tx_group=default

3.3 Add annotations for use

@GlobalTransactional

Extension: I used a database, and an error was reported during execution, saying that I was missing a primary key, so I added an incremental primary key id to the undo_log table. The SQL for creating the undo_log table above is the one I added the id to, and the official one is without an id. Please be aware! ! ! !

Question: There is an error in the singular source. The reason is that I am a database and need to set the proxy configuration as follows. According to my actual situation, it refers to the configuration file, otherwise an error will be reported.

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

This is the end of this article about the installation and use of Docker+nacos+seata1.3.0. For more information about the installation and use of Docker+nacos+seata, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the implementation process of Nacos docker stand-alone mode deployment
  • Detailed tutorial on installing nacos in docker and configuring the database
  • Teach you how to deploy nacos in docker

<<:  LinkedIn revamps to simplify website browsing

>>:  How to use Xtrabackup to back up and restore MySQL

Recommend

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

Graphical explanation of the solutions for front-end processing of small icons

Preface Before starting this article, let’s do a ...

Mysql error: Too many connections solution

MySQL database too many connections This error ob...

Practice of multi-layer nested display of element table

There is a requirement for a list containing mult...

Detailed explanation of the abbreviation of state in react

Preface What is state We all say that React is a ...

Non-standard implementation code for MySQL UPDATE statement

Today I will introduce to you a difference betwee...

How to configure Nginx domain name rewriting and wildcard domain name resolution

This article introduces how to configure Nginx to...

Vue3 setup() advanced usage examples detailed explanation

Table of contents 1. Differences between option A...

Comprehensive summary of mysql functions

Table of contents 1. Commonly used string functio...

Analysis of MySQL Aborted connection warning log

Preface: Sometimes, the session connected to MySQ...

How to turn local variables into global variables in JavaScript

First we need to know the self-calling of the fun...

Sample code for implementing the Olympic rings with pure HTML+CSS

Rendering Code - Take the blue and yellow rings a...

Optimizing JavaScript and CSS to improve website performance

<br /> In the first and second parts, we int...