This commit is contained in:
liuxiaobo 2025-09-03 22:21:30 +08:00
parent 426f24f720
commit 63f13fa409
12 changed files with 48 additions and 39 deletions

4
go.mod
View File

@ -6,8 +6,6 @@ require (
github.com/ClickHouse/clickhouse-go/v2 v2.36.0 github.com/ClickHouse/clickhouse-go/v2 v2.36.0
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/golang-module/carbon/v2 v2.6.5 github.com/golang-module/carbon/v2 v2.6.5
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/natefinch/lumberjack v2.0.0+incompatible github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/nats-io/nats.go v1.42.0 github.com/nats-io/nats.go v1.42.0
@ -38,7 +36,9 @@ require (
github.com/go-faster/errors v0.7.1 // indirect github.com/go-faster/errors v0.7.1 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.1 // indirect github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect

View File

@ -2,7 +2,7 @@ package ipb
import ( import (
"encoding/json" "encoding/json"
"github.com/golang/protobuf/proto" "google.golang.org/protobuf/proto"
) )
func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, data []byte) *InternalMsg { func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, data []byte) *InternalMsg {

View File

@ -3,7 +3,7 @@ package processor
import ( import (
"fmt" "fmt"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/golang/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"reflect" "reflect"
) )

View File

@ -5,7 +5,7 @@ import (
"github.com/fox/fox/ipb" "github.com/fox/fox/ipb"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/fox/fox/timer" "github.com/fox/fox/timer"
"github.com/golang/protobuf/proto" "google.golang.org/protobuf/proto"
"testing" "testing"
) )

View File

@ -1,26 +1,23 @@
package rmq package rmq
import (
"time"
)
type RabbitMQConfig struct { type RabbitMQConfig struct {
URL string URL string // url
ExchangeName string ExchangeName string // 交换器
QueueName string QueueName string // 队列名
RoutingKey string RoutingKey string // 路由名
ReconnectInterval time.Duration ReconnectInterval int64 // 重连间隔(s)
MaxRetries int MaxRetries int // 重连次数
PrefetchCount int PrefetchCount int // 预取数量 防止队列内消息过多,冲垮消费者
Durable bool // 消息持久化至硬盘
} }
func LoadRabbitMQConfig() *RabbitMQConfig { func LoadRabbitMQConfig() *RabbitMQConfig {
return &RabbitMQConfig{ return &RabbitMQConfig{
URL: "amqp://guest:guest@localhost:5672/", URL: "amqp://admin:password@114.132.124.145:5672/vh_game_dev",
ExchangeName: "app_exchange", ExchangeName: "app_exchange",
QueueName: "app_queue", QueueName: "app_queue",
RoutingKey: "app.routing.key", RoutingKey: "app.routing.key",
ReconnectInterval: 5 * time.Second, ReconnectInterval: 5,
MaxRetries: 3, MaxRetries: 3,
PrefetchCount: 10, PrefetchCount: 10,
} }

View File

@ -2,8 +2,8 @@ package rmq
import ( import (
"context" "context"
"errors"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"time" "time"
) )
@ -37,19 +37,19 @@ func (c *Connection) Connect() error {
// 建立连接 // 建立连接
c.conn, err = amqp.Dial(c.config.URL) c.conn, err = amqp.Dial(c.config.URL)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to connect to RabbitMQ") return errors.Join(err, errors.New("failed to connect to RabbitMQ"))
} }
// 创建通道 // 创建通道
c.channel, err = c.conn.Channel() c.channel, err = c.conn.Channel()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to open channel") return errors.Join(err, errors.New("failed to open channel"))
} }
// 设置QoS // 设置QoS
err = c.channel.Qos(c.config.PrefetchCount, 0, false) err = c.channel.Qos(c.config.PrefetchCount, 0, false)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to set QoS") return errors.Join(err, errors.New("failed to set QoS"))
} }
// 声明交换器 // 声明交换器
@ -63,20 +63,20 @@ func (c *Connection) Connect() error {
nil, nil,
) )
if err != nil { if err != nil {
return errors.Wrap(err, "failed to declare exchange") return errors.Join(err, errors.New("failed to declare exchange"))
} }
// 声明队列 // 声明队列
_, err = c.channel.QueueDeclare( _, err = c.channel.QueueDeclare(
c.config.QueueName, c.config.QueueName, // 队列名称
true, c.config.Durable, // 持久化 true队列元数据名称、属性和消息会写入磁盘RabbitMQ 重启后仍存在。false队列存在于内存重启后丢失。
false, false, // 不自动删除 true当最后一个消费者断开连接后队列自动删除。
false, false, // 非独占队列
false, false, // 等待服务器确认
nil, nil,
) )
if err != nil { if err != nil {
return errors.Wrap(err, "failed to declare queue") return errors.Join(err, errors.New("failed to declare queue"))
} }
// 绑定队列 // 绑定队列
@ -88,7 +88,7 @@ func (c *Connection) Connect() error {
nil, nil,
) )
if err != nil { if err != nil {
return errors.Wrap(err, "failed to bind queue") return errors.Join(err, errors.New("failed to bind queue"))
} }
// 设置确认通道 // 设置确认通道
@ -118,8 +118,8 @@ func (c *Connection) Reconnect(ctx context.Context) {
// 重连逻辑 // 重连逻辑
for { for {
if err := c.Connect(); err != nil { if err := c.Connect(); err != nil {
log.ErrorF("Failed to reconnect: %v. Retrying in %v", err, c.config.ReconnectInterval) log.ErrorF("Failed to reconnect: %v. Retrying in %vs", err, c.config.ReconnectInterval)
time.Sleep(c.config.ReconnectInterval) time.Sleep(time.Duration(c.config.ReconnectInterval) * time.Second)
continue continue
} }
break break

View File

@ -2,8 +2,8 @@ package rmq
import ( import (
"context" "context"
"errors"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"time" "time"
) )
@ -39,7 +39,7 @@ func (c *Consumer) StartConsuming(ctx context.Context) error {
nil, nil,
) )
if err != nil { if err != nil {
return errors.Wrap(err, "failed to start consuming") return errors.Join(err, errors.New("failed to start consuming"))
} }
go c.consumeMessages(ctx, msgs) go c.consumeMessages(ctx, msgs)

View File

@ -2,8 +2,8 @@ package rmq
import ( import (
"context" "context"
"errors"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"time" "time"
) )
@ -59,7 +59,7 @@ func (p *Producer) publishWithRetry(ctx context.Context, body []byte) error {
) )
if err != nil { if err != nil {
return errors.Wrap(err, "failed to publish message") return errors.Join(err, errors.New("failed to publish message"))
} }
// 等待确认 // 等待确认

12
rmq/rmq_docker.txt Normal file
View File

@ -0,0 +1,12 @@
docker run -d \
--name rabbitmq \
--hostname my-rabbit-host \ # 设置固定主机名
-p 5672:5672 \ # AMQP 协议端口
-p 15672:15672 \ # 管理界面端口
-v rabbitmq_data:/var/lib/rabbitmq \ # 持久化数据卷
-e RABBITMQ_DEFAULT_USER=admin \ # 设置默认用户名
-e RABBITMQ_DEFAULT_PASS=password \ # 设置默认密码
rabbitmq:3-management # 使用带管理插件的镜像
docker run -d --name rabbitmq --hostname my-rabbit-host -p 5672:5672 -p 15672:15672 -v rabbitmq_data:/var/lib/rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management

View File

@ -10,7 +10,7 @@ import (
"time" "time"
) )
//const url = "amqp://samba:samba@testbuild.shoa.com:5672/vh_samba" //const url = "amqp://admin:password@114.132.124.145:5672/vh_game_dev"
//const exchangeName = "test_e" //const exchangeName = "test_e"
//const queueName = "test_q" //const queueName = "test_q"

View File

@ -8,8 +8,8 @@ import (
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/fox/fox/nat" "github.com/fox/fox/nat"
"github.com/fox/fox/processor" "github.com/fox/fox/processor"
"github.com/golang/protobuf/proto"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"google.golang.org/protobuf/proto"
"os" "os"
"time" "time"
) )

View File

@ -5,8 +5,8 @@ import (
"github.com/fox/fox/ipb" "github.com/fox/fox/ipb"
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/fox/fox/safeChan" "github.com/fox/fox/safeChan"
"github.com/golang/protobuf/proto"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
"sync" "sync"
"time" "time"
) )