update
This commit is contained in:
@@ -293,77 +293,77 @@ func (e *ETHNode) handleUSDTEvent(vLog types.Log, ch chan any) {
|
||||
return
|
||||
}
|
||||
tx_hash := vLog.TxHash.Hex()
|
||||
tx, tx_ok := e.UnConfirmTxs[tx_hash]
|
||||
if tx_ok {
|
||||
// 【支付/提现】待确认交易中存在该交易Hash(说明是我们主动发起的交易)
|
||||
// 直接走确认流程,不发送待确认消息
|
||||
// log.Printf("🔍 检测到已发起的交易: TxHash=%s, Type=%d", tx_hash, tx.TxType)
|
||||
e.confirm("USDT", height, tx, ch)
|
||||
} else {
|
||||
// 【充值】待确认交易中不存在该交易hash(说明是外部转账)
|
||||
// 添加至待确认交易中,并立即发送待确认消息
|
||||
// 1,先根据to查询RmqMsgs,再根据存在的rmq_msg中的相关数据,存入待确认交易
|
||||
value, rmq_msg_ok := e.RmqMsgs[toAddr]
|
||||
var tx_type int
|
||||
if rmq_msg_ok {
|
||||
for _, v := range value {
|
||||
_, ok := v.(message.TopupMsg_req)
|
||||
if ok {
|
||||
tx_type = 0
|
||||
}
|
||||
_, ok1 := v.(message.WithdrawMsg_req)
|
||||
if ok1 {
|
||||
tx_type = 1
|
||||
}
|
||||
_, ok2 := v.(message.PayMsg_req)
|
||||
if ok2 {
|
||||
tx_type = 2
|
||||
}
|
||||
// tx, tx_ok := e.UnConfirmTxs[tx_hash]
|
||||
// if tx_ok {
|
||||
// // 【支付/提现】待确认交易中存在该交易Hash(说明是我们主动发起的交易)
|
||||
// // 直接走确认流程,不发送待确认消息
|
||||
// // log.Printf("🔍 检测到已发起的交易: TxHash=%s, Type=%d", tx_hash, tx.TxType)
|
||||
// e.confirm("USDT", height, tx, ch)
|
||||
// } else {
|
||||
// 【充值】待确认交易中不存在该交易hash(说明是外部转账)
|
||||
// 添加至待确认交易中,并立即发送待确认消息
|
||||
// 1,先根据to查询RmqMsgs,再根据存在的rmq_msg中的相关数据,存入待确认交易
|
||||
value, rmq_msg_ok := e.RmqMsgs[toAddr]
|
||||
var tx_type int
|
||||
if rmq_msg_ok {
|
||||
for _, v := range value {
|
||||
_, ok := v.(message.TopupMsg_req)
|
||||
if ok {
|
||||
tx_type = 0
|
||||
}
|
||||
_, ok1 := v.(message.WithdrawMsg_req)
|
||||
if ok1 {
|
||||
tx_type = 1
|
||||
}
|
||||
_, ok2 := v.(message.PayMsg_req)
|
||||
if ok2 {
|
||||
tx_type = 2
|
||||
}
|
||||
}
|
||||
e.UnConfirmTxs[tx_hash] = message.Tx_msg{
|
||||
TxType: tx_type,
|
||||
Tx: message.Tx{
|
||||
From: fromAddr,
|
||||
To: toAddr,
|
||||
Height: height,
|
||||
TxHash: tx_hash,
|
||||
Symbol: "USDT",
|
||||
Value: utils.BigIntUSDTToFloat64(transferEvent.Value),
|
||||
Status: 2, // 待确认状态
|
||||
},
|
||||
}
|
||||
// log.Printf("📝 待确认交易新增: TxHash=%s, Height=%d, To=%s, Type=%d", tx_hash, height, toAddr, tx_type)
|
||||
}
|
||||
e.UnConfirmTxs[tx_hash] = message.Tx_msg{
|
||||
TxType: tx_type,
|
||||
Tx: message.Tx{
|
||||
From: fromAddr,
|
||||
To: toAddr,
|
||||
Height: height,
|
||||
TxHash: tx_hash,
|
||||
Symbol: "USDT",
|
||||
Value: utils.BigIntUSDTToFloat64(transferEvent.Value),
|
||||
Status: 2, // 待确认状态
|
||||
},
|
||||
}
|
||||
// log.Printf("📝 待确认交易新增: TxHash=%s, Height=%d, To=%s, Type=%d", tx_hash, height, toAddr, tx_type)
|
||||
|
||||
// 🔔 【仅充值】立即发送待确认状态的消息(支付/提现不发送待确认消息)
|
||||
if tx_type == 0 && rmq_msg_ok {
|
||||
for _, v := range value {
|
||||
d1, ok := v.(message.TopupMsg_req)
|
||||
if ok && strings.ToLower(d1.Address) == toAddr {
|
||||
pendingMsg := message.TopupMsg_resp{
|
||||
Address: toAddr,
|
||||
Status: 2, // 待确认状态
|
||||
Chain: d1.Chain,
|
||||
Symbol: d1.Symbol,
|
||||
Amount: utils.BigIntUSDTToFloat64(transferEvent.Value),
|
||||
TxHash: tx_hash,
|
||||
// 🔔 【仅充值】立即发送待确认状态的消息(支付/提现不发送待确认消息)
|
||||
if tx_type == 0 && rmq_msg_ok {
|
||||
for _, v := range value {
|
||||
d1, ok := v.(message.TopupMsg_req)
|
||||
if ok && strings.ToLower(d1.Address) == toAddr {
|
||||
pendingMsg := message.TopupMsg_resp{
|
||||
Address: toAddr,
|
||||
Status: 2, // 待确认状态
|
||||
Chain: d1.Chain,
|
||||
Symbol: d1.Symbol,
|
||||
Amount: utils.BigIntUSDTToFloat64(transferEvent.Value),
|
||||
TxHash: tx_hash,
|
||||
}
|
||||
// log.Printf("📤 发送待确认充值消息: TxHash=%s, Address=%s, Amount=%.2f",
|
||||
// tx_hash, toAddr, pendingMsg.Amount)
|
||||
|
||||
// 异步发送,避免阻塞事件处理
|
||||
go func(msg message.TopupMsg_resp) {
|
||||
select {
|
||||
case ch <- msg:
|
||||
log.Printf("✅ 待确认充值消息已发送")
|
||||
default:
|
||||
log.Printf("⚠️ 通道阻塞,待确认消息发送失败")
|
||||
}
|
||||
// log.Printf("📤 发送待确认充值消息: TxHash=%s, Address=%s, Amount=%.2f",
|
||||
// tx_hash, toAddr, pendingMsg.Amount)
|
||||
|
||||
// 异步发送,避免阻塞事件处理
|
||||
go func(msg message.TopupMsg_resp) {
|
||||
select {
|
||||
case ch <- msg:
|
||||
log.Printf("✅ 待确认充值消息已发送")
|
||||
default:
|
||||
log.Printf("⚠️ 通道阻塞,待确认消息发送失败")
|
||||
}
|
||||
}(pendingMsg)
|
||||
break
|
||||
}
|
||||
}(pendingMsg)
|
||||
break
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -577,11 +577,11 @@ func (e *ETHNode) decodePrivatekey(address string) string {
|
||||
func (e *ETHNode) usdt_transfer(msg any) error {
|
||||
var user_from, final_from, to string
|
||||
var amount float64
|
||||
var tx_type int
|
||||
now_height, err := e.getBlockHeight()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get lastest height error: %v", err)
|
||||
}
|
||||
// var tx_type int
|
||||
// now_height, err := e.getBlockHeight()
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("get lastest height error: %v", err)
|
||||
// }
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 断言,确定本次转账是哪个类型
|
||||
// 支付操作
|
||||
@@ -590,7 +590,7 @@ func (e *ETHNode) usdt_transfer(msg any) error {
|
||||
e.AddAddress(v.ToAddress, v) // 存入该笔msg(AddAddress内部会转小写)
|
||||
// 统一转换为小写
|
||||
user_from, final_from, to, amount = strings.ToLower(v.FromAddress), strings.ToLower(v.FromAddress), strings.ToLower(v.ToAddress), v.Amount
|
||||
tx_type = 2
|
||||
// tx_type = 2
|
||||
}
|
||||
// 提现操作
|
||||
k, ok1 := msg.(message.WithdrawMsg_req)
|
||||
@@ -598,7 +598,7 @@ func (e *ETHNode) usdt_transfer(msg any) error {
|
||||
e.AddAddress(k.ToAddress, k) // 存入该笔msg(AddAddress内部会转小写)
|
||||
// 统一转换为小写
|
||||
user_from, final_from, to, amount = strings.ToLower(k.FromAddress), strings.ToLower(k.FromAddress), strings.ToLower(k.ToAddress), k.Amount
|
||||
tx_type = 1
|
||||
// tx_type = 1
|
||||
}
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 1,校验钱包余额
|
||||
@@ -658,7 +658,7 @@ func (e *ETHNode) usdt_transfer(msg any) error {
|
||||
)
|
||||
// 6, 签名交易并获得txHash
|
||||
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(e.NetId), privateKey)
|
||||
txHash := signedTx.Hash().Hex() // 通过签名信息解析出交易hash
|
||||
// txHash := signedTx.Hash().Hex() // 通过签名信息解析出交易hash
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sign transaction: %w", err)
|
||||
}
|
||||
@@ -667,21 +667,21 @@ func (e *ETHNode) usdt_transfer(msg any) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send transaction: %w", err)
|
||||
}
|
||||
// 8, 构造交易消息
|
||||
tx_msg := message.Tx_msg{
|
||||
TxType: tx_type,
|
||||
Tx: message.Tx{
|
||||
From: final_from,
|
||||
To: to,
|
||||
Height: now_height,
|
||||
TxHash: txHash,
|
||||
Symbol: "USDT",
|
||||
Value: amount,
|
||||
Status: 2,
|
||||
},
|
||||
}
|
||||
// 9, 将构造的交易消息存入待确认交易中
|
||||
e.UnConfirmTxs[txHash] = tx_msg
|
||||
// // 8, 构造交易消息
|
||||
// tx_msg := message.Tx_msg{
|
||||
// TxType: tx_type,
|
||||
// Tx: message.Tx{
|
||||
// From: final_from,
|
||||
// To: to,
|
||||
// Height: now_height,
|
||||
// TxHash: txHash,
|
||||
// Symbol: "USDT",
|
||||
// Value: amount,
|
||||
// Status: 2,
|
||||
// },
|
||||
// }
|
||||
// // 9, 将构造的交易消息存入待确认交易中
|
||||
// e.UnConfirmTxs[txHash] = tx_msg
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,9 @@ import "time"
|
||||
|
||||
// 配置文件结构
|
||||
type Config struct {
|
||||
RMQConfig RMQConfig `json:"rmq_config"`
|
||||
ETHConfig ETHConfig `json:"eth_config"`
|
||||
RMQConfig RMQConfig `json:"rmq_config"`
|
||||
ETHConfig ETHConfig `json:"eth_config"`
|
||||
TRONConfig TRONConfig `json:"tron_config"`
|
||||
}
|
||||
|
||||
type RMQConfig struct {
|
||||
@@ -31,6 +32,12 @@ type ETHConfig struct {
|
||||
DbConfig DbConfig `json:"dbConfig"`
|
||||
}
|
||||
|
||||
type TRONConfig struct {
|
||||
RpcUrl string `json:"rpcUrl"`
|
||||
ConfirmHeight uint64 `json:"confirmHeight"`
|
||||
DbConfig DbConfig `json:"dbConfig"`
|
||||
}
|
||||
|
||||
// Config 数据库配置
|
||||
type DbConfig struct {
|
||||
User string `json:"user"`
|
||||
|
||||
@@ -1,295 +1,40 @@
|
||||
# RabbitMQ 服务使用说明
|
||||
# RabbitMQ 服务模块
|
||||
|
||||
## 功能概述
|
||||
本模块提供 RabbitMQ 消息队列的封装,用于接收业务系统的请求和发送交易确认响应。
|
||||
|
||||
这个 RabbitMQ 服务实现了区块链支付系统的消息队列功能,包括:
|
||||
## 快速使用
|
||||
|
||||
### 消费队列(监听)
|
||||
1. **充值队列 (topup)** - 监听用户充值请求
|
||||
2. **提现队列 (withdraw)** - 监听用户提现请求
|
||||
3. **支付队列 (pay)** - 监听用户支付请求
|
||||
|
||||
### 发布队列(响应)
|
||||
1. **充值响应队列 (topup_resp)** - 发送充值确认结果
|
||||
2. **提现响应队列 (withdraw_resp)** - 发送提现确认结果
|
||||
3. **支付响应队列 (pay_resp)** - 发送支付确认结果
|
||||
|
||||
---
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 安装依赖
|
||||
|
||||
```bash
|
||||
go get github.com/rabbitmq/amqp091-go
|
||||
```
|
||||
|
||||
### 2. 配置文件示例 (config.json)
|
||||
|
||||
```json
|
||||
{
|
||||
"rmq_config": {
|
||||
"sub_addr": "amqp://username:password@localhost:5672",
|
||||
"pay": {
|
||||
"queue": "pay.auto.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.auto.routing.key"]
|
||||
},
|
||||
"topup": {
|
||||
"queue": "pay.recharge.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.recharge.routing.key"]
|
||||
},
|
||||
"withdraw": {
|
||||
"queue": "pay.withdraw.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.withdraw.routing.key"]
|
||||
},
|
||||
"pay_resp": {
|
||||
"queue": "pay.auto.return.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.auto.return.routing.key"]
|
||||
},
|
||||
"topup_resp": {
|
||||
"queue": "pay.recharge.return.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.recharge.return.routing.key"]
|
||||
},
|
||||
"withdraw_resp": {
|
||||
"queue": "pay.withdraw.return.queue",
|
||||
"exchange": "pay.exchange",
|
||||
"routing": ["pay.withdraw.return.routing.key"]
|
||||
}
|
||||
}
|
||||
```go
|
||||
// 创建 RabbitMQ 服务
|
||||
rmqServer, err := rmq.NewRabbitMQServer(config.RMQConfig)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
defer rmqServer.Close()
|
||||
|
||||
### 3. 使用示例
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
message "m2pool-payment/internal/msg"
|
||||
rmq "m2pool-payment/internal/queue"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 加载配置
|
||||
config := loadConfig() // 你的配置加载逻辑
|
||||
|
||||
// 创建 RabbitMQ 服务
|
||||
rmqServer, err := rmq.NewRabbitMQServer(config.RMQConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("创建 RabbitMQ 服务失败: %v", err)
|
||||
}
|
||||
defer rmqServer.Close()
|
||||
|
||||
// 设置消息处理回调
|
||||
rmqServer.OnTopupMsg = func(msg message.TopupMsg_req) {
|
||||
log.Printf("收到充值请求: %+v", msg)
|
||||
// 处理充值逻辑...
|
||||
// 添加地址监听等
|
||||
}
|
||||
|
||||
rmqServer.OnWithdrawMsg = func(msg message.WithdrawMsg_req) {
|
||||
log.Printf("收到提现请求: %+v", msg)
|
||||
// 处理提现逻辑...
|
||||
// 调用区块链转账
|
||||
}
|
||||
|
||||
rmqServer.OnPayMsg = func(msg message.PayMsg_req) {
|
||||
log.Printf("收到支付请求: %+v", msg)
|
||||
// 处理支付逻辑...
|
||||
// 调用区块链转账
|
||||
}
|
||||
|
||||
// 启动监听
|
||||
if err := rmqServer.Start(); err != nil {
|
||||
log.Fatalf("启动监听失败: %v", err)
|
||||
}
|
||||
|
||||
// 发送响应消息示例
|
||||
rmqServer.PublishTopupResp(message.TopupMsg_resp{
|
||||
Address: "0x123...",
|
||||
Status: 1,
|
||||
Chain: "ETH",
|
||||
Symbol: "USDT",
|
||||
Amount: 100.0,
|
||||
TxHash: "0xabc...",
|
||||
})
|
||||
|
||||
// 保持运行
|
||||
select {}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API 说明
|
||||
|
||||
### 创建服务
|
||||
|
||||
```go
|
||||
func NewRabbitMQServer(config message.RMQConfig) (*RabbitMQServer, error)
|
||||
```
|
||||
|
||||
创建一个新的 RabbitMQ 服务实例。
|
||||
|
||||
**参数:**
|
||||
- `config`: RabbitMQ 配置
|
||||
|
||||
**返回:**
|
||||
- `*RabbitMQServer`: 服务实例
|
||||
- `error`: 错误信息
|
||||
|
||||
---
|
||||
|
||||
### 启动监听
|
||||
|
||||
```go
|
||||
func (r *RabbitMQServer) Start() error
|
||||
```
|
||||
|
||||
启动所有队列的监听(topup, withdraw, pay)。
|
||||
|
||||
---
|
||||
|
||||
### 消息回调设置
|
||||
|
||||
```go
|
||||
// 充值请求回调
|
||||
// 设置消息回调
|
||||
rmqServer.OnTopupMsg = func(msg message.TopupMsg_req) {
|
||||
// 处理逻辑
|
||||
// 处理充值请求
|
||||
}
|
||||
|
||||
// 提现请求回调
|
||||
rmqServer.OnWithdrawMsg = func(msg message.WithdrawMsg_req) {
|
||||
// 处理逻辑
|
||||
}
|
||||
// 启动监听
|
||||
rmqServer.Start()
|
||||
|
||||
// 支付请求回调
|
||||
rmqServer.OnPayMsg = func(msg message.PayMsg_req) {
|
||||
// 处理逻辑
|
||||
}
|
||||
// 发送响应
|
||||
rmqServer.PublishTopupResp(response)
|
||||
```
|
||||
|
||||
---
|
||||
## 消息队列结构
|
||||
|
||||
### 发布响应消息
|
||||
### 请求队列(消费)
|
||||
- `topup` - 充值请求
|
||||
- `withdraw` - 提现请求
|
||||
- `pay` - 支付请求
|
||||
|
||||
```go
|
||||
// 发布充值响应
|
||||
func (r *RabbitMQServer) PublishTopupResp(resp message.TopupMsg_resp) error
|
||||
|
||||
// 发布提现响应
|
||||
func (r *RabbitMQServer) PublishWithdrawResp(resp message.WithdrawMsg_resp) error
|
||||
|
||||
// 发布支付响应
|
||||
func (r *RabbitMQServer) PublishPayResp(resp message.PayMsg_resp) error
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 关闭服务
|
||||
|
||||
```go
|
||||
func (r *RabbitMQServer) Close() error
|
||||
```
|
||||
|
||||
优雅关闭 RabbitMQ 连接和所有监听。
|
||||
|
||||
---
|
||||
|
||||
## 消息格式
|
||||
|
||||
### 充值请求 (TopupMsg_req)
|
||||
```json
|
||||
{
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"address": "0x123...",
|
||||
"timestamp": 1234567890,
|
||||
"sign": "signature_string"
|
||||
}
|
||||
```
|
||||
|
||||
### 充值响应 (TopupMsg_resp)
|
||||
```json
|
||||
{
|
||||
"address": "0x123...",
|
||||
"status": 1,
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"amount": 100.5,
|
||||
"tx_hash": "0xabc..."
|
||||
}
|
||||
```
|
||||
|
||||
### 提现请求 (WithdrawMsg_req)
|
||||
```json
|
||||
{
|
||||
"queue_id": "queue_123",
|
||||
"from_address": "0x123...",
|
||||
"to_address": "0x456...",
|
||||
"amount": 50.0,
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"timestamp": 1234567890,
|
||||
"sign": "signature_string"
|
||||
}
|
||||
```
|
||||
|
||||
### 提现响应 (WithdrawMsg_resp)
|
||||
```json
|
||||
{
|
||||
"queue_id": "queue_123",
|
||||
"status": 1,
|
||||
"amount": 50.0,
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"tx_hash": "0xdef..."
|
||||
}
|
||||
```
|
||||
|
||||
### 支付请求 (PayMsg_req)
|
||||
```json
|
||||
{
|
||||
"queue_id": "queue_456",
|
||||
"from_address": "0x123...",
|
||||
"to_address": "0x789...",
|
||||
"amount": 200.0,
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"order_id": "order_789",
|
||||
"timestamp": 1234567890,
|
||||
"sign": "signature_string"
|
||||
}
|
||||
```
|
||||
|
||||
### 支付响应 (PayMsg_resp)
|
||||
```json
|
||||
{
|
||||
"queue_id": "queue_456",
|
||||
"status": 1,
|
||||
"amount": 200.0,
|
||||
"chain": "ETH",
|
||||
"symbol": "USDT",
|
||||
"order_id": "order_789",
|
||||
"tx_hash": "0xghi..."
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 状态码说明
|
||||
|
||||
- `status = 0` - 失败
|
||||
- `status = 1` - 成功
|
||||
- `status = 2` - 待确认(仅用于链上交易)
|
||||
|
||||
---
|
||||
### 响应队列(发布)
|
||||
- `topup_resp` - 充值响应
|
||||
- `withdraw_resp` - 提现响应
|
||||
- `pay_resp` - 支付响应
|
||||
|
||||
## 特性
|
||||
|
||||
@@ -297,67 +42,5 @@ func (r *RabbitMQServer) Close() error
|
||||
✅ **消息持久化** - 消息不会丢失
|
||||
✅ **手动确认** - 处理成功后才确认消息
|
||||
✅ **并发安全** - 支持多 goroutine 并发发布
|
||||
✅ **优雅关闭** - 支持优雅关闭所有连接
|
||||
✅ **错误处理** - 完善的错误日志和重试机制
|
||||
|
||||
---
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **连接地址格式**: `amqp://username:password@host:port/`
|
||||
2. **消息确认**: 只有处理成功的消息才会被确认,失败的消息会重新入队
|
||||
3. **并发安全**: 发布消息时使用了互斥锁,可以安全地从多个 goroutine 调用
|
||||
4. **重连机制**: 连接断开时会自动重连,间隔 3 秒
|
||||
5. **消息持久化**: 消息和队列都是持久化的,重启后不会丢失
|
||||
|
||||
---
|
||||
|
||||
## 工作流程
|
||||
|
||||
```
|
||||
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
|
||||
│ 业务系统 │ ------> │ RabbitMQ │ ------> │ 支付系统 │
|
||||
│ │ 充值/ │ (消息队列) │ 消费 │ (本系统) │
|
||||
│ │ 提现/ │ │ │ │
|
||||
│ │ 支付 │ │ │ │
|
||||
└─────────────┘ └──────────────┘ └─────────────┘
|
||||
|
|
||||
| 监听链上
|
||||
v
|
||||
┌─────────────┐
|
||||
│ 区块链节点 │
|
||||
│ (ETH) │
|
||||
└─────────────┘
|
||||
|
|
||||
| 交易确认
|
||||
v
|
||||
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
|
||||
│ 业务系统 │ <------ │ RabbitMQ │ <------ │ 支付系统 │
|
||||
│ │ 响应 │ (响应队列) │ 发布 │ (本系统) │
|
||||
└─────────────┘ └──────────────┘ └─────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 故障排查
|
||||
|
||||
### 连接失败
|
||||
- 检查 RabbitMQ 服务是否运行
|
||||
- 检查用户名密码是否正确
|
||||
- 检查网络连接和端口(默认 5672)
|
||||
|
||||
### 消息未被消费
|
||||
- 检查队列绑定是否正确
|
||||
- 检查 routing key 是否匹配
|
||||
- 查看 RabbitMQ 管理界面的队列状态
|
||||
|
||||
### 消息重复消费
|
||||
- 确保消息处理成功后调用了 Ack
|
||||
- 检查是否有多个消费者实例
|
||||
|
||||
---
|
||||
|
||||
## 许可证
|
||||
|
||||
MIT License
|
||||
|
||||
更多详情请参考 [主 README](../../README.md)。
|
||||
|
||||
@@ -27,6 +27,7 @@ const (
|
||||
)
|
||||
|
||||
type ServerCtx struct {
|
||||
msgKey string
|
||||
Config message.Config
|
||||
blockChainServer *blockchain.BlockChainServer
|
||||
rmqServer *rmq.RabbitMQServer
|
||||
@@ -41,7 +42,7 @@ func verifyMessage(timestamp uint64, sign string) bool {
|
||||
return hash == sign
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
func loadConfig(msgKey string) {
|
||||
file, err := os.ReadFile("config.json")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("读取配置文件失败: %v", err))
|
||||
@@ -54,6 +55,8 @@ func loadConfig() {
|
||||
|
||||
log.Printf("✅ 配置加载成功: RPC=%s, WS=%s",
|
||||
s_ctx.Config.ETHConfig.RpcURL, s_ctx.Config.ETHConfig.WsURL)
|
||||
|
||||
s_ctx.msgKey = msgKey
|
||||
}
|
||||
|
||||
func initBlockChainServer() {
|
||||
@@ -252,13 +255,13 @@ func handleChainEvent(chainEventCh chan any) {
|
||||
}
|
||||
}
|
||||
|
||||
func Start() {
|
||||
func Start(msgKey string) {
|
||||
log.Println("========================================")
|
||||
log.Println("🚀 M2Pool Payment System Starting...")
|
||||
log.Println("========================================")
|
||||
|
||||
// 加载配置
|
||||
loadConfig()
|
||||
loadConfig(msgKey)
|
||||
|
||||
// ================== 初始化区块链节点 ==================
|
||||
initBlockChainServer()
|
||||
|
||||
Reference in New Issue
Block a user