看了好多文章,启动项目一直有问题,比如空指针,string 转 bool类型失败、找不到对应服务等等,最后终于成功
项目基本的搭建很简单,网上很多文章,这里就不提了
项目基于postgresql,同样适用于mysql,两种都调试通过
1、下载seata-server1.4.0,注意不要放到有空格的目录
2、配置config.txt
导入命令:sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 70f61dc8-f57b-44bd-a220-d62bdb478904 -u nacos -w nacos
注意:好多地方defalut DEFAULT的问题,我确实前期也存在这个问题,后面跑通之后两个都分别试了下,仍然是正常的,也不知道之前到底是为啥,估计还是代码配置的问题
service.vgroupMapping.service-order-group=defalut
service.vgroupMapping.service-storage-group=defalut
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.db.datasource=druid
store.db.dbType=postgresql
store.db.driverClassName=org.postgresql.Driver
store.db.url=jdbc:postgresql://localhost:5432/SEATA
store.db.user=postgres
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
3、配置file.conf
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "db"
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "postgresql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:postgresql://localhost:5432/SEATA"
user = "postgres"
password = "123456"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
4、配置register.conf
注意:好多地方defalut DEFAULT的问题,我确实前期也存在这个问题,后面跑通之后两个都分别试了下,仍然是正常的,也不知道之前到底是为啥,估计还是代码配置的问题
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "serverAddr"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = "70f61dc8-f57b-44bd-a220-d62bdb478904"
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = "70f61dc8-f57b-44bd-a220-d62bdb478904"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
5、微服务中调用
5.1 pom中直接引用
#使用下面依赖,不需要配置数据库
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
5.2 application.properties中配置
#只有这两个是必须的,其他的在server端配置
#注意:需要在使用事务的两个微服务里分别配置如下代码,否则分布式事务无效
#千万不要配置 seata.config.type和seata.register.type,否则找问题让你怀疑人生
seata.tx-service-group=service-order-group
seata.service.vgroup-mapping.service-order-group=default
5.3 服务间调用
#注意:需要在使用事务的两个微服务里分别配置如下代码,否则分布式事务无效
#在controller或service对应的方法前使用
@GlobalTransactional