Seata(1.6.1)实战安装部署与Spring Cloud应用
Seata的安装和配置涉及以下步骤:
- 下载并解压Seata:
wget https://github.com/seata/seata/releases/download/v1.6.1/seata-server-1.6.1.tar.gz
tar -zxvf seata-server-1.6.1.tar.gz
cd seata-server-1.6.1/
- 修改配置文件
conf/file.conf
和conf/registry.conf
:
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 = "file"
file {
name = "file.conf"
}
}
config {
type = "file"
file {
name = "file.conf"
}
}
- 启动Seata Server:
sh bin/seata-server.sh -p 8091 -h 127.0.0.1 -m file
在Spring Cloud应用中使用Seata进行分布式事务管理,需要做以下操作:
- 在项目的
pom.xml
中添加Seata的依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
- 在
application.yml
或application.properties
中配置Seata客户端:
spring:
cloud:
alibaba:
seata:
tx-service-group: my_tx_group
service:
grouplist:
default: 127.0.0.1:8091
- 在业务代码中使用
@GlobalTransactional
注解标记全局事务:
import io.seata.spring.annotation.GlobalTransactional;
@Service
public class BusinessService {
@GlobalTransactional
public void doBusiness() {
// 执行业务操作,如:
// 1. 调用本地服务
// 2. 调用远程服务
// ...
}
}
确保Seata Server和应用服务器的网络连接正常,并且Seata Server的版本与客户端依赖版本兼容。以上步骤提供了Seata的基本安装和配置,以及如何在Spring Cloud应用中集成Seata进行分布式事务管理。
评论已关闭