欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Docker+nacos+seata1.3.0安装与使用配置教程

程序员文章站 2022-03-30 17:03:09
在此之前我搞了一天,虽然seata好用,用起来也超级简单,但是安装配置是真的麻烦,遇见了各种坑,下面来进入正题。o(╥﹏╥)o一 . 版本 注意:如果版本不匹配也会有各种报错,可以根据官网匹配版本。s...

在此之前我搞了一天,虽然seata好用,用起来也超级简单,但是安装配置是真的麻烦,遇见了各种坑,下面来进入正题。o(╥﹏╥)o

一 . 版本

   注意:如果版本不匹配也会有各种报错,可以根据官网匹配版本。

seata:1.3.0
alibaba.cloud:2.2.3.release
nacos: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 /data
mkdir seate
vim 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 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;

        实现分布式每个业务库都要加一张表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';

2.4 创建推送配置文件 vim config.txt,是将文件中的配置推送到nacos中去。

vim config.txt
service.vgroupmapping.btb_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbtype=mysql
store.db.driverclassname=com.mysql.cj.jdbc.driver
store.db.url=jdbc:mysql://172.0.0.1:3306/seata?useunicode=true
store.db.user=root
store.db.password=root
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

注意btb_tx_group需要与客户端保持一致,顺便注意数据库驱动,如果是8以上用我的这个驱动,5.7的用com.mysql.jdbc.driver

        2.5 创建推送脚本,因为执行脚本要在config.txt的下一层,所有加一层目录

mkdir sh
cd sh
vim 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

        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)

3.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包,并且服务端和客户端的包版本要一致。

3.2 配置项目配置文件

#seata
seata.application-id=${spring.application.name}
seata.tx-service-group=btb_tx_group
seata.config.type=nacos
seata.config.nacos.server-addr=172.0.0.1:8848
seata.config.nacos.group=seata_group
seata.registry.type=nacos
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=172.0.0.1:8848
seata.registry.nacos.group=seata_group

注意:这里的btb_tx_group要跟服务端的vgroupmapping后面的key保持一致,

如:service.vgroupmapping.btb_tx_group=default

3.3 加入注解使用

@globaltransactional

扩展: 我用的是一个数据库,执行报错,说我缺少主键,于是我在表undo_log加了一个增的主键id,上面的创建undo_log表的sql是我加了id的,官方给的是没有id的。请知晓!!!!

Docker+nacos+seata1.3.0安装与使用配置教程

问题:单数源是有报错的,原因是因为我是一个数据库,需要设置代理配置如下 ,根据自己实际情况是指配置文件,不然会报错

#单数据源
seata.enable-auto-data-source-proxy=true
#多数据源
seata.enable-auto-data-source-proxy=false

到此这篇关于docker+nacos+seata1.3.0安装与使用的文章就介绍到这了,更多相关docker+nacos+seata安装使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!