说明
- 用到的环境都使用docker容器运行,用docker-compose编排。
- 前期以集成讲解演示为主,所有容器都是单机运行,后面会讲解搭建高可用集群环境。
本地环境配置
操作系统 | 运行内存 | CPU核心数 | Docker version |
---|---|---|---|
Mac OS 11 | 32 GB | 六核 | 20.10.5 |
环境搭建
这里创建的容器有:Nacos 2.0.0,MySql 8.0.16,Sentinel Dashboard 1.8.0,Seata 1.4.1。
至于Redis,Mongo,RocketMQ,ES等等,这些我们后面用到再弄。
注意:这里我们的Seata配置直接整合到了Nacos
配置文件
docker-compose
docker-compose.yml
配置如下
version: "3"
services:
ali-nacos:
image: nacos/nacos-server:2.0.0
container_name: ali-nacos
hostname: ali-nacos
env_file:
- ./nacos/nacos-standlone-mysql.env
environment:
- TZ=Asia/Shanghai
volumes:
- ./nacos/logs/:/home/nacos/logs
- ./nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- 8848:8848
- 9555:9555
depends_on:
- ali-mysql
restart: always
networks:
- cloud_alibaba
ali-mysql:
image: nacos/nacos-mysql:8.0.16
container_name: ali-mysql
hostname: ali-mysql
env_file:
- ./mysql/mysql.env
environment:
- TZ=Asia/Shanghai
volumes:
- ./mysql/data:/var/lib/mysql
ports:
- 3306:3306
restart: always
networks:
- cloud_alibaba
ali-sentinel:
image: bladex/sentinel-dashboard:1.8.0
container_name: ali-sentinel
hostname: ali-sentinel
ports:
- 8858:8858
environment:
- TZ=Asia/Shanghai
restart: always
networks:
- cloud_alibaba
ali-seata:
image: seataio/seata-server:1.4.1
container_name: ali-seata
hostname: ali-server
ports:
- 8091:8091
environment:
- SEATA_CONFIG_NAME=file:/root/seata-config/registry
- SEATA_IP=127.0.0.1
- SEATA_PORT=8091
- TZ=Asia/Shanghai
volumes:
- ./seata/config:/root/seata-config
depends_on:
- ali-mysql
- ali-nacos
restart: always
networks:
- cloud_alibaba
networks:
cloud_alibaba:
external:
name: cloud_alibaba
nacos
在yml文件的同目录下创建nacos
目录,然后在目录下创建下面2个文件
创建nacos-standlone-mysql.env
文件
PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=ali-mysql
MYSQL_SERVICE_DB_NAME=nacos_config
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=nacos
MYSQL_SERVICE_PASSWORD=nacos
环境变量配置说明:
名称 | 描述 | 选项 |
---|---|---|
MODE | cluster模式/standalone模式 | cluster/standalone default cluster |
NACOS_SERVERS | nacos cluster地址 | eg. ip1,ip2,ip3 |
PREFER_HOST_MODE | 是否支持hostname | hostname/ip default ip |
NACOS_SERVER_PORT | nacos服务器端口 | default 8848 |
NACOS_SERVER_IP | 多网卡下的自定义nacos服务器IP | |
SPRING_DATASOURCE_PLATFORM | standalone 支持 mysql | mysql / empty default empty |
MYSQL_MASTER_SERVICE_HOST | mysql 主节点host | |
MYSQL_MASTER_SERVICE_PORT | mysql 主节点端口 | default : 3306 |
MYSQL_MASTER_SERVICE_DB_NAME | mysql 主节点数据库 | |
MYSQL_MASTER_SERVICE_USER | 数据库用户名 | |
MYSQL_MASTER_SERVICE_PASSWORD | 数据库密码 | |
MYSQL_SLAVE_SERVICE_HOST | mysql从节点host | |
MYSQL_SLAVE_SERVICE_PORT | mysql从节点端口 | default :3306 |
MYSQL_DATABASE_NUM | 数据库数量 | default :2 |
JVM_XMS | -Xms | default :2g |
JVM_XMX | -Xmx | default :2g |
JVM_XMN | -Xmn | default :1g |
JVM_MS | -XX:MetaspaceSize | default :128m |
JVM_MMS | -XX:MaxMetaspaceSize | default :320m |
NACOS_DEBUG | 开启远程调试 | y/n default :n |
TOMCAT_ACCESSLOG_ENABLED | server.tomcat.accesslog.enabled | default :false |
创建init.d/custom.properties
文件
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**
#management.metrics.export.elastic.host=http://localhost:9200
# metrics for prometheus
management.endpoints.web.exposure.include=*
# metrics for elastic search
#management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200
# metrics for influx
#management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true
mysql
在yml文件的同目录下创建mysql
目录,然后创建mysql.env
文件
MYSQL_ROOT_PASSWORD=codelong
MYSQL_DATABASE=nacos_config
MYSQL_USER=nacos
MYSQL_PASSWORD=nacos
配置说明:
名称 | 描述 |
---|---|
MYSQL_ROOT_PASSWORD | root用户的密码 |
MYSQL_DATABASE | nacos对应的数据库名称 |
MYSQL_USER | nacos用户名 |
MYSQL_PASSWORD | nacos密码 |
seata
在yml文件的同目录下创建seata
目录,然后创建/config/registry.yml
文件
registry:
# file,nacos,eureka,redis,zk,consul,etcd3,sofa
type: nacos
nacos:
application: seata-server
serverAddr: ali-nacos:8848
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
config:
# file、nacos 、apollo、zk、consul、etcd3
type: nacos
nacos:
serverAddr: ali-nacos:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
在yml文件的同目录下创建config-center
目录,用于储存将seata的配置推送到nacos的脚本和配置
创建config.txt
配置文件
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
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
service.vgroupMapping.my_test_tx_group=default
service.vgroupMapping.product-service_group=default
service.vgroupMapping.order-service_group=default
service.vgroupMapping.account-service_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
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=false
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
store.mode=db
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://ali-mysql:3306/seata_config?useUnicode=true&serverTimezone=GMT%2b8
store.db.user=root
store.db.password=codelong
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
store.redis.host=127.0.0.1
store.redis.port=6379
store.redis.maxConn=10
store.redis.minConn=1
store.redis.database=0
store.redis.password=null
store.redis.queryLimit=100
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
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
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
这里我们用sh脚本上传(也可以选择用python脚本上传),创建config-center/nacos/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
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
目录结构
启动容器
进入yml
文件所造目录执行启动命令
# 创建网络组
docker network create cloud_alibaba
# 运行
docker-compose -f docker-compose-cloud-ali.yaml up -d
等启动完成后链接mysql
进入config-center/nacos
目录下执行sh脚本上传seata配置到nacos
cd config-center/nacos
sh ./nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -u nacos -w nacos
# 说明 sh ./nacos-config.sh -h nacos地址 -p nacos端口 -g 所在组 -u nacos用户名 -w nacos密码
看到Init nacos config finished, please start seata-server.
就可以了.
然后创建一个数据库seata_config
执行下面语句创建seata全局事物的3张表
官方sql:github.com/seata/seata…
-- -------------------------------- 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容器会没刷新,可以手动重启一下seata容器
docker restart ali-seata
服务验证
名称 | 地址 | 用户名 | 密码 |
---|---|---|---|
nacos | http://127.0.0.1:8848/nacos | nacos | nacos |
sentinel | http://127.0.0.1:8858/ | sentinel | sentinel |
seata | 127.0.0.1:8091 | ||
mysql | 127.0.0.1:3306 | nacos | nacos |
mysql的root用户的密码为codelong
nacos
浏览器输入:http://127.0.0.1:8848/nacos
用户名和密码都是nacos
说明nacos启动成功。
sentinel
浏览器输入:http://127.0.0.1:8858/
用户名和密码都是sentinel
说明sentinel启动成功。
seata
进入nacos控制台查看服务列表,看到seata-server说明seata注册成功。
mysql
用户名和密码都是nacos
也可以使用root用户登录
到此基础环境已经搭建完毕,具体的配置我们在集成的时候再一一讲解。
附件
文章中用到的文件我都上传到了GitHub上,大家可以直接下载来用。
GitHub链接:github.com/lovemianhua…