作者:じ☆ve宝贝
发布时间:2019-09-19T08:55:22
本文主要介绍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来操作数据。 如果你还对 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 在这里我们就不一个一个介绍它们是怎么使用和原理,详细请学习官方文档,在这里我将开始对它们进行整合,完成一个简单的案例,来让大家了解 nacos下载:https://github.com/alibaba/nacos/releases/tag/1.1.3 Nacos 快速入门:https://nacos.io/en-us/docs/quick-start.html 在浏览器打开Nacos web 控制台:http://192.168.10.200:8848/nacos/index.html 输入nacos的账号和密码 分别为nacos:nacos 目前seata支持如下的file、nacos 、apollo、zk、consul的注册中心和配置中心。这里我们以 serverAddr = "192.168.10.200" :nacos 的地址 namespace = "public" :nacos的命名空间默认为 cluster = "default" :集群设置未默认 这里主要修改了如下几项: store.mode :存储模式 默认file 这里我修改为db 模式 ,并且需要两个表 store.db.driver-class-name:默认没有,会报错。添加了 store.db.datasource=dbcp :数据源 dbcp store.db.db-type=mysql : 存储数据库的类型为 store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true : 修改为自己的数据库 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 注意: 配置文件末尾有空行,需要删除,否则会提示失败,尽管实际上是成功的1.简介
SpringBoot
、Dubbo
、Nacos
、Seata
、Mybatis
不是很了解的话,这里我为大家整理个它们的官网网站,如下
Seata
来实现Dubbo
分布式事务管理的基本流程。2.环境准备
2.1 下载nacos并安装启动
sh startup.sh -m standalone
这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 配置,
nacos
为例。将 type 改为 nacosregistry {
# 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"
}
}
public
default
2.2.3 修改 conf/nacos-config.txt配置
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.thread-factory.boss-thread-prefix=NettyBoss
transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
transport.thread-factory.share-boss-worker=false
transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
transport.thread-factory.client-selector-thread-size=1
transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
transport.thread-factory.boss-thread-size=1
transport.thread-factory.worker-thread-size=8
transport.shutdown.wait=3
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
service.enableDegrade=false
service.disable=false
service.max.commit.retry.timeout=-1
service.max.rollback.retry.timeout=-1
client.async.commit.buffer.limit=10000
client.lock.retry.internal=10
client.lock.retry.times=30
store.mode=db
store.file.dir=file_store/data
store.file.max-branch-session-size=16384
store.file.max-global-session-size=512
store.file.file-write-buffer-cache-size=16384
store.file.flush-disk-mode=async
store.file.session.reload.read_size=100
store.db.driver-class-name=com.mysql.jdbc.Driver
store.db.datasource=dbcp
store.db.db-type=mysql
store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true
store.db.user=lidong
store.db.password=cwj887766@@
store.db.min-conn=1
store.db.max-conn=3
store.db.global.table=global_table
store.db.branch.table=branch_table
store.db.query-limit=100
store.db.lock-table=lock_table
recovery.committing-retry-period=1000
recovery.asyn-committing-retry-period=1000
recovery.rollbacking-retry-period=1000
recovery.timeout-retry-period=1000
transaction.undo.data.validation=true
transaction.undo.log.serialization=jackson
transaction.undo.log.save.days=7
transaction.undo.log.delete.period=86400000
transaction.undo.log.table=undo_log
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registry-type=compact
metrics.exporter-list=prometheus
metrics.exporter-prometheus-port=9898
global_table
和branch_table
com.mysql.jdbc.Driver
mysql
url
、port
、数据库名称
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,