seata部署并設(shè)置開機(jī)自啟

seata部署文檔
上傳壓縮包
上傳壓縮包 seata-server-1.4.0.tar.gz 到linux服務(wù)器 /opt 下

解壓
解壓 seata-server-1.4.0.tar.gz

tar -zxvf seata-server-1.4.0.tar.gz
解壓后的目錄是 seata

seata的配置文件修改
修改 /opt/seata/conf 下的 file.conf 和 registry.conf

file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"

  ## database store property
  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"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://10.100.232.173:3306/seata"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

 

}

registry.conf

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

  nacos {
    application = "seata-server"
    serverAddr = "10.100.232.172:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
 
}

創(chuàng)建seata的數(shù)據(jù)庫(kù)
在mysql上創(chuàng)建一個(gè) mscm-seata-test 的數(shù)據(jù)庫(kù)(你可以根據(jù)自己的喜好取名字),執(zhí)行如下腳本,把表同步進(jìn)去

-- -------------------------------- 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;
seata配置信息數(shù)據(jù)上傳到nacos
創(chuàng)建nacos-seata文件夾,存儲(chǔ)nacos的腳本和配置



創(chuàng)建 nacos-config.sh,和 nacos-config.txt

我的在一個(gè)目錄下的,一會(huì)兒改下nacos-config.sh的掃描nacos-config.txt路徑就行



nacos-config.txt內(nèi)容

#我的只是mysql的DB模式,就需要這些,你若是用其他的,可以保留其他配置
service.vgroupMapping.my_test_tx_group=default  
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://10.100.232.173:3306/seata?useUnicode=true
store.db.user=root
store.db.password=123456
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
nacos-config.sh內(nèi)容:






#!/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
# 剛剛$(dirname "$PWD")/nacos-seata/nacos-config.txt所在的路徑,你的不一樣,可以自己修改
for line in $(cat $(dirname "$PWD")/nacos-seata/nacos-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
然后給nacos-config.sh執(zhí)行權(quán)限即可,再執(zhí)行如下命令:

sh nacos-config.sh



看下nacos




管理腳本
切換到/opt/seata的目錄下,創(chuàng)建 seata-start.sh 和 seata-stop.sh 和 kill-process.sh

seata-start.sh 內(nèi)容

nohup ./bin/seata-server.sh -p 8091 > ./logs/seata.log 2>&1 &
kill-process.sh 內(nèi)容

#!/bin/sh
#根據(jù)進(jìn)程名殺死進(jìn)程
if [ $# -lt 1 ]
then
  echo "缺少參數(shù):procedure_name"
  exit 1
fi
 
PROCESS=`ps -ef|grep $1|grep -v grep|grep -v PPID|awk '{ print $2}'`
for i in $PROCESS
do
  echo "Kill the $1 process [ $i ]"
  kill -9 $i
done

kill-process.sh 內(nèi)容

./kill-process.sh seata
seata-stop.sh 內(nèi)容

#殺死進(jìn)程名字 = seata 的進(jìn)程
./kill-process.sh seata
再創(chuàng)建一個(gè)seata-restart.sh的腳本,處理seata的重啟,里面就是調(diào)用 seata-stop.sh,再調(diào)用 seata-start.sh而已;

seata-restart.sh:

#執(zhí)行停止腳本
./seata-stop.sh
echo "seata正在啟動(dòng)......"
#執(zhí)行啟動(dòng)腳本
./seata-start.sh
# $?代表上個(gè)命令執(zhí)行狀態(tài),如果 = 0,則說明執(zhí)行成功
if [ $? -eq 0 ];then
   sleep 5s
   echo "seata啟動(dòng)成功......"
else
   echo "seata啟動(dòng)失敗......"
fi
分別給這幾個(gè)sh腳本執(zhí)行權(quán)限,就可以直接執(zhí)行對(duì)應(yīng)腳本,來啟動(dòng),停止,重啟seata了

seata開機(jī)自啟
上面的腳本是手動(dòng)執(zhí)行,開機(jī)不能自啟 ,每次宕機(jī),都要手動(dòng)重啟seata,太麻煩

給seata設(shè)置開機(jī)自啟

1)cd /etc/init.d 進(jìn)入目錄

2)創(chuàng)建文件seata

#!/bin/bash
#
#chkconfig: 345 63 37
#description: seata
#processname: seata


export JAVA_HOME=/opt/jdk8/jdk1.8.0_333
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PWD/bin:$JAVA_HOME/bin:$PATH

SEATA_HOME=/opt/seata

case $1 in
  start)
    nohup sh $SEATA_HOME/bin/seata-server.sh -p 8091 -h 10.100.232.171 > seata.out 2>&1 &
        echo $! > $SEATA_HOME/bin/seata.pid
    echo "seata is started"
    ;;
  stop)
    pid=`ps -ef|grep seata |grep -v grep|grep -v PPID|awk '{ print $2}'`
    kill -9 $pid
    echo "seata is stopped"
    ;;

  *)
    echo "start|stop|restart"
    ;;
esac
exit 0

3)給腳本添加權(quán)限chmod 755 seata

4)添加服務(wù)到開機(jī)項(xiàng) chkconfig --add seata

5)設(shè)置為開機(jī)啟動(dòng) chkconfig seata on

6)測(cè)試 service seata start




作者:IT學(xué)習(xí)道場(chǎng)

歡迎關(guān)注微信公眾號(hào) : IT學(xué)習(xí)道場(chǎng)