基于nacos的seate-samples-nacos在windows环境的调试运行

358 阅读3分钟

基于nacos的seate-samples-nacos在windows环境的调试运行

准备:

  1. github.com/seata/seata…
  2. seata-server 使用release 1.4.2版本
  3. nacos
  4. mysql

本文只阐述seata-samples和seata-sever的配置步骤,nacos和mysql不在做阐述,可参考seata-samples的module,seata-samples-nacos 中的readme。

  1. git clone seata-sample项目后,使用IDEA打开,在seata-samples的pom中注释打开的dubbo module,去掉nacos module的注释。

  2. 根据配置自己的mysql情况调整依赖和jdbc.properties。

  3. 引入jackson-core依赖,排除掉nacos-client引入的jackson-core依赖,使用jackson-core的版本2.13.4

  4. 下载seata-server 1.4.2后解压,在conf文件夹下添加nacos-conf.sh文件,内容如下:

    #!/bin/sh
    # 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"
    
    urlencode() {
      length="${#1}"
      i=0
      while [ $length -gt $i ]; do
        char="${1:$i:1}"
        case $char in
        [a-zA-Z0-9.~_-]) printf $char ;;
        *) printf '%%%02X' "'$char" ;;
        esac
        i=`expr $i + 1`
      done
    }
    
    failCount=0
    tempLog=$(mktemp -u)
    function addConfig() {
      dataId=`urlencode $1`
      content=`urlencode $2`
    # 删除了tenant参数
      curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$dataId&group=$group&content=$content&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=`expr $failCount + 1`
      fi
    }
    
    count=0
    COMMENT_START="#"
    for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
        if [[ "$line" =~ ^"${COMMENT_START}".*  ]]; then
          continue
        fi
        count=`expr $count + 1`
    	  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
    

在config文件夹同级目录添加config.txt,文件内容如下:

#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
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
transport.serialization=seata
transport.compressor=none

#Transaction routing rules configuration, only for the client
# 修改vgroupMapping为my_test_tx_group
service.vgroupMapping.my_test_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

#Transaction rule configuration, only for the client
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=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
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.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

#Log rule configuration, for client and server
log.exceptionRate=100

#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=file
store.lock.mode=file
store.session.mode=file
#Used for password encryption
#store.publicKey=

#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
store.file.dir=file_store/sessionStore
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100

#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
# 删除了db 配置项

#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
# 删除了redis配置项

#Transaction rule configuration, only for the server
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
server.distributedLockExpireTime=10000
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false

#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

对nacos-config.sh和config.txt的修改已使用 标识出来了,请对比查看。

执行sh [nacos-config.sh](http://nacos-config.sh/) -h localhost -p 8848 -g SEATA_GROUP -t 0af6e97b-a684-4647-b696-7c6d42aecce7 -u nacos -w nacos ,出现 Init nacos config finished,please start seata-server 字样说明seata配置导入nacos成功,seata-client可以通过nacos获取seata-server的信息。

Untitled.png

导入配置成功后nacos中的配置列表如图所示

Untitled 1.png 总结:

对seata-samples和seata-server的配置修改阐述完毕,包括

  1. seata-server中需要添加conf.txt和nacos-conf.sh
  2. seata-samples中jdbc.properties的修改,pom中注释dubbo module,去掉nacos module的注释。module seata-samples-nacos pom中nacos-client排除jackson-core依赖,重新添加jackson-core 更新一点的版本-2.13.4

完整运行步骤请参考module seata-samples-nacos 中的readme文件。