erp-java/seata/README.md

187 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Seata 分布式事务配置
## 概述
本目录包含Seata分布式事务的完整配置和示例代码用于ERP系统的分布式事务管理。
### 技术栈
- Seata Server 1.8.0
- Spring Cloud Alibaba Seata
- Docker Compose
- MySQL 8.0
- Prometheus + Grafana 监控
## 目录结构
```
seata/
├── server/ # Seata Server部署配置
│ ├── docker-compose.yml # Docker Compose配置
│ └── config/ # Seata配置文件
│ ├── registry.conf # 注册中心配置
│ └── file.conf # 存储和客户端配置
├── client/ # Seata客户端配置
│ ├── seata.yml # 通用Seata配置
│ ├── order-service/ # 订单服务配置
│ ├── inventory-service/ # 库存服务配置
│ └── finance-service/ # 财务服务配置
├── sql/ # 数据库脚本
│ ├── store.sql # Seata存储库表
│ ├── undo_log.sql # AT模式undo_log表
│ └── business_tables.sql # 业务表结构
├── example/ # 分布式事务示例
│ ├── GlobalTransactionalExample.java # 全局事务示例
│ ├── OrderService.java # 订单服务
│ ├── InventoryService.java # 库存服务
│ ├── FinanceService.java # 财务服务
│ ├── entity/ # 实体类
│ └── mapper/ # Mapper接口
└── monitoring/ # 监控配置
├── prometheus.yml # Prometheus配置
└── grafana/ # Grafana配置
```
## 快速开始
### 1. 启动Seata Server
```bash
cd /root/.openclaw/workspace/erp-java-backend/seata/server
docker-compose up -d
```
启动后服务端口:
- Seata Server: 8091 (事务处理), 7091 (控制台)
- Seata MySQL: 3306
- Prometheus: 9090
- Grafana: 3000 (admin/admin123)
### 2. 初始化数据库
```bash
# 创建Seata存储库
mysql -h localhost -u root -proot123 < ../sql/store.sql
# 在业务库创建undo_log表
mysql -h localhost -u root -proot123 order_db < ../sql/undo_log.sql
mysql -h localhost -u root -proot123 inventory_db < ../sql/undo_log.sql
mysql -h localhost -u root -proot123 finance_db < ../sql/undo_log.sql
# 创建业务表
mysql -h localhost -u root -proot123 < ../sql/business_tables.sql
```
### 3. 配置微服务
在各微服务的 `application.yml` 中添加Seata配置
```yaml
seata:
enabled: true
tx-service-group: default_tx_group
service:
vgroup-mapping:
default_tx_group: default
grouplist:
default: 127.0.0.1:8091
```
### 4. 使用分布式事务
在需要分布式事务的方法上添加 `@GlobalTransactional` 注解:
```java
@GlobalTransactional(name = "create-order-transaction", timeoutMills = 30000, rollbackFor = Exception.class)
public String createOrder(Long customerId, Long productId, Integer quantity) {
// 创建订单
String orderNo = orderService.createOrder(customerId, productId, quantity);
// 扣减库存
inventoryService.reserveStock(productId, quantity, orderNo);
// 扣减余额
financeService.freezeAmount(customerId, amount, orderNo);
return orderNo;
}
```
## AT模式原理
1. **分支事务注册**微服务在执行本地事务前向TC注册分支事务
2. **undo_log记录**在执行SQL前记录数据前后镜像
3. **本地事务提交**SQL与undo_log在同一事务中提交
4. **全局事务完成**TM通知TC提交/回滚全局事务
5. **分支回滚/删除**TC驱动各分支根据undo_log回滚或删除undo_log
## 监控
### Prometheus指标
- `seata_server_transactions_total` - 事务总数
- `seata_server_branch_transaction_total` - 分支事务总数
- `seata_server_lock_keys_count` - 全局锁数量
- `seata_server_heartbeat_timestamp` - 心跳时间戳
### Grafana Dashboard
访问 http://localhost:3000 查看Seata监控面板
## 高可用配置
### 集群模式
修改 `docker-compose.yml` 中的 `SEATA_SERVER_MODE`
```yaml
SEATA_SERVER_MODE: cluster
```
### 外部注册中心
修改 `config/registry.conf` 使用Nacos/ZK等
```conf
registry {
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
cluster = "default"
}
}
```
## 故障恢复
### 查看事务状态
```bash
curl http://localhost:7091/v1/transaction/global/query?xid=your-xid
```
### 强制回滚
```bash
curl -X PUT http://localhost:7091/v1/transaction/global/rollback -d '{"xid":"your-xid"}'
```
### 强制提交
```bash
curl -X PUT http://localhost:7091/v1/transaction/global/commit -d '{"xid":"your-xid"}'
```
## 常见问题
### 1. 事务超时
默认超时时间60秒可在 `@GlobalTransactional(timeoutMills = 30000)` 修改
### 2. 脏数据清理
undo_log保留7天通过 `undoLogRetentionDays` 配置
### 3. 锁等待
锁重试间隔30ms最多重试100次可通过 `lock-retry-interval``lock-retry-times` 调整
## 配置参数
| 参数 | 默认值 | 说明 |
|------|--------|------|
| `global-session-timeout` | 60000 | 全局事务超时(ms) |
| `async-commit-timeout` | 10000 | 异步提交超时(ms) |
| `lock-retry-interval` | 30 | 锁重试间隔(ms) |
| `lock-retry-times` | 100 | 锁重试次数 |
| `commit-retry-count` | 5 | 提交重试次数 |
| `rollback-retry-count` | 5 | 回滚重试次数 |
| `undo-log-retention-days` | 7 | undo_log保留天数 |