說明
nacos版本:2.0.2
seata版本:1.4.2
mysql版本:8.0.25
1. nacos安裝配置
1.1 nacos代碼拉取
# 拉取最新的代碼
git clone https://github.com/alibaba/nacos.git --depth=1
# 獲取tag
git fetch --tags
# 切換到最新的tag
git checkout tag
# 打包
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
1.2 將打包好的文件夾復制到自己的安裝目錄並執行sql
# 進入taget目錄
cd (D:\srccode自己的目錄)\nacos\distribution\target
# 導入nacos-mysql.sql到自己的nacos庫
# 修改application.properties 取消下面行注釋
spring.datasource.platform=mysql
db.num=1
db.password.0=nacos# db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456
1.3. 啟動
進入bin目錄打開命令框
startup.bat -m standalone
2. seata安裝配置
2.1 seata代碼拉取與配置
# 拉取最新的代碼
git clone https://github.com/alibaba/seata.git --depth=1
# 獲取tag
git fetch --tags
# 切換到最新的tag
git checkout tag
# 打包
mvn -Prelease-seata -Dmaven.test.skip=true clean install -U
2.2 修改seata配置
file.conf
# 默認file,這里改成數據庫,根據自己的需要改
mode = "db"
# db節點下面修改用戶名密碼,其他介質改其他的
user = "root"
password = "123456"
registry.conf
# 注冊中心 registry
# 修改type然后修改對應的配置
type="nacos"
# nacos節點
serverAddr
username
password
# 配置中心
type = "nacos"
配置導入
導入seata配置到nacos
# 進入配置文件目錄
cd D:\srccode\seata\script\config-center\nacos
# git 命令框 自己根據情況修改好config.txt后導入配置
# 無需用戶名密碼
sh ./nacos-config.sh -h 127.0.0.1 -p 8848 ../config.txt
# 需要用戶名 密碼
sh ./nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -u nacos -w nacos ../config.txt
-h:服務器ip
-g:nacos組
-u:nacos用戶名
-w:nacos密碼
-t:租戶,默認''
注意 :如果seata配置文件registry.conf中含有dataId = "seataServer.properties",將config.txt中的配置復制到nacos的seataServer.properties配置中修改配置
seata啟動
# windows
seata-server.bat -h 127.0.0.1 -p 8091 -m db
# linxu
seata-server.sh -h 127.0.0.1 -p 8091 -m db
nacos路徑錯誤
application.properties添加
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
項目中需要添加的配置
seata.config.type=nacos
seata.config.nacos.data-id=seataServer.properties
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.username=nacos
seata.config.nacos.password=nacos
seata1.4.2較1.3及之前版本中的坑
1. nacos配置文件導入
1.3及之前的導入方式
D:\srccode\seata\script\config-center\nacos\nacos-config.sh
導入
D:\srccode\seata\script\config-center\nacos目錄的config.txt,該方式一條配置占用一個配置文件的位置
1.4.2
在nacos配置中創建seataServer.properties配置文件,將config.txt復制到seataServer.properties,然后修改數據庫相關配置
2. LocalDateTime坑
如果實體中有LocalDateTime類型字段,數據庫為datetime類型,數據庫使用了mysql8,同時jdbc驅動也是8.0,使用默認的jackson序列化方式,事務回滾會報序列化失敗的錯誤,解決方式
a. 修改seataServer.properties中的client.undo.logSerialization=kryo
b. 清空seata表中的反序列化錯誤的數據
c. 重啟seata服務
d. 項目中添加kryo依賴,版本與seata lib版本保持一致
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
</dependency>
參考:
https://baijiahao.baidu.com/s?id=1700872368390839990&wfr=spider&for=pc
https://blog.csdn.net/richie696/article/details/116896511
3. 沒有undo_log日志
@GlobalTransactional
// @Transactional(rollbackFor = Exception.class)
public void placeOrder(String userId, String commodityCode, Integer count) {
BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
Order order = new Order()
.setUserId(userId)
.setCommodityCode(commodityCode)
.setCount(count)
.setMoney(orderMoney);
orderMapper.insert(order);
storageFeignClient.deduct(commodityCode, count);
}
去掉事務發起者的@Transactional(rollbackFor = Exception.class)