Merge pull request #13 from yiyiis/fix/separate-rabbitmq-channels

Fix/separate rabbitmq channels
This commit is contained in:
Chaoqian Xian
2026-05-23 23:14:58 +08:00
committed by GitHub
10 changed files with 162 additions and 114 deletions

View File

@@ -18,6 +18,7 @@ import (
"time" "time"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"github.com/joho/godotenv"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -55,6 +56,10 @@ func connectWithRetry(name string, maxRetries int, fn func() error) {
} }
func main() { func main() {
// 加载 .env本地开发
if err := godotenv.Load(); err != nil {
log.Println(".env not found; continuing")
}
// 加载配置 // 加载配置
configPath := os.Getenv("CONFIG_PATH") configPath := os.Getenv("CONFIG_PATH")
if configPath == "" { if configPath == "" {

View File

@@ -201,18 +201,21 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
timelineMQ = nil timelineMQ = nil
} }
worker.StartOutboxPoller(db, timelineMQ) worker.StartOutboxPoller(db, timelineMQ)
worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache) worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache, rmq)
// SSE notification // SSE notification
if rmq != nil && rmq.Ch != nil { if rmq != nil {
if err := rmq.DeclareTopic("like.events", "notification.like", "like.like"); err != nil { if notifCh, err := rmq.NewChannel(); err == nil {
log.Printf("notification like topic init failed: %v", err) if err := rabbitmq.DeclareTopic(notifCh, "like.events", "notification.like", "like.like"); err != nil {
} log.Printf("notification like topic init failed: %v", err)
if err := rmq.DeclareTopic("comment.events", "notification.comment", "comment.publish"); err != nil { }
log.Printf("notification comment topic init failed: %v", err) if err := rabbitmq.DeclareTopic(notifCh, "comment.events", "notification.comment", "comment.publish"); err != nil {
} log.Printf("notification comment topic init failed: %v", err)
if err := rmq.DeclareTopic("social.events", "notification.social", "social.follow"); err != nil { }
log.Printf("notification social topic init failed: %v", err) if err := rabbitmq.DeclareTopic(notifCh, "social.events", "notification.social", "social.follow"); err != nil {
log.Printf("notification social topic init failed: %v", err)
}
notifCh.Close()
} }
} }
sseHub := worker.NewSSEHub(db) sseHub := worker.NewSSEHub(db)
@@ -221,12 +224,12 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
sseHub.RegisterRoutes(r, notifGroup) sseHub.RegisterRoutes(r, notifGroup)
go func() { go func() {
if rmq != nil && rmq.Ch != nil { if rmq != nil {
hub := sseHub hub := sseHub
ctx := context.Background() ctx := context.Background()
// consume from like queue // consume from like queue
go func() { go func() {
ch, err := rmq.Conn.Channel() ch, err := rmq.NewChannel()
if err != nil { if err != nil {
log.Printf("notification-like channel: %v", err) log.Printf("notification-like channel: %v", err)
return return
@@ -238,7 +241,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
} }
}() }()
go func() { go func() {
ch, err := rmq.Conn.Channel() ch, err := rmq.NewChannel()
if err != nil { if err != nil {
log.Printf("notification-comment channel: %v", err) log.Printf("notification-comment channel: %v", err)
return return
@@ -250,7 +253,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
} }
}() }()
go func() { go func() {
ch, err := rmq.Conn.Channel() ch, err := rmq.NewChannel()
if err != nil { if err != nil {
log.Printf("notification-social channel: %v", err) log.Printf("notification-social channel: %v", err)
return return

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"errors" "errors"
"time" "time"
amqp "github.com/rabbitmq/amqp091-go"
) )
type CommentMQ struct { type CommentMQ struct {
*RabbitMQ ch *amqp.Channel
} }
const ( const (
@@ -34,10 +36,15 @@ func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) {
if base == nil { if base == nil {
return nil, errors.New("rabbitmq base is nil") return nil, errors.New("rabbitmq base is nil")
} }
if err := base.DeclareTopic(commentExchange, commentQueue, commentBindingKey); err != nil { ch, err := base.NewChannel()
if err != nil {
return nil, err return nil, err
} }
return &CommentMQ{RabbitMQ: base}, nil if err := DeclareTopic(ch, commentExchange, commentQueue, commentBindingKey); err != nil {
ch.Close()
return nil, err
}
return &CommentMQ{ch: ch}, nil
} }
func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, authorID uint, content string) error { func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, authorID uint, content string) error {
@@ -56,7 +63,7 @@ func (c *CommentMQ) Delete(ctx context.Context, commentID uint) error {
} }
func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt CommentEvent) error { func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt CommentEvent) error {
if c == nil || c.RabbitMQ == nil { if c == nil || c.ch == nil {
return errors.New("comment mq is not initialized") return errors.New("comment mq is not initialized")
} }
id, err := newEventID(16) id, err := newEventID(16)
@@ -66,5 +73,5 @@ func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt
evt.EventID = id evt.EventID = id
evt.Action = action evt.Action = action
evt.OccurredAt = time.Now().UTC() evt.OccurredAt = time.Now().UTC()
return c.PublishJSON(ctx, commentExchange, routingKey, evt) return PublishJSON(ctx, c.ch, commentExchange, routingKey, evt)
} }

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"errors" "errors"
"time" "time"
amqp "github.com/rabbitmq/amqp091-go"
) )
type LikeMQ struct { type LikeMQ struct {
*RabbitMQ ch *amqp.Channel
} }
const ( const (
@@ -31,10 +33,15 @@ func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
if base == nil { if base == nil {
return nil, errors.New("rabbitmq base is nil") return nil, errors.New("rabbitmq base is nil")
} }
if err := base.DeclareTopic(likeExchange, likeQueue, likeBindingKey); err != nil { ch, err := base.NewChannel()
if err != nil {
return nil, err return nil, err
} }
return &LikeMQ{RabbitMQ: base}, nil if err := DeclareTopic(ch, likeExchange, likeQueue, likeBindingKey); err != nil {
ch.Close()
return nil, err
}
return &LikeMQ{ch: ch}, nil
} }
func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint) error { func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint) error {
@@ -46,7 +53,7 @@ func (l *LikeMQ) Unlike(ctx context.Context, userID, videoID uint) error {
} }
func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint) error { func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint) error {
if l == nil || l.RabbitMQ == nil { if l == nil || l.ch == nil {
return errors.New("like mq is not initialized") return errors.New("like mq is not initialized")
} }
if userID == 0 || videoID == 0 { if userID == 0 || videoID == 0 {
@@ -63,5 +70,5 @@ func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID,
VideoID: videoID, VideoID: videoID,
OccurredAt: time.Now(), OccurredAt: time.Now(),
} }
return l.PublishJSON(ctx, likeExchange, routingKey, event) return PublishJSON(ctx, l.ch, likeExchange, routingKey, event)
} }

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"errors" "errors"
"time" "time"
amqp "github.com/rabbitmq/amqp091-go"
) )
type PopularityMQ struct { type PopularityMQ struct {
*RabbitMQ ch *amqp.Channel
} }
const ( const (
@@ -29,14 +31,19 @@ func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) {
if base == nil { if base == nil {
return nil, errors.New("rabbitmq base is nil") return nil, errors.New("rabbitmq base is nil")
} }
if err := base.DeclareTopic(popularityExchange, popularityQueue, popularityBindingKey); err != nil { ch, err := base.NewChannel()
if err != nil {
return nil, err return nil, err
} }
return &PopularityMQ{RabbitMQ: base}, nil if err := DeclareTopic(ch, popularityExchange, popularityQueue, popularityBindingKey); err != nil {
ch.Close()
return nil, err
}
return &PopularityMQ{ch: ch}, nil
} }
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error { func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error {
if p == nil || p.RabbitMQ == nil { if p == nil || p.ch == nil {
return errors.New("popularity mq is not initialized") return errors.New("popularity mq is not initialized")
} }
if videoID == 0 || change == 0 { if videoID == 0 || change == 0 {
@@ -52,5 +59,5 @@ func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) e
Change: change, Change: change,
OccurredAt: time.Now().UTC(), OccurredAt: time.Now().UTC(),
} }
return p.PublishJSON(ctx, popularityExchange, popularityUpdateRK, event) return PublishJSON(ctx, p.ch, popularityExchange, popularityUpdateRK, event)
} }

View File

@@ -14,9 +14,9 @@ import (
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
) )
// RabbitMQ 只管理 ConnectionChannel 由各组件按需创建
type RabbitMQ struct { type RabbitMQ struct {
Conn *amqp.Connection Conn *amqp.Connection
Ch *amqp.Channel
} }
func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) { func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) {
@@ -28,41 +28,35 @@ func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ch, err := conn.Channel() return &RabbitMQ{Conn: conn}, nil
if err != nil {
_ = conn.Close()
return nil, err
}
return &RabbitMQ{Conn: conn, Ch: ch}, nil
} }
func (r *RabbitMQ) Close() error { func (r *RabbitMQ) Close() error {
if r == nil { if r == nil {
return nil return nil
} }
var closeErr error
if r.Ch != nil {
if err := r.Ch.Close(); err != nil {
closeErr = err
}
}
if r.Conn != nil { if r.Conn != nil {
if err := r.Conn.Close(); closeErr == nil && err != nil { return r.Conn.Close()
closeErr = err
}
} }
return closeErr return nil
} }
func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string) error { func (r *RabbitMQ) NewChannel() (*amqp.Channel, error) {
if r == nil || r.Ch == nil { if r == nil || r.Conn == nil {
return errors.New("rabbitmq is not initialized") return nil, errors.New("rabbitmq connection is not initialized")
}
return r.Conn.Channel()
}
func DeclareTopic(ch *amqp.Channel, exchange string, queue string, bindingKey string) error {
if ch == nil {
return errors.New("channel is not initialized")
} }
if exchange == "" || queue == "" || bindingKey == "" { if exchange == "" || queue == "" || bindingKey == "" {
return errors.New("exchange/queue/bindingKey is required") return errors.New("exchange/queue/bindingKey is required")
} }
if err := r.Ch.ExchangeDeclare( if err := ch.ExchangeDeclare(
exchange, exchange,
"topic", "topic",
true, true,
@@ -74,7 +68,7 @@ func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string
return err return err
} }
q, err := r.Ch.QueueDeclare( q, err := ch.QueueDeclare(
queue, queue,
true, true,
false, false,
@@ -86,7 +80,7 @@ func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string
return err return err
} }
if err := r.Ch.QueueBind( if err := ch.QueueBind(
q.Name, q.Name,
bindingKey, bindingKey,
exchange, exchange,
@@ -95,15 +89,15 @@ func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string
); err != nil { ); err != nil {
return err return err
} }
if err := DeclareDLX(r.Ch, queue); err != nil { if err := DeclareDLX(ch, queue); err != nil {
log.Printf("DLX declare failed for %s: %v", queue, err) log.Printf("DLX declare failed for %s: %v", queue, err)
} }
return nil return nil
} }
func (r *RabbitMQ) PublishJSON(ctx context.Context, exchange string, routingKey string, payload any) error { func PublishJSON(ctx context.Context, ch *amqp.Channel, exchange string, routingKey string, payload any) error {
if r == nil || r.Ch == nil { if ch == nil {
return errors.New("rabbitmq is not initialized") return errors.New("channel is not initialized")
} }
if exchange == "" || routingKey == "" { if exchange == "" || routingKey == "" {
return errors.New("exchange and routingKey are required") return errors.New("exchange and routingKey are required")
@@ -112,7 +106,7 @@ func (r *RabbitMQ) PublishJSON(ctx context.Context, exchange string, routingKey
if err != nil { if err != nil {
return err return err
} }
return r.Ch.PublishWithContext(ctx, exchange, routingKey, false, false, amqp.Publishing{ return ch.PublishWithContext(ctx, exchange, routingKey, false, false, amqp.Publishing{
ContentType: "application/json", ContentType: "application/json",
DeliveryMode: amqp.Persistent, DeliveryMode: amqp.Persistent,
Timestamp: time.Now(), Timestamp: time.Now(),

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"errors" "errors"
"time" "time"
amqp "github.com/rabbitmq/amqp091-go"
) )
type SocialMQ struct { type SocialMQ struct {
*RabbitMQ ch *amqp.Channel
} }
const ( const (
@@ -31,10 +33,15 @@ func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
if base == nil { if base == nil {
return nil, errors.New("rabbitmq base is nil") return nil, errors.New("rabbitmq base is nil")
} }
if err := base.DeclareTopic(socialExchange, socialQueue, socialBindingKey); err != nil { ch, err := base.NewChannel()
if err != nil {
return nil, err return nil, err
} }
return &SocialMQ{RabbitMQ: base}, nil if err := DeclareTopic(ch, socialExchange, socialQueue, socialBindingKey); err != nil {
ch.Close()
return nil, err
}
return &SocialMQ{ch: ch}, nil
} }
func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error { func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error {
@@ -46,7 +53,7 @@ func (s *SocialMQ) UnFollow(ctx context.Context, followerID, vloggerID uint) err
} }
func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error { func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error {
if s == nil || s.RabbitMQ == nil { if s == nil || s.ch == nil {
return errors.New("social mq is not initialized") return errors.New("social mq is not initialized")
} }
if followerID == 0 || vloggerID == 0 { if followerID == 0 || vloggerID == 0 {
@@ -63,5 +70,5 @@ func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, follo
VloggerID: vloggerID, VloggerID: vloggerID,
OccurredAt: time.Now().UTC(), OccurredAt: time.Now().UTC(),
} }
return s.PublishJSON(ctx, socialExchange, routingKey, evt) return PublishJSON(ctx, s.ch, socialExchange, routingKey, evt)
} }

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"errors" "errors"
"time" "time"
amqp "github.com/rabbitmq/amqp091-go"
) )
type TimelineMQ struct { type TimelineMQ struct {
*RabbitMQ ch *amqp.Channel
} }
const ( const (
@@ -28,14 +30,19 @@ func NewTimelineMQ(base *RabbitMQ) (*TimelineMQ, error) {
if base == nil { if base == nil {
return nil, errors.New("rabbitmq base is nil") return nil, errors.New("rabbitmq base is nil")
} }
if err := base.DeclareTopic(timelineExchange, timelineQueue, timelineBindingKey); err != nil { ch, err := base.NewChannel()
if err != nil {
return nil, err return nil, err
} }
return &TimelineMQ{RabbitMQ: base}, nil if err := DeclareTopic(ch, timelineExchange, timelineQueue, timelineBindingKey); err != nil {
ch.Close()
return nil, err
}
return &TimelineMQ{ch: ch}, nil
} }
func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime time.Time) error { func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime time.Time) error {
if t == nil || t.RabbitMQ == nil { if t == nil || t.ch == nil {
return errors.New("timeline mq is not initialized") return errors.New("timeline mq is not initialized")
} }
if videoID == 0 { if videoID == 0 {
@@ -51,5 +58,5 @@ func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime
CreateTime: createTime.UnixMilli(), CreateTime: createTime.UnixMilli(),
OccurredAt: time.Now(), OccurredAt: time.Now(),
} }
return t.PublishJSON(ctx, timelineExchange, timelinePublishRK, timeline) return PublishJSON(ctx, t.ch, timelineExchange, timelinePublishRK, timeline)
} }

View File

@@ -15,6 +15,7 @@ func UpdatePopularityCache(ctx context.Context, cache *rediscache.Client, id uin
} }
_ = cache.Del(context.Background(), cache.Key("video:detail:id=%d", id)) _ = cache.Del(context.Background(), cache.Key("video:detail:id=%d", id))
_ = cache.Del(context.Background(), cache.Key("video:entity:%d", id))
now := time.Now().UTC().Truncate(time.Minute) now := time.Now().UTC().Truncate(time.Minute)
windowKey := cache.Key("hot:video:1m:%s", now.Format("200601021504")) windowKey := cache.Key("hot:video:1m:%s", now.Format("200601021504"))

View File

@@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"feedsystem_video_go/internal/middleware/rabbitmq" "feedsystem_video_go/internal/middleware/rabbitmq"
"feedsystem_video_go/internal/middleware/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"feedsystem_video_go/internal/video" "feedsystem_video_go/internal/video"
"fmt" "fmt"
"log" "log"
@@ -15,7 +15,7 @@ import (
) )
func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) { func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
if db == nil || tmq == nil || tmq.RabbitMQ == nil || tmq.Ch == nil { if db == nil || tmq == nil {
log.Printf("Outbox poller disabled: timeline mq is not initialized") log.Printf("Outbox poller disabled: timeline mq is not initialized")
return return
} }
@@ -46,9 +46,9 @@ func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
}() }()
} }
func StartConsumer(tmq *rabbitmq.TimelineMQ, queueName string, redisClient *redis.Client) { func StartConsumer(tmq *rabbitmq.TimelineMQ, queueName string, redisClient *rediscache.Client, rmq *rabbitmq.RabbitMQ) {
if tmq == nil || tmq.RabbitMQ == nil || tmq.Ch == nil { if tmq == nil || rmq == nil || rmq.Conn == nil {
log.Printf("Timeline consumer disabled: timeline mq is not initialized") log.Printf("Timeline consumer disabled: rabbitmq is not initialized")
return return
} }
if redisClient == nil { if redisClient == nil {
@@ -56,54 +56,64 @@ func StartConsumer(tmq *rabbitmq.TimelineMQ, queueName string, redisClient *redi
return return
} }
msgs, err := tmq.Ch.Consume(
queueName,
"",
false,
false,
false,
false,
nil,
)
if err != nil {
log.Printf("注册消费失败")
return
}
go func() { go func() {
for msg := range msgs { for {
var event rabbitmq.TimelineEvent // 每次重连创建独立的 Channel不与发布者共用
err := json.Unmarshal(msg.Body, &event) ch, err := rmq.NewChannel()
if err != nil { if err != nil {
log.Printf("反序列化失败") log.Printf("Timeline consumer: 创建 Channel 失败: %v, 5秒后重试", err)
time.Sleep(5 * time.Second)
continue
}
if err := ch.Qos(10, 0, false); err != nil {
log.Printf("Timeline consumer: QoS 设置失败: %v", err)
}
msgs, err := ch.Consume(queueName, "", false, false, false, false, nil)
if err != nil {
log.Printf("Timeline consumer: 注册消费失败: %v, 5秒后重试", err)
ch.Close()
time.Sleep(5 * time.Second)
continue
}
log.Printf("Timeline consumer 已启动, queue=%s", queueName)
for msg := range msgs {
var event rabbitmq.TimelineEvent
if err := json.Unmarshal(msg.Body, &event); err != nil {
log.Printf("Timeline consumer: 反序列化失败: %v", err)
msg.Ack(false)
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
timelineKey := redisClient.Key("feed:global_timeline")
err = redisClient.ZAdd(ctx, timelineKey, oredis.Z{
Score: float64(event.CreateTime),
Member: fmt.Sprintf("%d", event.VideoID),
})
if err != nil {
log.Printf("Timeline consumer: 写入Zset失败: %v", err)
msg.Nack(false, true)
cancel()
continue
}
if err := redisClient.ZRemRangeByRank(ctx, timelineKey, 0, -1001); err != nil {
log.Printf("Timeline consumer: ZRem失败: %v", err)
}
msg.Ack(false) msg.Ack(false)
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
timelineKey := redisClient.Key("feed:global_timeline")
err = redisClient.ZAdd(ctx, timelineKey, oredis.Z{
Score: float64(event.CreateTime),
Member: fmt.Sprintf("%d", event.VideoID),
})
if err != nil {
log.Printf("写入Zset失败")
msg.Nack(false, true)
cancel() cancel()
continue
} }
err = redisClient.ZRemRangeByRank(ctx, timelineKey, 0, -1001) // msgs channel 关闭说明 AMQP Channel 断开,关闭并重连
ch.Close()
if err != nil { log.Printf("Timeline consumer: Channel 断开, 5秒后重连...")
log.Printf("ZRem失败") time.Sleep(5 * time.Second)
}
msg.Ack(false)
cancel()
} }
}() }()
} }