目录
前言:seata提供了提供了AT、TCC、SAGA 和 XA 事务模式,此环节配置的是AT模式。
1、nacos作为注册中心
1.1 本地nacos版本是1.2.0
2、seata
2.1 去官网下载seata-1.2.0版本:下载中心
2.2 下载完进入到conf目录修改配置文件:registry.conf、file.conf
2.2.1 registry.conf文件:
2.2.2 file.conf 文件
2.3 上传nacos配置
2.3.1 下载源码,进入script\config-center\nacos目录下执行 nacos-config.sh,结果如下:
2.3.2 启动nacos注册中心,可以查看注册进来的列表:
2.3.3 启动seata服务,进入到bin目录下,双击执行seata-server.bat
3、构建SpringCloud服务 ,服务micro-order ——>调用micro-web,验证分布式事物,重点是配置
3.1 服务micro-order
3.1.1 micro-order服务引入pom依赖
3.1.2 application.yml文件增加seata配置
3.1.3 bootstrap.properties文件
3.1.4 测试类Controller加分布式事物注解 @GlobalTransactional
3.2 服务micro-web
3.2.1 micro-web服务引入pom依赖
3.2.2 application.yml文件增加seata配置
3.2.3 bootstrap.properties文件
3.2.4 测试类Service,本服务ORM框架用的是MyBatisPlus
4、数据库表结构
4.1 seata必用的表结构可以去官网找,再找到undo_log表
4.2 测试表结构
5、验证结果
5.1 启动服务micro-order、micro-web,seata窗口显示类似信息
5.2 登录nacos地址,micro-order、micro-web、seata已经注册进去
5.3 访问服务接口http://127.0.0.1:18084/dxxx/updateById
AT模式:基于支持本地的ACID事物的关系型数据库,Java应用,通过JDBC访问数据库。
*AT模式分为两阶段提交:
阶段一:在同一本地事物中提交业务数据和回滚日志,然后释放本地锁和连接资源。
阶段二:对于提交案例,异步的快速完成工作;对于回滚情况,根据阶段一创建的回滚日志进行补偿。
*TCC模式:最终一致的分阶段事务模式,有业务进入 ;
*SAGA模式:长事务模式,有业务侵入;
*XA模式:强一致性分阶段事务模式,牺牲了一定的可用性,屋业务侵入。
注意:① type = "nacos",nacos作为注册中心写nacos,eureka作为注册中心写eureka;
②serverAddr = "localhost:8848" ,没有修改nacos端口号,可以这么写“serverAddr = "localhost”;
③ config标签中的type = "nacos",也要修改,参照“注意①”
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "localhost:8848"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://localhost:18763/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 = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
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"
}
}
例:
注意:store标签后的mode = "db"要修改成对应的,用文件形式配置file,数据库形式配置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?serverTimezone=UTC"
user = "root"
password = "123456"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
例:
运行结果:
org.springframework.boot
spring-boot-starter-web
${spring-boot-version}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.0.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.0.RELEASE
org.projectlombok
lombok
1.18.10
true
com.xiangxue.geo
micro-web
1.0-SNAPSHOT
com.alibaba.cloud
spring-cloud-starter-alibaba-seata
2.1.3.RELEASE
io.seata
seata-spring-boot-starter
io.seata
seata-spring-boot-starter
1.2.0
seata:
enabled: true
application-id: micro-order
tx-service-group: my_test_tx_group
enable-auto-data-source-proxy: true
use-jdk-proxy: false
config:
type: nacos
nacos:
namespace:
serverAddr: 127.0.0.1:8848
group: SEATA_GROUP
userName: ""
password: ""
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace:
userName: ""
password: ""
spring.application.name=micro-order
server.port=18084
#20211104
#eureka.client.serviceUrl.defaultZone=http\://localhost\:18763/eureka/
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
seata:
enabled: true
application-id: micro-web
tx-service-group: my_test_tx_group
enable-auto-data-source-proxy: true
use-jdk-proxy: true
config:
type: nacos
nacos:
namespace:
serverAddr: 127.0.0.1:8848
group: SEATA_GROUP
userName: ""
password: ""
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace:
userName: ""
password: ""
spring.application.name=micro-web
server.port=18083
#eureka.client.serviceUrl.defaultZone=http\://localhost\:18763/eureka/
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#当前微服务去nacos上拉去配置必须加
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
mybatis-plus.type-aliases-package=com.xiangxue..entity
service层有
System.out.println(1/0); 所以数据更新失败,seata起作用,去掉输出语句数据更新成功。