SpringCloud+Nacos集成Seata-1.7.0分布式事务
要在Spring Cloud和Nacos中整合Seata 1.7.0进行分布式事务管理,你需要按照以下步骤操作:
- 安装Seata Server。
- 配置Seata Server。
- 在Spring Cloud应用中集成Seata。
以下是简化的步骤和示例配置:
1. 安装Seata Server
下载Seata Server:
wget https://github.com/seata/seata/releases/download/v1.7.0/seata-server-1.7.0.tar.gz
tar -zxvf seata-server-1.7.0.tar.gz
cd seata-server-1.7.0/
修改配置文件 conf/file.conf
和 conf/registry.conf
:
file.conf
配置事务日志和存储模式。registry.conf
配置Seata注册中心和配置中心,这里使用Nacos。
2. 配置Seata Server
在 file.conf
中配置:
store {
mode = "file"
file {
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
}
}
在 registry.conf
中配置:
registry {
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
cluster = "default"
}
}
config {
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
}
}
启动Seata Server:
sh bin/seata-server.sh -p 8091 -h 127.0.0.1
3. 在Spring Cloud应用中集成Seata
- 在项目的
pom.xml
中添加Seata依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
- 在
application.yml
或application.properties
中配置Seata:
spring:
cloud:
alibaba:
seata:
tx-service-group: my_tx_group
service:
vgroup-mapping:
my_tx_group: default
grouplist:
default: 127.0.0.1:8091
- 使用
@GlobalTransactional
注解标记你的分布式事务方法。
示例代码:
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.
评论已关闭