一.拉取镜像

  1.docker images查看服务器上有没有seata-server镜像,没有的话从服务器上拉取镜像

docker pull seataio/seata-server:1.1.0

二.自定义配置

   有了镜像之后当然想docker run,但是我们需要自定义一些配置,比如:注册到注册中心,数据库配置

   2.1.创建配置文件目录 mkdir seata-config

   2.2. 目录下创建注册文件 registry.conf(以下使用nacos做demo)

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "172.17.0.2:8848"
    namespace = "public"
    cluster = "default"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "172.17.0.2:8848"
    namespace = "public"
  }

 file {
    name = "/root/seata-config/file.conf"
  }
}

   2.3. 目录下创建file.conf

service {
  #transaction service group mapping
  #指定测试的事务组名称
  vgroup_mapping.my_test_tx_group = "default"
  #only support when registry.type=file, please don't set multiple addresses
  #指定默认组 地址和端口,可以设置多个地址
  default.grouplist = "localhost:8091"
  #disable seata
  #禁用全局事务=false 即开启服务
  disableGlobalTransaction = false
}
 
## transaction log store, only used in seata-server 日志存储方案选择db
store {
  ## store mode: file、db
  mode = "db"
 
  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
  }
 
  ## database store property 配置数据
  db {
    datasource = "druid"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.cj.jdbc.Driver"
    url = "jdbc:mysql://localhost:3306/fescar?serverTimezone=GMT%2B8"
    user = "root"
    password = "ZYP6666"
  }
}

 

三.启动脚本

流程 1.停止docker,2.删除container,3.docker run

#!/bin/bash

docker stop seata-server;
docker container rm seata-server;

docker run --name seata-server -it -d  -p 8091:8091 \
#指定配置文件目录,不用.conf
-e SEATA_CONFIG_NAME=file:/root/seata/config/registry \
-e SEATA_IP=127.0.0.1 \
#挂载前两个自定义的配置进入docker root目录中,注意前者本地目录,后者docker目录
-v /root/seata-config/:/root/seata/config \
--net=bridge --restart=always docker.io/seataio/seata-server:1.1.0;

一个临时学习可用的分布式事务服务就搭好了,使用seata-samples去试试看,使用ip:端口使用

 

四.注册配置中心

接下来我们把seata配置推送至naocs上,到seata源码或者网上弄来两个文件,集群模式下必备技能

第一步:copy一下两个文件到服务机上,这两个文件的作用就是把配置推送到nacos,切记.sh .txt两个文件在同一个目录下

第二步:执行.sh文件,这里的localhsot我曾用内网ip貌似推送失败,未检查原因

sh nacos-config.sh localhost
localhost替换为nacos的ip地址

第三步:出现以下内容认为推送成功,然后重启seata-server服务(执行前面的seata启动脚本)

以下是nacos-config.sh 和.txt文件内容,我们介绍一波sh文件内容

1.判断 sh nacos-config.sh 后面有没有输入参数nacosIP 没有输出提示

2.逐行遍历nacos-config.txt

3.令 key = 行等于号前字符  value = 行等于号后字符

4.向nacos发送post请求,dataId =key ,conent=value,group=SEATA_GROUP 如果返回true打印结果,否则打印异常

5.如果异常数=0,打印初始化成功,否则打印初始化配置失败

#!/usr/bin/env bash
if [ $# != 1 ]; then
echo "./nacos-config.sh nacosIp"
exit -1
fi

nacosIp=$1
echo "set nacosIp=$nacosIp"
error=0

for line in $(cat nacos-config.txt)

do

key=${line%%=*}
value=${line#*=}
echo "\r\n set "${key}" = "${value}

result=`curl -X POST "http://$nacosIp:8848/nacos/v1/cs/configs?dataId=$key&group=SEATA_GROUP&content=$value"`

if [ "$result"x == "true"x ]; then

  echo "\033[42;37m $result \033[0m"

else

  echo "\033[41;37 $result \033[0m"
  let error++

fi

done


if [ $error -eq 0 ]; then

echo  "\r\n\033[42;37m init nacos config finished, please start seata-server. \033[0m"

else

echo  "\r\n\033[41;33m init nacos config fail. \033[0m"

fi

.txt属于配置文件比较容易看懂,不做解释

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.thread-factory.boss-thread-prefix=NettyBoss
transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
transport.thread-factory.share-boss-worker=false
transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
transport.thread-factory.client-selector-thread-size=1
transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
transport.thread-factory.boss-thread-size=1
transport.thread-factory.worker-thread-size=8
transport.shutdown.wait=3
service.vgroup_mapping.my_test_tx_group=default
service.enableDegrade=false
service.disable=false
service.max.commit.retry.timeout=-1
service.max.rollback.retry.timeout=-1
client.async.commit.buffer.limit=10000
client.lock.retry.internal=10
client.lock.retry.times=30
client.lock.retry.policy.branch-rollback-on-conflict=true
client.table.meta.check.enable=true
client.report.retry.count=5
client.tm.commit.retry.count=1
client.tm.rollback.retry.count=1
store.mode=file
store.file.dir=file_store/data
store.file.max-branch-session-size=16384
store.file.max-global-session-size=512
store.file.file-write-buffer-cache-size=16384
store.file.flush-disk-mode=async
store.file.session.reload.read_size=100
store.db.datasource=dbcp
store.db.db-type=mysql
store.db.driver-class-name=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=mysql
store.db.password=mysql
store.db.min-conn=1
store.db.max-conn=3
store.db.global.table=global_table
store.db.branch.table=branch_table
store.db.query-limit=100
store.db.lock-table=lock_table
recovery.committing-retry-period=1000
recovery.asyn-committing-retry-period=1000
recovery.rollbacking-retry-period=1000
recovery.timeout-retry-period=1000
transaction.undo.data.validation=true
transaction.undo.log.serialization=jackson
transaction.undo.log.save.days=7
transaction.undo.log.delete.period=86400000
transaction.undo.log.table=undo_log
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registry-type=compact
metrics.exporter-list=prometheus
metrics.exporter-prometheus-port=9898
support.spring.datasource.autoproxy=false

 

最终结果nacos可以看到seata配置

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐