目录
0101 can not connect to 127.0.0.1:8091 cause:can not register RM
can not get cluster name in registry config
Host 'host.docker.internal' is not allowed to connect to this MySQL server
com.alibaba.nacos.api.exception.NacosException: dataId invalid
一、前言
目标:使用docker容器化的seata-server服务连接windows本机的nacos,使windows本机的微服务注册到seata-server服务,并设置nacos为配置中心。
二、部署seata-server
1拉取seata-server镜像
docker pull seataio/seata-server:1.4.2
2配置registry.conf
registry.conf中config的type如果改为nacos则不需要再项目中引入file.conf了
registry {
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "192.168.1.11:8848"
group = "DEFAULT_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
type = "nacos"
file {
name = "file.conf"
}
nacos {
serverAddr = "192.168.1.11:8848"
namespace = ""
group = "DEFAULT_GROUP"
username = "nacos"
password = "nacos"
dataId = "seataServer.properties"
}
}
3配置seata-server服务端和客户端的表
store.session.mode=db时需要给服务端配置数据库地址以及数据表
branch_table
global_table
lock_table
undo_log
4nacos配置
service.vgroupMapping.aqua_tx_group=default
service.default.grouplist=localhost:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.lock.mode=db
store.session.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.11:3306/mydb?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=root
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
5启动seata-server容器
docker run --name seata-server \
-p 8091:8091 \
-e SEATA_CONFIG_NAME=file:/root/seata-config/registry \
-v D:/jar/seata-server-1.4.2/conf:/root/seata-config \
seataio/seata-server:1.4.2
挂载本地配置文件到docker容器 D:\jar\seata-server-1.4.2\conf
参考官网文档:https://seata.io/zh-cn/docs/ops/deploy-by-docker.html
根据容器名称删除容器(注意:不能在cmd中执行,请使用gitbash)
docker rm $(docker ps -a -q --filter name=seata-server) -f
启动成功:
三、启动微服务
1springboot配置
只展示关键配置
源码位置:demo-provider
maven依赖
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.4.2</version>
</dependency>
application.properties
seata.config.type=nacos设置了配置在nacos的DataId是seataServer.properties的配置中
#seata服务配置
#seata.service.grouplist.default=localhost:8091
#seata.service.vgroupMapping.aqua_tx_group=default
#注册中心
seata.registry.nacos.application=seata-server
seata.registry.nacos.cluster=default
seata.registry.nacos.group=DEFAULT_GROUP
#seata.registry.nacos.server-addr=192.168.1.11:8848
#配置中心
seata.config.type=nacos
seata.config.nacos.server-addr=127.0.0.1:8848
seata.config.nacos.group=DEFAULT_GROUP
seata.config.nacos.dataId=seataServer.properties
seata.config.nacos.username=nacos
seata.config.nacos.password=nacos
2启动springboot
3查看nacos
四、测试事务
事务提交
事务回滚
接口报错模拟业务异常,使事务回滚。(测试seata是否生效使用单机事务即可)
五、踩坑记录
注意使用本机ip
0101 can not connect to 127.0.0.1:8091 cause:can not register RM
原因:docker启动命令使用了--net=host会导致微服务报错,因为主机网络模式只在linux生效
can not get cluster name in registry config
application.properties增加配置
seata.service.vgroupMapping.aqua_tx_group=default
seata.tx-service-group=aqua_tx_group
Host 'host.docker.internal' is not allowed to connect to this MySQL server
开启mysql远程连接
参考:mysql远程连接
com.alibaba.nacos.api.exception.NacosException: dataId invalid
application.properties配置属性文件不能加双引号
错误示例
seata.config.nacos.dataId="seataServer.properties"
正确示例
seata.config.nacos.dataId=seataServer.properties