一、版本
SpringBoot(2.1.6.RELEASE),SpringCloud(Greenwich.SR2),SpringCloudAlibaba(2.1.3.RELEASE),Nacos(1.2.1),Seata(1.2.0)
二、环境搭建
1、nacos安装
下载解压安装:https://github.com/alibaba/nacos/releases
运行:./bin/startup.sh -m standalone &
访问:http://localhost:8848/nacos/
2、安装seata
seata-service下载解压:https://seata.io/zh-cn/blog/download.html
seata源码:https://github.com/seata/seata/releases
undo_log表:在seata1.2
源码seata-1.2.0/script/client/at/db
目录下有提供针对mysql
、oracle
、postgresql
这三种数据库生成undo-log
逆向日志回滚表的表创建脚本,根据自身数据库类型来选择。
seata事务相关表:在seata1.2
源码seata-1.2.0/
seata-1.2.0/script/server/db目录下有提供针对mysql
、oracle
、postgresql
这三种数据库事务表创建脚本,根据自身数据库类型来选择。
2.1、创建seata数据库,并在该库下执行事务相关表的创建(global_table、branch_table、lock_table)
2.2、分别创建相关服务数据库
- 账户服务库:seata_account
CREATE TABLE `t_account` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
`total` decimal(10,0) DEFAULT NULL COMMENT '总额度',
`used` decimal(10,0) DEFAULT NULL COMMENT '已用额度',
`residue` decimal(10,0) DEFAULT NULL COMMENT '剩余可用额度',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8 COMMENT='账户';
- 订单服务库:seata_order
CREATE TABLE `t_order` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
`product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
`count` int(11) DEFAULT NULL COMMENT '数量',
`money` decimal(11,0) DEFAULT NULL COMMENT '金额',
`status` int(1) DEFAULT NULL COMMENT '订单状态 0:创建中 1:已完成',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=220 DEFAULT CHARSET=utf8 COMMENT='订单表';
- 库存服务库:seata_storage
CREATE TABLE `t_storage` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
`total` int(11) DEFAULT NULL COMMENT '总库存',
`used` int(11) DEFAULT NULL COMMENT '已用库存',
`residue` int(11) DEFAULT NULL COMMENT '剩余库存',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='库存表';
2.3、分别在业务库seata_account、seata_order、seata_storage下创建undo-log表
2.4、配置seata
- 配置
registry.conf文件中的registry和config模块改
为nacos
,配置nacos
相关属性参数
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "localhost:8848" # 上面安装的nacos访问地址
group = "SEATA_GROUP"
namespace = "public"
cluster = "default"
username = "nacos"
password = "nacos"
}
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 = "localhost:8848"
namespace = "public"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
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"
}
}
- 配置
file.conf文件中的
store模块改
为db
,配置db相关属性参数
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata" # 上面创建的seata事务相关数据库
user = "mysql"
password = "123456"
minConn = 5
maxConn = 30
globalTable = "global_table" # 上面创建的事务相关表
branchTable = "branch_table" # 上面创建的事务相关表
lockTable = "lock_table" # 上面创建的事务相关表
queryLimit = 100
maxWait = 5000
}
}
2.5、修改seata的配置提交到nacos,并提交到nacos
- 修改:/seata-1.2.0/script/config-center/config.txt 下的这些参数。config.txt提供的脚本是
seata-server
服务自身所需要的配置文件信息,我们把相关脚本提交到nacos中,然后方便在nacos控制台上进行修改变更了
service.vgroupMapping.my_test_tx_group=default
store.mode=db
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
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
- 运行:/seata-1.2.0/script/config-center/nacos/nacos-config.sh 将上面config.txt提交到
nacos
控制台,如果有需要修改参数,可直接通过登录nacos
控制台修改
./nacos-config.sh
或
./nacos-config.sh -h host -p port -g group
host: nacos的ip地址
port:nacos的端口
group:nacos配置中心的组
- 运行成功后可在nacos中的配置列表中看到相关配置: