当前位置: 首页 > news >正文

springcloud第4季 分布式事务seata作用服务搭建1

一  seata作用

1.1 seata简介

1.seata是一款解决分布式事务的解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

1.2 seata的术语

一个中心:全局事务id,xid,在调用服务链路的上下文中进行传播。TC(Transaction Coordinator):事务协调者。其中seata服务器就是TC角色,负责维护全局和分支事务的状态,驱动全局事务的提交或回滚TC只有1个;

TM(Transaction Manager):事务管理者。标注了@GlobalTransaction注解的微服务模块(如订单模块);它是事物的发起者,负责定义事务的全局范围,根据TC维护全局事务和分支事务的状态,做出开启事务,提交事务,回滚事物的决议。TM只有1个

RM(Resource Manager): 资源管理器。Mysql数据库本身就是,负责管理分支事务的资源,向TC注册分支事务,汇报分支事务的状态,驱动分支事务的提交或者回滚。可以多个RM。

Tc以seata服务器形式独立部署。TM,RM是以seata client的形式集成在微服务中运行。

1.3 seata的工作流程

1.简述版

1.TM向TC申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的xid。

2.xid在微服务调用链路的上下文张工传播

3.RM向TC注册分支事务,将其纳入到xid对应的全局事务的管辖范围

4.TM向TC发起针对xid的全局提交或回滚决议。

5.TC调度xid管辖的全部分支事务,完成提交或回滚请求。

2.详情版:

1.服务A中的TM向TC申请开启一个全局事务,TC就会创建一个全局事务并返回一个唯一的XID。

2.服务A中的RM向TC注册分支事务,然后将这个分支事务纳入XID对应的全局事务管辖中。

3.服务A开始执行分支事务。

4.服务A开始远程调用B服务,此时XID会根据调用链传播

5.服务B中的RM也向TC注册分支事务,然后将这个分支事务纳入XID对应的全局事务管辖中。

6.服务B开始执行分支事务。

7.全局事务调用处理结束后,TM会根据有无异常情况,向TC发起全局事务的提交或回滚。

8.TC协调其管辖之下的所有分支事务,决定是提交还是回滚。

 二  seata服务端搭建 

2.1 seata搭建

2.2.1 seata 服务端下载安装

下载地址: Seata-Server下载 | Apache Seata

截图如下:

2.2.2 使用mysql初始化seata所需表

1.下载脚本地址: 

incubator-seata/script/server/db/mysql.sql at 2.x · apache/incubator-seata · GitHub

2.脚本内容

--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You 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.
---- -------------------------------- 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_status_gmt_modified` (`status` , `gmt_modified`),KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;-- 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 = InnoDBDEFAULT CHARSET = utf8mb4;-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(`row_key`        VARCHAR(128) NOT NULL,`xid`            VARCHAR(128),`transaction_id` BIGINT,`branch_id`      BIGINT       NOT NULL,`resource_id`    VARCHAR(256),`table_name`     VARCHAR(32),`pk`             VARCHAR(36),`status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',`gmt_create`     DATETIME,`gmt_modified`   DATETIME,PRIMARY KEY (`row_key`),KEY `idx_status` (`status`),KEY `idx_branch_id` (`branch_id`),KEY `idx_xid` (`xid`)
) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE IF NOT EXISTS `distributed_lock`
(`lock_key`       CHAR(20) NOT NULL,`lock_value`     VARCHAR(20) NOT NULL,`expire`         BIGINT,primary key (`lock_key`)
) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

  3.附加mysql数据库

 2.2.3 nacos的信息配置

1.创建namespace

2.创建dataid,groupid

3.具体seata-server.properties的内容 

# socket通信方式, 公共部分
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
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
transport.serialization=seata
transport.compressor=none
# 首先应用程序(客户端)中配置了事务分组,若应用程序是SpringBoot则通过配置seata.tx-service-group=[事务分组配置项]
# 事务群组,service.vgroupMapping.[事务分组配置项]=TC集群的名称
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false# undo配置
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h
# You can choose from the following options: fastjson, jackson, gson
tcc.contextJsonParserType=fastjson
#Log rule configuration, for client and server
log.exceptionRate=100
#事务会话信息存储方式
store.mode=db
#事务锁信息存储方式
store.lock.mode=db
#事务回话信息存储方式
store.session.mode=db
#db或redis存储密码解密公钥
#store.publicKey=
#存储方式为db
## druid数据源
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=cloudiip
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
# 事务规则配置
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
server.distributedLockExpireTime=10000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=true
server.enableParallelHandleBranch=false
server.raft.cluster=127.0.0.1:7091,127.0.0.1:7092,127.0.0.1:7093
server.raft.snapshotInterval=600
server.raft.applyBatch=32
server.raft.maxAppendBufferSize=262144
server.raft.maxReplicatorInflightMsgs=256
server.raft.disruptorBufferSize=16384
server.raft.electionTimeoutMs=2000
server.raft.reporterEnabled=false
server.raft.reporterInitialDelay=60
server.raft.serialization=jackson
server.raft.compressor=none
server.raft.sync=true
#Metrics配置
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

2.2.4 seata 服务端进行配置

1.application的配置,打开config/application.yml文件,我们主要关注以下几个模块的配置。

1)server端配置中心指定,seata.config.type

2)server端注册中心指定,seata.registry.type

3)server端存储模式指定,seata.store.mode

修改config/application.yml文件,指定nacos为配置中心、注册中心,并将seata.store.mode屏蔽,后续中配置中心中进行配置。

详情代码:

#  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.server:port: 7091spring:application:name: seata-server-zdylogging:config: classpath:logback-spring.xmlfile:path: ${log.home:${user.home}/logs/seata}extend:logstash-appender:destination: 127.0.0.1:4560kafka-appender:bootstrap-servers: 127.0.0.1:9092topic: logback_to_logstashconsole:user:username: seatapassword: seataseata:config:type: nacosnacos:server-addr: 127.0.0.1:8848namespace: c364e76c-e133-4bc0-951e-205cf732a19fgroup: LJF_GROUP #后续自己在nacos里面新建,不想新建SEATA_GROUP,就写DEFAULT_GROUPusername: nacospassword: nacosdata-id: seata-server.propertiesregistry:type: nacosnacos:application: seata-server-zdyserver-addr: 127.0.0.1:8848group: LJF_GROUP #后续自己在nacos里面新建,不想新建SEATA_GROUP,就写DEFAULT_GROUPnamespace: c364e76c-e133-4bc0-951e-205cf732a19fcluster: defaultusername: nacospassword: nacos    store:mode: dbdb:datasource: druiddb-type: mysqldriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/seata?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=trueuser: rootpassword: cloudiipmin-conn: 10max-conn: 100global-table: global_tablebranch-table: branch_tablelock-table: lock_tabledistributed-lock-table: distributed_lockquery-limit: 1000max-wait: 5000#  server:#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'security:secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017tokenValidityInMilliseconds: 1800000ignore:urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/metadata/v1/**

2.2.5 启动服务

1.启动nacos

E:\nacos-server2.2.3\bin>startup.cmd -m standalone

2.启动seata

F:\seata\bin>seata-server.bat

 2.2.6 查看服务

1.查看nacos

2.查看seata

http://localhost:7901

看到nacos中,seata服务完成了注册,以及能够登录到seata的查询页面。

相关文章:

  • Linux上快速定位Java代码问题行
  • 云桌面运维工程师
  • 大数据------JavaWeb------JSP(完整知识点汇总)
  • Spring Boot中的安全漏洞防护
  • Django靓号管理系统:实现用户列表功能
  • react 重新加载子组件
  • 目标检测入门:3.目标检测损失函数(IOU、GIOU、GIOU)
  • 【ubuntu】切换shell并显示git分支名字
  • Python学习笔记28:进阶篇(十七)常见标准库使用之质量控制中的代码质量与风格第二部分
  • 笔记-linux写文件的方法
  • 【FFmpeg】avcodec_open2函数
  • tls各个版本的安全性介绍
  • Appium启动APP时报错Security exception: Permission Denial
  • 高级Redis之Stream的用法示例
  • 同时在一台服务器上使用Java 8和Java 17的技术指南
  • 2017年终总结、随想
  • 2019.2.20 c++ 知识梳理
  • android图片蒙层
  • canvas 高仿 Apple Watch 表盘
  • CentOS从零开始部署Nodejs项目
  • ComponentOne 2017 V2版本正式发布
  • docker容器内的网络抓包
  • flutter的key在widget list的作用以及必要性
  • js
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • OSS Web直传 (文件图片)
  • Python - 闭包Closure
  • SOFAMosn配置模型
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Zsh 开发指南(第十四篇 文件读写)
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 基于webpack 的 vue 多页架构
  • 深度解析利用ES6进行Promise封装总结
  • 线性表及其算法(java实现)
  • 一道闭包题引发的思考
  • 用jQuery怎么做到前后端分离
  • No resource identifier found for attribute,RxJava之zip操作符
  • 数据可视化之下发图实践
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • # Maven错误Error executing Maven
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (+4)2.2UML建模图
  • (1)Android开发优化---------UI优化
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (8)STL算法之替换
  • (a /b)*c的值
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (差分)胡桃爱原石
  • (二)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (黑马C++)L06 重载与继承