Seata服务高可用+负载均衡搭建安装、快速启动

参考官方文档:

​http://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html​

描述

目前三台服务器,分别在 192.168.128.36、192.168.128.37、192.168.128.32 上,通过三台服务器达到高可用,使用spring cloud ribbon达到负载均衡。

高可用部署

1.高可用部署参照:

​http://seata.io/zh-cn/docs/ops/deploy-ha.html​

seata github项目地址

​https://github.com/seata/seata​

2.安装目录


​[root@localhost conf]# pwd/usr/local/seata1.3/seata/conf[root@localhost conf]# lsfile.conf file.conf.example logback.xml META-INF README.md README-zh.md registry.conf​


3.registry.conf配置如下:(file.conf不用管,因为用的nacos)


​registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "192.168.128.20" group = "SEATA_GROUP" namespace = "1c53b59e-9f3d-44a4-b2d7-5faaea25b3c6" cluster = "default" username = "" password = "" } eureka { serviceUrl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serverAddr = "localhost:6379" db = 0 password = "" cluster = "default" timeout = 0 } zk { cluster = "default" serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } consul { cluster = "default" serverAddr = "127.0.0.1:8500" } etcd3 { cluster = "default" serverAddr = "http://localhost:2379" } sofa { serverAddr = "127.0.0.1:9603" application = "default" region = "DEFAULT_ZONE" datacenter = "DefaultDataCenter" cluster = "default" group = "SEATA_GROUP" addressWaitTime = "3000" } file { name = "file.conf" }}config { # file、nacos 、apollo、zk、consul、etcd3 type = "nacos" nacos { serverAddr = "192.168.128.20" namespace = "aebcc8df-bcca-4f1d-8f27-fa98828753d7" group = "SEATA_GROUP" username = "" password = "" } consul { serverAddr = "127.0.0.1:8500" } apollo { appId = "seata-server" apolloMeta = "http://192.168.1.204:8801" namespace = "application" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" }}​


4.在mysql(​​官方db地址​​)分别创建global_table, branch_table, lock_table表。

5.在nacos添加namespace=seata




 


seata集群搭建_redis


image.png


 

6.导入seata配置文件,

步骤:

6.1修改nacos-config.sh脚本,以适用自己的nacos配置,​​官方配置​


​#!/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 ;; esacdoneif [[ -z ${host} ]]; then host=localhostfiif [[ -z ${port} ]]; then port=8849fiif [[ -z ${group} ]]; then group="SEATA_GROUP"fiif [[ -z ${tenant} ]]; then tenant=""fiif [[ -z ${username} ]]; then username=""fiif [[ -z ${password} ]]; then password=""finacosAddr=$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}"doneecho "========================================================================="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​


6.2修改config.txt文件,以适用自己的nacos配置,​​官方配置​

7.执行nacos-config.sh脚本


​sh nacos-config.sh -h 192.168.128.20 -p 8848 -g SEATA_GROUP -t aebcc8df-bcca-4f1d-8f27-fa98828753d7​




 


seata集群搭建_spring_02


image.png


 

执行完成以后即可看到nacos配置

 




 


seata集群搭建_d3_03


image.png


启动

1.进入bin目录下所示


​[root@localhost bin]# pwd/usr/local/seata1.3/seata/bin[root@localhost bin]# lsnohup.out seata-server.bat seata-server.sh​


2.三台机器分别执行如下命令:


​nohup sh ./seata-server.sh -p 8091 -h 192.168.128.36 -m db -n 3 &nohup sh ./seata-server.sh -p 8091 -h 192.168.128.37 -m db -n 2 &nohup sh ./seata-server.sh -p 8091 -h 192.168.128.32 -m db -n 1 &​


windows下配置和启动

1.windows启动命令(本机)

​.\seata-server.bat -p 28091 -m db -n 4​

  1. registry.conf配置

​registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "192.168.128.20" group = "SEATA_GROUP" namespace = "aebcc8df-bcca-4f1d-8f27-fa98828753d7" cluster = "default" username = "" password = "" } eureka { serviceUrl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serverAddr = "localhost:6379" db = 0 password = "" cluster = "default" timeout = 0 } zk { cluster = "default" serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } consul { cluster = "default" serverAddr = "127.0.0.1:8500" } etcd3 { cluster = "default" serverAddr = "http://localhost:2379" } sofa { serverAddr = "127.0.0.1:9603" application = "default" region = "DEFAULT_ZONE" datacenter = "DefaultDataCenter" cluster = "default" group = "SEATA_GROUP" addressWaitTime = "3000" } file { name = "file.conf" }}config { # file、nacos 、apollo、zk、consul、etcd3 type = "nacos" nacos { serverAddr = "192.168.128.20" namespace = "aebcc8df-bcca-4f1d-8f27-fa98828753d7" group = "SEATA_GROUP" username = "" password = "" } consul { serverAddr = "127.0.0.1:8500" } apollo { appId = "seata-server" apolloMeta = "http://192.168.1.204:8801" namespace = "application" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" }}​



seata集群搭建


 


LawsonJin 2020-09-09 12:01:02  1203  收藏


分类专栏: Java挑战20K-40K线路图


版权


seata1.3 nacos1.3.0配置中心


1.下载seata-dev-tools


 


​https://gitee.com/seata-io/seata​


 


2.下载seata1.3


 


3.cd script/config-center/nacos/


 


4.vim nacos-config.sh   44行    配置nacos的地址 、端口、账号密码


 


 


if [[ -z ${host} ]]; then


    host=nacos.baidu.com


fi


if [[ -z ${port} ]]; then


    port=80


fi


if [[ -z ${group} ]]; then


    group="SEATA_GROUP"


fi


if [[ -z ${tenant} ]]; then


    tenant=""


fi


if [[ -z ${username} ]]; then


    username="nacos"


fi


if [[ -z ${password} ]]; then


    password="hon20"


fi


5. sh nacos-config.sh  上传配置信息


 


6.unzip seata1.3  下载seata1.3解压


 


7.cd seata1.3/conf


 


8.vim file.conf    这里我使用db模式


 


 


## transaction log store, only used in seata-server


store {


  ## store mode: file、db、redis


  mode = "db"


 


 


 


  ## database store property


  db {


    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.


    datasource = "druid"


    ## mysql/oracle/postgresql/h2/oceanbase etc.


    dbType = "mysql"


    driverClassName = "com.mysql.jdbc.Driver"


    url = "jdbc:mysql://39.97.999.29:3306/seata"


    user = "root"


    password = "ho20"


    minConn = 5


    maxConn = 30


    globalTable = "global_table"


    branchTable = "branch_table"


    lockTable = "lock_table"


    queryLimit = 100


    maxWait = 5000


  }


 


service {


  vgroupMapping.storage-service-group= "default"


  vgroupMapping.order-service-group= "default"


  vgroupMapping.business-service-group= "default"


}


}


 


 


9.配置数据库文件 seata数据库


 


/*


 Navicat Premium Data Transfer


 Source Server         : OBD服务器


 Source Server Type    : MySQL


 Source Server Version : 50730


 Source Host           : 39.97.208.29:3306


 Source Schema         : seata


 Target Server Type    : MySQL


 Target Server Version : 50730


 File Encoding         : 65001


 Date: 09/09/2020 11:54:01


*/


 


SET NAMES utf8mb4;


SET FOREIGN_KEY_CHECKS = 0;


 


-- ----------------------------


-- Table structure for branch_table


-- ----------------------------


DROP TABLE IF EXISTS `branch_table`;


CREATE TABLE `branch_table` (


  `branch_id` bigint(20) NOT NULL,


  `xid` varchar(128) NOT NULL,


  `transaction_id` bigint(20) DEFAULT NULL,


  `resource_group_id` varchar(32) DEFAULT NULL,


  `resource_id` varchar(256) DEFAULT NULL,


  `branch_type` varchar(8) DEFAULT NULL,


  `status` tinyint(4) DEFAULT NULL,


  `client_id` varchar(64) DEFAULT NULL,


  `application_data` varchar(2000) DEFAULT NULL,


  `gmt_create` datetime(6) DEFAULT NULL,


  `gmt_modified` datetime(6) DEFAULT NULL,


  PRIMARY KEY (`branch_id`),


  KEY `idx_xid` (`xid`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;


 


-- ----------------------------


-- Table structure for global_table


-- ----------------------------


DROP TABLE IF EXISTS `global_table`;


CREATE TABLE `global_table` (


  `xid` varchar(128) NOT NULL,


  `transaction_id` bigint(20) DEFAULT NULL,


  `status` tinyint(4) NOT NULL,


  `application_id` varchar(32) DEFAULT NULL,


  `transaction_service_group` varchar(32) DEFAULT NULL,


  `transaction_name` varchar(128) DEFAULT NULL,


  `timeout` int(11) DEFAULT NULL,


  `begin_time` bigint(20) DEFAULT NULL,


  `application_data` varchar(2000) DEFAULT NULL,


  `gmt_create` datetime DEFAULT NULL,


  `gmt_modified` datetime DEFAULT NULL,


  PRIMARY KEY (`xid`),


  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),


  KEY `idx_transaction_id` (`transaction_id`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;


 


-- ----------------------------


-- Table structure for lock_table


-- ----------------------------


DROP TABLE IF EXISTS `lock_table`;


CREATE TABLE `lock_table` (


  `row_key` varchar(128) NOT NULL,


  `xid` varchar(96) DEFAULT NULL,


  `transaction_id` bigint(20) DEFAULT NULL,


  `branch_id` bigint(20) NOT NULL,


  `resource_id` varchar(256) DEFAULT NULL,


  `table_name` varchar(32) DEFAULT NULL,


  `pk` varchar(36) DEFAULT NULL,


  `gmt_create` datetime DEFAULT NULL,


  `gmt_modified` datetime DEFAULT NULL,


  PRIMARY KEY (`row_key`),


  KEY `idx_branch_id` (`branch_id`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;


 


SET FOREIGN_KEY_CHECKS = 1;


10.每个需要全局事务的数据库中,放一个undo_log的表


 


/*


 Navicat Premium Data Transfer


 Source Server         : OBD服务器


 Source Server Type    : MySQL


 Source Server Version : 50730


 Source Host           : 39.97.208.29:3306


 Source Schema         : seata-demo


 Target Server Type    : MySQL


 Target Server Version : 50730


 File Encoding         : 65001


 Date: 07/08/2020 10:15:13


*/


 


SET NAMES utf8mb4;


SET FOREIGN_KEY_CHECKS = 0;


 


-- ----------------------------


-- Table structure for undo_log


-- ----------------------------


DROP TABLE IF EXISTS `undo_log`;


CREATE TABLE `undo_log` (


  `id` bigint(20) NOT NULL AUTO_INCREMENT,


  `branch_id` bigint(20) NOT NULL,


  `xid` varchar(100) NOT NULL,


  `context` varchar(128) NOT NULL,


  `rollback_info` longblob NOT NULL,


  `log_status` int(11) NOT NULL,


  `log_created` datetime NOT NULL,


  `log_modified` datetime NOT NULL,


  `ext` varchar(100) DEFAULT NULL,


  PRIMARY KEY (`id`),


  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)


) ENGINE=InnoDB AUTO_INCREMENT=432 DEFAULT CHARSET=utf8;


 


SET FOREIGN_KEY_CHECKS = 1;


11.vim registry.conf


 


registry {


  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa


  type = "nacos"


 


  nacos {


    application = "seata-server"


    serverAddr = "nacos.baidu.com:80"


    group = "SEATA_GROUP"


    namespace = ""


    cluster = "default"


    username = "nacos"


    password = "ho20"


  }


}


 


config {


  # file、nacos 、apollo、zk、consul、etcd3


  type = "nacos"


 


  nacos {


    serverAddr = "nacos.baidu.com:80"


    namespace = ""


    group = "SEATA_GROUP"


    username = "nacos"


    password = "ho0"


  }


}


12. cd bin 


 


13.sh seata-server.sh -p 8091 -h 192.168.0.199 -m file &> seata.log &        -h如果是外网的话,请设置为外网IP,否则出错


 


14. 代码中配置seata      tips:需要在每个分布式事务的微服务中都配置


 


seata:


  tx-service-group: test_tx_service_group


  config:


    type: nacos


    nacos:


      serverAddr: ${spring.cloud.nacos.server-addr}


      group: SEATA_GROUP


      username: ${spring.cloud.nacos.username}


      password: ${spring.cloud.nacos.password}


  registry:


    type: nacos


    nacos:


      server-addr: ${spring.cloud.nacos.server-addr}


      group: SEATA_GROUP


      username: ${spring.cloud.nacos.username}


      password: ${spring.cloud.nacos.password}


15:在业务开始的地方添加注解@GlobalTransactional ,该注解下的所有增删改都会走分布式事务


 


16.seata常见问题 ​​https://seata.io/zh-cn/docs/overview/faq.html​


 


17.集群:在其他机器上,再开启一个seata,配置不变。


————————————————