前言
项目中项目订单需要创建订单,同时需要扣减库存和扣减金额的,当扣减库存方法体内出现异常时,若订单服务方法已执行,要求订单服务能够进行回滚,需要借助分布式事务实现。Seata是一个比较成熟的分布式事务工具,但官方文档比较简洁,查阅网上资料也版本较多不太统一,在此记录一下集成过程以供下次参考。
环境信息
操作系统 | window 11 |
---|---|
JDK版本 | 17.0.7 |
Spring Cloud Version | Spring Cloud 2022.0.0 |
Spring Boot Version | 3.0.2 |
Nacos Version | 2.2.1 |
Seata Version | 1.7.0 |
MySQL | 8.0 |
一、集成过程
1. 下载seata-server
GitHub下载地址:
https://github.com/seata/seata/releases/tag/v1.7.0
2. 修改配置文件
以seata-1.7.0为例,打开conf/application.yml进行修改,重点修改nacos部分配置。
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${
user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 192.168.3.46:8848
namespace: 60e9ab72-53ec-482b-a1c3-27ec8de33686
group: SEATA_GROUP
data-id: seataServer.properties
username: nacos
password: nacos
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 192.168.3.46:8848
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
store:
# support: file 、 db 、 redis
mode: file
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login
修改成功后,意味着seata将从nacos获取配置信息,同时注册自身服务到nacos中心。
3. nacos预备
注意到上面配置项中有一项:seata.config.data-id=seataServer.properties,意思为要读nacos上的seataServer.properties配置文件,接下来去Nacos创建该配置文件,注意Group与第2步中的保持一致,这里是SEATA_GROUP。
#Transaction storage configuration, only for the server.
store.mode=db
store.lock.mode=db
store.session.mode=db
#These configurations are required if the `store mode` is `db`.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useSSL=false&useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=root123
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
上述具体配置项是从seata-server-1.7.0/seata/s