赞
踩
“本文主要介绍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
、Dubbo
、Nacos
、Seata
、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
在这里我们就不一个一个介绍它们是怎么使用和原理,详细请学习官方文档,在这里我将开始对它们进行整合,完成一个简单的案例,来让大家了解Seata
来实现Dubbo
分布式事务管理的基本流程。
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
- 1
在浏览器打开Nacos web 控制台:http://192.168.10.200:8848/nacos/index.html
输入nacos的账号和密码 分别为nacos:nacos
这是时候naocs 就正常启动了。
- .
- ├──bin
- ├──conf
- ├──file_store
- └──lib
- 12345
目前seata支持如下的file、nacos 、apollo、zk、consul的注册中心和配置中心。这里我们以nacos
为例。将 type 改为 nacos
- registry {
- # file nacos
- type = "nacos"
-
- nacos {
- serverAddr = "192.168.10.200:8848"
- namespace = "public"
- cluster = "default"
- }
- file {
- name = "file.conf"
- }
- }
-
- config {
- # file、nacos 、apollo、zk、consul
- type = "nacos"
-
- nacos {
- serverAddr = "192.168.10.200:8848"
- namespace = "public"
- cluster = "default"
- }
-
- file {
- name = "file.conf"
- }
- }
-
- 1234567891011121314151617181920212223242526272829
serverAddr = “192.168.10.200:8848” :nacos 的地址
namespace = “public” :nacos的命名空间默认为public
cluster = “default” :集群设置未默认 default
注意:seata0.9.0之后,配置如下, 其中namespace = ""
- registry {
- # file nacos
- type = "nacos"
-
- nacos {
- serverAddr = "192.168.10.200:8848"
- namespace = ""
- cluster = "default"
- }
- file {
- name = "file.conf"
- }
- }
-
- config {
- # file、nacos 、apollo、zk、consul
- type = "nacos"
-
- nacos {
- serverAddr = "192.168.10.200:8848"
- namespace = ""
- }
-
- file {
- name = "file.conf"
- }
- }
- 123456789101112131415161718192021222324252627
注意:如果你的seata是1.1之后的版本请按照如下导入方式操作
下载https://github.com/seata/seata/blob/develop/script/config-center/目录下的config.txt
文件放入到seata-server
目录下的conf中,可更改文件名为nacos-config.txt
。
下载https://github.com/seata/seata/tree/develop/script/config-center/nacos/目录下的nacos-config.py文件(window可用python命令执行,linux也可执行,前提先有安装python)或者nacos-config.sh文件(window下可放在git命令行执行,linux可执行文件)
- 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
- service.default.grouplist=127.0.0.1:8091
- 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
这里主要修改了如下几项:
store.mode :存储模式 默认file 这里我修改为db 模式 ,并且需要三个表global_table
、branch_table
和lock_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 : 修改为自己的数据库url
、port
、数据库名称
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 to services-server
注意: 配置文件末尾有空行,需要删除,否则会提示失败,尽管实际上是成功的
db模式下的所需的三个表的数据库脚本位于seata\conf\db_store.sql
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;
-
- 1234567891011121314151617
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,
- `status` tinyint(4) DEFAULT NULL,
- `client_id` varchar(64) DEFAULT NULL,
- `application_data` varchar(2000) DEFAULT NULL,
- `gmt_create` datetime DEFAULT NULL,
- `gmt_modified` datetime DEFAULT NULL,
- PRIMARY KEY (`branch_id`),
- KEY `idx_xid` (`xid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-
- 123456789101112131415161718
lock_table
的表结构
- create table `lock_table` (
- `row_key` varchar(128) not null,
- `xid` varchar(96),
- `transaction_id` long ,
- `branch_id` long,
- `resource_id` varchar(256) ,
- `table_name` varchar(32) ,
- `pk` varchar(32) ,
- `gmt_create` datetime ,
- `gmt_modified` datetime,
- primary key(`row_key`)
- );
- 123456789101112
使用命令如下
- cd conf
- sh nacos-config.sh localhost
- 12
成功后会提示
- init nacos config finished, please start seata-server
- 1
在 Nacos 管理页面应该可以看到有 59 个 Group 为SEATA_GROUP的配置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。