feat: 添加rabbitMQ

This commit is contained in:
Leon
2025-12-29 04:13:13 +08:00
parent 7daa95821d
commit 4225b026d7
5 changed files with 230 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ import (
type Config struct {
Server ServerConfig `yaml:"server"`
Database DatabaseConfig `yaml:"database"`
Redis RedisConfig `yaml:"redis"`
RabbitMQ RabbitMQConfig `yaml:"rabbitmq"`
}
type ServerConfig struct {
@@ -23,6 +25,20 @@ type DatabaseConfig struct {
DBName string `yaml:"dbname"`
}
type RedisConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
type RabbitMQConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
func Load(filename string) (Config, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {

View File

@@ -0,0 +1,106 @@
package rabbitmq
import (
"context"
"encoding/json"
"errors"
"feedsystem_video_go/internal/config"
"strconv"
"time"
amqp "github.com/rabbitmq/amqp091-go"
)
type RabbitMQ struct {
conn *amqp.Connection
ch *amqp.Channel
}
func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) {
if cfg == nil {
return nil, errors.New("rabbitmq config is nil")
}
url := "amqp://" + cfg.Username + ":" + cfg.Password + "@" + cfg.Host + ":" + strconv.Itoa(cfg.Port) + "/"
conn, err := amqp.Dial(url)
if err != nil {
return nil, err
}
ch, err := conn.Channel()
if err != nil {
return nil, err
}
return &RabbitMQ{conn: conn, ch: ch}, nil
}
func (r *RabbitMQ) Close() error {
if r == nil || r.ch == nil || r.conn == nil {
return nil
}
if err := r.ch.Close(); err != nil {
return err
}
if err := r.conn.Close(); err != nil {
return err
}
return nil
}
func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string) error {
if r == nil || r.ch == nil {
return errors.New("rabbitmq is not initialized")
}
if exchange == "" || queue == "" || bindingKey == "" {
return errors.New("exchange/queue/bindingKey is required")
}
if err := r.ch.ExchangeDeclare(
exchange,
"topic",
true,
false,
false,
false,
nil,
); err != nil {
return err
}
q, err := r.ch.QueueDeclare(
queue,
true,
false,
false,
false,
nil,
)
if err != nil {
return err
}
return r.ch.QueueBind(
q.Name,
bindingKey,
exchange,
false,
nil,
)
}
func (r *RabbitMQ) PublishJSON(ctx context.Context, exchange string, routingKey string, payload any) error {
if r == nil || r.ch == nil {
return errors.New("rabbitmq is not initialized")
}
if exchange == "" || routingKey == "" {
return errors.New("exchange and routingKey are required")
}
b, err := json.Marshal(payload)
if err != nil {
return err
}
return r.ch.PublishWithContext(ctx, exchange, routingKey, false, false, amqp.Publishing{
ContentType: "application/json",
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
Body: b,
})
}

View File

@@ -0,0 +1,77 @@
package rabbitmq
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"time"
)
type SocialMQ struct {
*RabbitMQ
}
const (
socialExchange = "social.events"
socialQueue = "social.events"
socialBindingKey = "social.*"
socialFollowRK = "social.follow"
socialUnfollowRK = "social.unfollow"
)
type SocialEvent struct {
EventID string `json:"event_id"`
Action string `json:"action"`
FollowerID uint `json:"follower_id"`
VloggerID uint `json:"vlogger_id"`
OccurredAt time.Time `json:"occurred_at"`
}
func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
if err := base.DeclareTopic(socialExchange, socialQueue, socialBindingKey); err != nil {
return nil, err
}
return &SocialMQ{RabbitMQ: base}, nil
}
func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error {
return s.publish(ctx, "follow", socialFollowRK, followerID, vloggerID)
}
func (s *SocialMQ) UnFollow(ctx context.Context, followerID, vloggerID uint) error {
return s.publish(ctx, "unfollow", socialUnfollowRK, followerID, vloggerID)
}
func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error {
if s == nil || s.RabbitMQ == nil {
return errors.New("social mq is not initialized")
}
if followerID == 0 || vloggerID == 0 {
return errors.New("followerID and vloggerID are required")
}
id, err := newEventID(16)
if err != nil {
return err
}
evt := SocialEvent{
EventID: id,
Action: action,
FollowerID: followerID,
VloggerID: vloggerID,
OccurredAt: time.Now().UTC(),
}
return s.PublishJSON(ctx, socialExchange, routingKey, evt)
}
func newEventID(n int) (string, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}