使用SpringBoot和Seata实现Dubbo分布式事务管理

作者:じ☆ve宝贝

发布时间:2019-09-19T08:55:22

1.简介

本文主要介绍SpringBoot2.1.5 + Dubbo 2.7.3 + Mybatis 3.4.2 + Nacos 1.1.3 +Seata 0.8.0整合来实现Dubbo分布式事务管理,使用Nacos 作为 Dubbo和Seata的注册中心和配置中心,使用 MySQL 数据库和 MyBatis来操作数据。

如果你还对SpringBootDubboNacosSeataMybatis 不是很了解的话,这里我为大家整理个它们的官网网站,如下

  • SpringBoot:https://spring.io/projects/spring-boot

  • Dubbo:http://dubbo.apache.org/en-us/

  • Nacos:https://nacos.io/zh-cn/docs/quick-start.html

  • Seata:https://github.com/seata/seata/wiki/Home_Chinese

  • MyBatis:http://www.mybatis.org/mybatis-3/zh/index.html

在这里我们就不一个一个介绍它们是怎么使用和原理,详细请学习官方文档,在这里我将开始对它们进行整合,完成一个简单的案例,来让大家了解Seata来实现Dubbo分布式事务管理的基本流程。

2.环境准备

2.1 下载nacos并安装启动

nacos下载:https://github.com/alibaba/nacos/releases/tag/1.1.3

Nacos 快速入门:https://nacos.io/en-us/docs/quick-start.html

sh startup.sh -m standalone

在浏览器打开Nacos web 控制台:http://192.168.10.200:8848/nacos/index.html

输入nacos的账号和密码 分别为nacos:nacos

zhihou 后naocs 就正常启动了。

2.2 下载seata0.8.0 并安装启动

2.2.1 在 Seata Release 下载最新版的 Seata Server 并解压得到如下目录:

.├──bin├──conf├──file_store└──lib

2.2.2 修改 conf/registry.conf 配置,

目前seata支持如下的file、nacos 、apollo、zk、consul的注册中心和配置中心。这里我们以nacos 为例。将 type 改为 nacos

registry { # file nacos type = "nacos"
nacos { serverAddr = "192.168.10.200" namespace = "public" cluster = "default" } file { name = "file.conf" }}
config { # file、nacos 、apollo、zk、consul type = "nacos"
nacos { serverAddr = "192.168.10.200" namespace = "public" cluster = "default" }
file { name = "file.conf" }}
  • serverAddr = "192.168.10.200"   :nacos 的地址

  • namespace = "public" :nacos的命名空间默认为public

  • cluster = "default"  :集群设置未默认 default

2.2.3 修改 conf/nacos-config.txt配置

transport.type=TCPtransport.server=NIOtransport.heartbeat=truetransport.thread-factory.boss-thread-prefix=NettyBosstransport.thread-factory.worker-thread-prefix=NettyServerNIOWorkertransport.thread-factory.server-executor-thread-prefix=NettyServerBizHandlertransport.thread-factory.share-boss-worker=falsetransport.thread-factory.client-selector-thread-prefix=NettyClientSelectortransport.thread-factory.client-selector-thread-size=1transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThreadtransport.thread-factory.boss-thread-size=1transport.thread-factory.worker-thread-size=8transport.shutdown.wait=3service.vgroup_mapping.order-service-seata-service-group=defaultservice.vgroup_mapping.account-service-seata-service-group=defaultservice.vgroup_mapping.storage-service-seata-service-group=defaultservice.vgroup_mapping.business-service-seata-service-group=defaultservice.enableDegrade=falseservice.disable=falseservice.max.commit.retry.timeout=-1service.max.rollback.retry.timeout=-1client.async.commit.buffer.limit=10000client.lock.retry.internal=10client.lock.retry.times=30store.mode=dbstore.file.dir=file_store/datastore.file.max-branch-session-size=16384store.file.max-global-session-size=512store.file.file-write-buffer-cache-size=16384store.file.flush-disk-mode=asyncstore.file.session.reload.read_size=100store.db.driver-class-name=com.mysql.jdbc.Driverstore.db.datasource=dbcpstore.db.db-type=mysqlstore.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=truestore.db.user=lidongstore.db.password=cwj887766@@store.db.min-conn=1store.db.max-conn=3store.db.global.table=global_tablestore.db.branch.table=branch_tablestore.db.query-limit=100store.db.lock-table=lock_tablerecovery.committing-retry-period=1000recovery.asyn-committing-retry-period=1000recovery.rollbacking-retry-period=1000recovery.timeout-retry-period=1000transaction.undo.data.validation=truetransaction.undo.log.serialization=jacksontransaction.undo.log.save.days=7transaction.undo.log.delete.period=86400000transaction.undo.log.table=undo_logtransport.serialization=seatatransport.compressor=nonemetrics.enabled=falsemetrics.registry-type=compactmetrics.exporter-list=prometheusmetrics.exporter-prometheus-port=9898

这里主要修改了如下几项:

  • store.mode :存储模式 默认file  这里我修改为db 模式 ,并且需要两个表global_tablebranch_table

  • store.db.driver-class-name:默认没有,会报错。添加了 com.mysql.jdbc.Driver

  • store.db.datasource=dbcp :数据源 dbcp

  • store.db.db-type=mysql : 存储数据库的类型为mysql

  • store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true : 修改为自己的数据库urlport数据库名称

  • store.db.user=lidong :数据库的账号

  • store.db.password=cwj887766@@ :数据库的密码

  • service.vgroup_mapping.order-service-seata-service-group=default

  • service.vgroup_mapping.account-service-seata-service-group=default

  • service.vgroup_mapping.storage-service-seata-service-group=default

  • service.vgroup_mapping.business-service-seata-service-group=default

也可以在 Nacos 配置页面添加,data-id 为 service.vgroup_mapping.${YOUR_SERVICE_NAME}-seata-service-group, group 为 SEATA_GROUP, 如果不添加该配置,启动后会提示no available server to connect

注意: 配置文件末尾有空行,需要删除,否则会提示失败,尽管实际上是成功的

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(64) DEFAULT NULL, `transaction_service_group` varchar(64) DEFAULT NULL, `transaction_name` varchar(64) 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=utf8mb4;

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, `lock_key` varchar(128) DEFAULT NULL, `branch_type` varchar(8) DEFAULT NULL,