seata单个环境多实例的示例
时间: 2023-10-28 18:04:47 浏览: 36
假设我们将seata应用程序部署在3个不同的实例上,每个实例都在不同的端口上运行。
1. 配置文件
在每个实例的seata配置文件中,需要设置以下参数:
- serverNode.ip:设置seata服务端节点的IP地址,这里应该是相同的,因为它们是在同一个环境中运行的。
- serverNode.port:设置seata服务端节点的端口号,这里应该是不同的,因为每个实例都在不同的端口上运行。
- service.vgroupMapping.{groupName}:将seata服务组名映射到实际的服务名,这里我们将它们都设置为default。
比如,假设我们有3个实例,它们的端口分别是8091、8092、8093,那么它们的配置文件应该如下:
实例1:
```conf
## registry
registry.type = "file"
registry.file.name = "file.conf"
## file
file.conf = "file:/path/to/conf/nacos.conf"
## nacos
nacos.serverAddr = "localhost:8848"
nacos.namespace = ""
nacos.group = "SEATA_GROUP"
nacos.username = ""
nacos.password = ""
## service
service.vgroupMapping.default = "default"
## enablePessimisticLockFallback
enablePessimisticLockFallback = true
## server
server.recoverDelayInMills = 1000
server.maxCommitRetryTimeoutInMills = 120000
server.maxRollbackRetryTimeoutInMills = 120000
server.rollbackRetryTimeoutUnlockEnable = true
server.transport.server = "netty"
server.transport.codec = "seata"
server.transport.heartbeat = "true"
server.transport.enableClientBatchSendRequest = "true"
server.shutdown.wait = "3"
## client
client.rm.asyncCommitBufferLimit = 10000
client.rm.lock.retryInterval = 10
client.rm.lock.retryTimes = 30
client.rm.reportRetryCount = 5
client.rm.tableMetaCheckEnable = true
client.rm.reportSuccessEnable = false
client.tm.commitRetryCount = 5
client.tm.rollbackRetryCount = 5
client.tm.defaultGlobalTransactionTimeout = 600000
client.tm.degradeCheckAllowTimes = 10
client.tm.degradeCheckPeriod = 2000
client.tm.degradeCheck = true
client.tm.disableGlobalTransaction = false
client.tm.expirationScannerInterval = 60000
client.tm.globalTransactionTimeout = 600000
client.tm.highPerformanceMode = false
client.tm.idGeneratorType = "SIMPLE"
client.tm.rollbackRetryTimeout = 120000
client.tm.tableMetaCheckEnable = true
client.tm.reportRetryCount = 5
client.tm.reportSuccessEnable = true
client.tm.committingRetryPeriod = 1000
client.tm.rollbackingRetryPeriod = 1000
client.tm.timeoutRetryPeriod = 1000
client.rm.disableGlobalTransaction = false
client.rm.datasource.autoproxy = true
## serverNode
serverNode.ip = "127.0.0.1"
serverNode.port = "8091"
```
实例2:
```conf
## registry
registry.type = "file"
registry.file.name = "file.conf"
## file
file.conf = "file:/path/to/conf/nacos.conf"
## nacos
nacos.serverAddr = "localhost:8848"
nacos.namespace = ""
nacos.group = "SEATA_GROUP"
nacos.username = ""
nacos.password = ""
## service
service.vgroupMapping.default = "default"
## enablePessimisticLockFallback
enablePessimisticLockFallback = true
## server
server.recoverDelayInMills = 1000
server.maxCommitRetryTimeoutInMills = 120000
server.maxRollbackRetryTimeoutInMills = 120000
server.rollbackRetryTimeoutUnlockEnable = true
server.transport.server = "netty"
server.transport.codec = "seata"
server.transport.heartbeat = "true"
server.transport.enableClientBatchSendRequest = "true"
server.shutdown.wait = "3"
## client
client.rm.asyncCommitBufferLimit = 10000
client.rm.lock.retryInterval = 10
client.rm.lock.retryTimes = 30
client.rm.reportRetryCount = 5
client.rm.tableMetaCheckEnable = true
client.rm.reportSuccessEnable = false
client.tm.commitRetryCount = 5
client.tm.rollbackRetryCount = 5
client.tm.defaultGlobalTransactionTimeout = 600000
client.tm.degradeCheckAllowTimes = 10
client.tm.degradeCheckPeriod = 2000
client.tm.degradeCheck = true
client.tm.disableGlobalTransaction = false
client.tm.expirationScannerInterval = 60000
client.tm.globalTransactionTimeout = 600000
client.tm.highPerformanceMode = false
client.tm.idGeneratorType = "SIMPLE"
client.tm.rollbackRetryTimeout = 120000
client.tm.tableMetaCheckEnable = true
client.tm.reportRetryCount = 5
client.tm.reportSuccessEnable = true
client.tm.committingRetryPeriod = 1000
client.tm.rollbackingRetryPeriod = 1000
client.tm.timeoutRetryPeriod = 1000
client.rm.disableGlobalTransaction = false
client.rm.datasource.autoproxy = true
## serverNode
serverNode.ip = "127.0.0.1"
serverNode.port = "8092"
```
实例3:
```conf
## registry
registry.type = "file"
registry.file.name = "file.conf"
## file
file.conf = "file:/path/to/conf/nacos.conf"
## nacos
nacos.serverAddr = "localhost:8848"
nacos.namespace = ""
nacos.group = "SEATA_GROUP"
nacos.username = ""
nacos.password = ""
## service
service.vgroupMapping.default = "default"
## enablePessimisticLockFallback
enablePessimisticLockFallback = true
## server
server.recoverDelayInMills = 1000
server.maxCommitRetryTimeoutInMills = 120000
server.maxRollbackRetryTimeoutInMills = 120000
server.rollbackRetryTimeoutUnlockEnable = true
server.transport.server = "netty"
server.transport.codec = "seata"
server.transport.heartbeat = "true"
server.transport.enableClientBatchSendRequest = "true"
server.shutdown.wait = "3"
## client
client.rm.asyncCommitBufferLimit = 10000
client.rm.lock.retryInterval = 10
client.rm.lock.retryTimes = 30
client.rm.reportRetryCount = 5
client.rm.tableMetaCheckEnable = true
client.rm.reportSuccessEnable = false
client.tm.commitRetryCount = 5
client.tm.rollbackRetryCount = 5
client.tm.defaultGlobalTransactionTimeout = 600000
client.tm.degradeCheckAllowTimes = 10
client.tm.degradeCheckPeriod = 2000
client.tm.degradeCheck = true
client.tm.disableGlobalTransaction = false
client.tm.expirationScannerInterval = 60000
client.tm.globalTransactionTimeout = 600000
client.tm.highPerformanceMode = false
client.tm.idGeneratorType = "SIMPLE"
client.tm.rollbackRetryTimeout = 120000
client.tm.tableMetaCheckEnable = true
client.tm.reportRetryCount = 5
client.tm.reportSuccessEnable = true
client.tm.committingRetryPeriod = 1000
client.tm.rollbackingRetryPeriod = 1000
client.tm.timeoutRetryPeriod = 1000
client.rm.disableGlobalTransaction = false
client.rm.datasource.autoproxy = true
## serverNode
serverNode.ip = "127.0.0.1"
serverNode.port = "8093"
```
2. 启动实例
在每个实例中启动seata服务。
```
bin/seata-server.sh -p 8091 -h 127.0.0.1 -m db
bin/seata-server.sh -p 8092 -h 127.0.0.1 -m db
bin/seata-server.sh -p 8093 -h 127.0.0.1 -m db
```
3. 配置应用程序
在应用程序中,需要配置seata数据源和事务管理器,以及在每个需要事务支持的方法上添加@GlobalTransactional注解。
```java
@Configuration
public class SeataConfiguration {
@Bean
public DataSourceProxy dataSourceProxy(DataSource dataSource) {
return new DataSourceProxy(dataSource);
}
@Bean
public GlobalTransactionScanner globalTransactionScanner() {
return new GlobalTransactionScanner("my-app", "default");
}
}
@Service
public class MyService {
@Autowired
private MyMapper myMapper;
@GlobalTransactional
public void doSomething() {
myMapper.insertRecord();
}
}
```