Docker+nacos+seata1.3.0安装与使用配置方法
这篇文章主要介绍“Docker+nacos+seata1.3.0安装与使用配置方法”,在日常操作中,相信很多人在Docker+nacos+seata1.3.0安装与使用配置方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Docker+nacos+seata1.3.0安装与使用配置方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在此之前我搞了一天,虽然seata好用,用起来也超级简单,但是安装配置是真的麻烦,遇见了各种坑,下面来进入正题。o(╥﹏╥)o
一 . 版本
注意:如果版本不匹配也会有各种报错,可以根据官网匹配版本。
seata:1.3.0alibaba.cloud:2.2.3.RELEASEnacos:2.0.2
二. docker安装搭建seata服务端
2.1 下载seata镜像
docker pull seataio/seata-server:1.3.0
2.2 在Linux目录下创建registry.conf,我的路径在/data/seate/registry.conf,接下来的所有创建都在这个目录下
cd /datamkdir seatevim registry.conf
2.3 registry.conf中的内容如下
registry { type = "nacos" nacos { application = "seata-server" serverAddr = "127.0.0.1:8848" group = "SEATA_GROUP" namespace = "" cluster = "default" username = "" password = "" }} config { type = "nacos" nacos { serverAddr = "127.0.0.1:8848" namespace = "" group = "SEATA_GROUP" username = "" password = "" }}
注意registry和config需要在同一个组下,注册中心我用的是nacos,注意nacos的地址要改。
----->>>这里我插一句,需要新建一个数据库seata,并且新建三张表,
-- -------------------------------- The script used when storeMode is 'db' ---------------------------------- the table to store GlobalSession dataCREATE 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 dataCREATE 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 dataCREATE 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;
实现分布式每个业务库都要加一张表undo_log,不然会报错,
CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `branch_id` bigint(20) NOT NULL COMMENT 'branch transaction id', `xid` varchar(100) NOT NULL COMMENT 'global transaction id', `context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization', `rollback_info` longblob NOT NULL COMMENT 'rollback info', `log_status` int(11) NOT NULL COMMENT '0:normal status,1:defense status', `log_created` datetime(6) NOT NULL COMMENT 'create datetime', `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime', PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';
4 创建推送配置文件 vim config.txt,是将文件中的配置推送到nacos中去。
vim config.txt
service.vgroupMapping.btb_tx_group=defaultstore.mode=dbstore.db.datasource=druidstore.db.dbType=mysqlstore.db.driverClassName=com.mysql.cj.jdbc.Driverstore.db.url=jdbc:mysql://172.0.0.1:3306/seata?useUnicode=truestore.db.user=rootstore.db.password=rootstore.db.minConn=5store.db.maxConn=30store.db.globalTable=global_tablestore.db.branchTable=branch_tablestore.db.queryLimit=100store.db.lockTable=lock_tablestore.db.maxWait=5000
注意btb_tx_group需要与客户端保持一致,顺便注意数据库驱动,如果是8以上用我的这个驱动,5.7的用com.mysql.jdbc.Driver
2.5 创建推送脚本,因为执行脚本要在config.txt的下一层,所有加一层目录
mkdir shcd shvim 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:" optdo 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 ;; esacdone if [[ -z ${host} ]]; then host=localhostfiif [[ -z ${port} ]]; then port=8848fiif [[ -z ${group} ]]; then group="SEATA_GROUP"fiif [[ -z ${tenant} ]]; then tenant=""fiif [[ -z ${username} ]]; then username=""fiif [[ -z ${password} ]]; then password=""fi nacosAddr=$host:$portcontentType="content-type:application/json;charset=UTF-8" echo "set nacosAddr=$nacosAddr"echo "set group=$group" failCount=0tempLog=$(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=0for 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 ]]; thenecho " Init nacos config finished, please start seata-server. "elseecho " init nacos config fail. "fi
2.6 执行推送脚本,后面是nacos的ip地址,如果端口不是8848还需要加一个-p 8884你的端口
bash nacos-config.sh -h 127.0.0.1
2.7 创建容器,注意SEATA_IP如果是阿里云服务器需要写外网ip
docker run -d --restart always --name seata-server -p 8091:8091 -e SEATA_IP=172.0.0.1 -e SEATA_CONFIG_NAME=file:/data/seata/registry -v /data/seata:/data/seata seataio/seata-server:1.3.0
三 . 客户端(也就是微服务,项目中使用seata)
1 pom.xml 引入依赖
<!-- seata分布式事务--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
注意这里一定要剔除原来自带的 io.seata包,并且服务端和客户端的包版本要一致。
2 配置项目配置文件
#seataseata.application-id=${spring.application.name}seata.tx-service-group=btb_tx_groupseata.config.type=nacosseata.config.nacos.server-addr=172.0.0.1:8848seata.config.nacos.group=SEATA_GROUPseata.registry.type=nacosseata.registry.nacos.application=seata-serverseata.registry.nacos.server-addr=172.0.0.1:8848seata.registry.nacos.group=SEATA_GROUP
注意:这里的btb_tx_group要跟服务端的vgroupMapping后面的key保持一致,
如:service.vgroupMapping.btb_tx_group=default
3 加入注解使用
@GlobalTransactional
扩展: 我用的是一个数据库,执行报错,说我缺少主键,于是我在表undo_log加了一个增的主键id,上面的创建undo_log表的sql是我加了id的,官方给的是没有id的。请知晓!!!!
问题:单数源是有报错的,原因是因为我是一个数据库,需要设置代理配置如下 ,根据自己实际情况是指配置文件,不然会报错
#单数据源seata.enable-auto-data-source-proxy=true#多数据源seata.enable-auto-data-source-proxy=false
到此,关于“Docker+nacos+seata1.3.0安装与使用配置方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341