seata结合nacos示例及序列化问题处理

说明

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配置官网

注意 :如果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

seata启动成功


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)

代码

posted @ 2021-07-25 22:27  学无终  阅读(1590)  评论(0编辑  收藏  举报