linux中安装seata,并且集成到nacos

1,下载seata1.4压缩包:

https://github.com/seata/seata/releases

上传带linux服务器,并且解压,我上传到/opt/seata-1.4下

 

2,创建nacos-seata文件夹,存储nacos的脚本和配置

3,创建 nacos-config.sh,和 nacos-config.txt

我的在一个目录下的,一会儿改下nacos-config.sh的扫描nacos-config.txt路径就行

创建seata数据库:在你的mysql创建个seata数据库,执行下sql语句

-- -------------------------------- 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;

nacos-config.txt内容:

#我的只是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://127.0.0.1:3306/seata?useUnicode=true 
store.db.user=root
store.db.password=xxxxxxxx123456@
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内容:

#!/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执行权限即可,在执行如下命令:

sh nacos-config.sh

 

修改 seata/conf 目录中的 file.conf

修改 seata/conf 目录中的 registry.conf,注册中心的配置,我们这边采用的nacos

 type 值改为 nacos

application 是注册到nacos的seata服务名称,默认即可

serverAddr 是 nacos 服务地址

group 是 seata 在 nacos上注册服务的分组

namespace 是 nacos 命名空间ID(如果使用默认的 public 命名空间,可以注掉这行)

username和password是nacos验证,没有开启就可以注掉

创建seata的启动sh脚本

touch seata-start.sh

脚本让内容如下:
nohup ./bin/seata-server.sh -p 8091 > ./logs/seata.log 2>&1 &  后台启动

给予seata-start.sh执行权限,

启动seata即可 

 我的一开始起不起来,是因为seata用nacos-config.sh同步nacos-config.txt是,数据库的密码总是同步步到nacos中,不得已手动在nacos上建了一个 store.db.password 

 点击编辑,进去把你的数据库密码放进去就行,在点击发布

 在重新启动seata即可

./seata-start.sh 

为了方便对seata的管理,有了启动脚本,怎么没有停止和重启脚本,自己搞一个

我这里先创建一个根据进程名kill进程的一个脚本,取名 = kill-process.sh;

#!/bin/sh
#根据进程名杀死进程
if [ $# -lt 1 ]
then
  echo "缺少参数: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

再创建一个seata的stop脚本,取名 = seata-sop.sh,里面就是调用  kill-process.sh

seata-sop.sh:

#杀死进程名字 = seata 的进程
./kill-process.sh seata

再创建一个seata-restart.sh的脚本,处理seata的重启,里面就是调用 seata-stop.sh,再调用 seata-start.sh而已;

seata-restart.sh:

#执行停止脚本
./seata-stop.sh
echo "seata正在启动......"
#执行启动脚本
./seata-start.sh
# $?代表上个命令执行状态,如果 = 0,则说明执行成功
if [ $? -eq 0 ];then
    sleep 5s
    echo "seata启动成功......"
else 
    echo "seata启动失败......"
fi

分别给这几个sh脚本执行权限,就可以直接执行对应脚本,来启动,停止,重启seata了

更多精彩分享请移步:IT学习道场

更多分享关注公众号【IT学习道场】

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT学习道场

为你的进步加点油!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值