dynamic-datasource多数据源seata分布式事务dubbo微服务框架环境配置
dynamic-datasource 多数据源 seata 分布式事务 dubbo微服务框架环境配置
在开发中dubbo需要引⼊seata来进⾏分布式事务管理,查了很多⽂章,都是seata版本⽐较⽼旧等⽂章环境配起来很⿇烦,踩了不少坑,浪费了不少时间,记录⼀下。
⼀.安装seata服务端
transport {
# tcp udt unix-domain-socket
type="TCP"
#NIO NATIVE
server ="NIO"
#enable heartbeat
heartbeat =true
# the client batch send request enable
enableClientBatchSendRequest =true
#thread factory for netty
threadFactory {
bossThreadPrefix ="NettyBoss"
workerThreadPrefix ="NettyServerNIOWorker"
serverExecutorThread-prefix ="NettyServerBizHandler"
shareBossWorker =false
clientSelectorThreadPrefix ="NettyClientSelector"
clientSelectorThreadSize = 1
clientWorkerThreadPrefix ="NettyClientWorkerThread"
# netty boss thread size,will not be used for UDT
bossThreadSize = 1
#auto default pin or 8
workerThreadSize ="default"
}
validation框架shutdown{
# when destroy server, wait seconds
wait= 3
}
serialization ="seata"
compressor ="none"
}
service{
#transaction service group mapping
<_test_tx_group ="default"
#only support pe=file, please don't set multiple addresses
#degrade, current not support
enableDegrade =false
#disable seata
disableGlobalTransaction =false
}
client {
rm{
asyncCommitBufferLimit = 10000
lock {
retryInterval = 10
retryTimes = 30
retryPolicyBranchRollbackOnConflict =true
}
reportRetryCount = 5
tableMetaCheckEnable =false
reportSuccessEnable =false
}
tm {
commitRetryCount = 5
rollbackRetryCount = 5
}
undo {
dataValidation =true
logSerialization ="jackson"
logTable ="undo_log"
}
log {
exceptionRate = 100
}
}
2.然后修改f⽂件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type="zk"
nacos {
application ="seata-server"
serverAddr ="localhost"
namespace =""
username =""
password =""
}
eureka {
serviceUrl ="localhost:8761/eureka"
weight ="1"
}
redis {
serverAddr ="localhost:6379"
db ="0"
password =""
timeout="0"
}
zk {
serverAddr ="127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username =""
password =""
}
consul {
serverAddr ="127.0.0.1:8500"
}
etcd3 {
serverAddr ="localhost:2379"
}
sofa {
serverAddr ="127.0.0.1:9603"
region ="DEFAULT_ZONE"
datacenter ="DefaultDataCenter"
group ="SEATA_GROUP"
addressWaitTime ="3000"
}
file{
name ="f"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3、springCloudConfig type="file"
nacos {
serverAddr ="localhost"
namespace =""
group ="SEATA_GROUP"
username =""
password =""
}
consul {
serverAddr ="127.0.0.1:8500"
}
apollo {
appId ="seata-server"
apolloMeta ="192.168.1.204:8801"
namespace ="application"
}
zk {
serverAddr ="127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username =""
password =""
}
etcd3 {
serverAddr ="localhost:2379"
}
file{
name ="f"
}
}
type修改为你是⽤的注册中⼼(我这边⽤的zookeeper就修改为zk)
3.切换bin⽬录直接命令运⾏seata-server.sh(.bat)可执⾏⽂件,运⾏时保证注册中⼼已启动
4.新增seata所需的数据库表
DROP TABLE IF EXISTS`undo_log`;
CREATE TABLE`undo_log`(
`id`bigint(20)NOT NULL AUTO_INCREMENT,
`branch_id`bigint(20)NOT NULL,
`xid`varchar(100)NOT NULL,
`context`varchar(128)NOT NULL,
`rollback_info`longblob NOT NULL,
`log_status`int(11)NOT NULL,
`log_created`datetime NOT NULL,
`log_modified`datetime NOT NULL,
PRIMARY KEY(`id`),
UNIQUE KEY`ux_undo_log`(`xid`,`branch_id`)
)ENGINE=InnoDB AUTO_INCREMENT=10DEFAULT CHARSET=utf8;
⼆.在pom⽂件中引⼊seata包
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
这⾥引⼊seata和springboot的整合包。
dynamic-datasource
我这⾥已经引⼊过dynamic-datasource多数据源的等依赖了,这些百度有详细配置⾃⾏引⼊三.修改Springboot的yml配置
spring:
boot:
admin:
client:
url: 127.0.0.1:8180
instance:
prefer-ip:true
service-base-url: 127.0.0.1:${server.port}
datasource:
dynamic:
primary: db1 #设置默认的数据源或者数据源组,默认值即为master
strict:false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使⽤默认数据源.
seata:true #seata1.0之后⽀持⾃动代理这⾥直接配置true
seata-mode: at #seata模式使⽤的at
datasource:
db1:# 数据源1配置
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test1
username: root
password: root
db2:# 数据源2配置
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test2
username: root
password: root
seata:
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
my_test_tx_group: default
enable-degrade:false
disable-global-transaction:false
enabled:true
application-id: ${dubbo.application.id}#这⾥填你服务的id
tx-service-group: my_test_tx_group #这⾥填seata服务端 f⾥ service vgroupMapping.{分组名}
client:
tm:
commit-retry-count:3
rollback-retry-count:3
enable-auto-data-source-proxy:false
rm:
report-success-enable:true
table-meta-check-enable:true
report-retry-count:5
async-commit-buffer-limit:1000
transport:
type: TCP
server: NIO
heartbeat:true
serialization: seata
compressor: none
enable-auto-data-source-proxy:true
shutdown:
wait:3
thread-factory:
boss-thread-prefix: NettyBoss
client-worker-thread-prefix: NettyServerNIOWorker
server-executor-thread-prefix: NettyServerBizHandler
client-selector-thread-size:1
client-selector-thread-prefix: NettyClientWorkerThread
新版本的seata只需要把上⾯的yml⽂件复制到相应位置就可以了
四.开启分布式事务
@GlobalTransactional 开启分布式事务
@DS(“db2") 切换数据源名称“db2”的数据源
在开启的每个分布式事务的⽅法上标记的数据源,对应的每个数据库中⾥都需要有⼀张seata⽤于回滚的undo_log表,他会记录事务的信息
到对应数据源的表中

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。