153 lines
3.7 KiB
Go
153 lines
3.7 KiB
Go
|
|
package message
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type RedisConfig struct {
|
|||
|
|
// Stream 名称
|
|||
|
|
Stream string
|
|||
|
|
|
|||
|
|
// 消费者组名称
|
|||
|
|
Group string
|
|||
|
|
|
|||
|
|
// 消费者名称
|
|||
|
|
Consumer string
|
|||
|
|
|
|||
|
|
// 每次消费数量
|
|||
|
|
Count int64
|
|||
|
|
|
|||
|
|
// 是否自动 ACK
|
|||
|
|
AutoAck bool
|
|||
|
|
|
|||
|
|
// 处理函数
|
|||
|
|
HandleFunc func(ctx context.Context, message map[string]interface{}) error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// RabbitMQConfig RabbitMQ 队列配置
|
|||
|
|
type RabbitMQConfig struct {
|
|||
|
|
Mode string
|
|||
|
|
Exchange string
|
|||
|
|
Topic string
|
|||
|
|
DelayMessage bool
|
|||
|
|
|
|||
|
|
// 队列名称(必需)
|
|||
|
|
Name string
|
|||
|
|
|
|||
|
|
// 实际队列名(用于绑定)
|
|||
|
|
Queue string
|
|||
|
|
|
|||
|
|
// 是否持久化
|
|||
|
|
Durable bool
|
|||
|
|
|
|||
|
|
// QoS 预取数量(每次推送的消息数量,默认10)
|
|||
|
|
PrefetchCount int
|
|||
|
|
|
|||
|
|
// 最大重试次数(默认3)
|
|||
|
|
MaxRetry int
|
|||
|
|
|
|||
|
|
// 是否自动 ACK
|
|||
|
|
AutoAck bool
|
|||
|
|
|
|||
|
|
// 处理函数
|
|||
|
|
HandleFunc func(ctx context.Context, message map[string]interface{}) error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NATSConfig NATS 队列配置
|
|||
|
|
type NATSConfig struct {
|
|||
|
|
DelayMessage bool
|
|||
|
|
// Stream 名称
|
|||
|
|
Stream string
|
|||
|
|
|
|||
|
|
// 消费者名称
|
|||
|
|
Consumer string
|
|||
|
|
|
|||
|
|
// 是否持久化
|
|||
|
|
Durable bool
|
|||
|
|
|
|||
|
|
// 副本数
|
|||
|
|
Replicas int
|
|||
|
|
// QoS 预取数量(每次推送的消息数量,默认10)
|
|||
|
|
PrefetchCount int
|
|||
|
|
|
|||
|
|
// 是否自动 ACK
|
|||
|
|
AutoAck bool
|
|||
|
|
|
|||
|
|
// 处理函数
|
|||
|
|
HandleFunc func(ctx context.Context, message map[string]interface{}) error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// messageBroker 消息代理接口
|
|||
|
|
type messageBroker interface {
|
|||
|
|
// StreamGroup 创建消费组(支持单个配置或批量配置)
|
|||
|
|
streamGroup(ctx context.Context, configs ...interface{}) error
|
|||
|
|
|
|||
|
|
// Publish 发布消息(支持单个配置或批量配置)
|
|||
|
|
publish(ctx context.Context, config interface{}, data interface{}) error
|
|||
|
|
|
|||
|
|
// PublishDelayed 发布延迟消息(支持单个配置或批量配置)
|
|||
|
|
publishDelayed(ctx context.Context, config interface{}, data interface{}, delay int) error
|
|||
|
|
|
|||
|
|
// Subscribe 订阅消息(支持单个配置或批量配置)
|
|||
|
|
subscribe(ctx context.Context, configs ...interface{}) error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
type messageClientType string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
ClientTypeRedis messageClientType = "redis"
|
|||
|
|
ClientTypeRabbitMQ messageClientType = "rabbitmq"
|
|||
|
|
ClientTypeNATS messageClientType = "nats"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// newMessageBroker 创建消息代理实例
|
|||
|
|
func newMessageBroker(ctx context.Context, clientType messageClientType) (messageBroker, error) {
|
|||
|
|
switch clientType {
|
|||
|
|
case ClientTypeRedis:
|
|||
|
|
return &redisMessageClient{clientType: clientType}, nil
|
|||
|
|
case ClientTypeRabbitMQ:
|
|||
|
|
return &rabbitMQMessageClient{clientType: clientType}, nil
|
|||
|
|
case ClientTypeNATS:
|
|||
|
|
return &natsMessageClient{clientType: clientType}, nil
|
|||
|
|
default:
|
|||
|
|
return nil, fmt.Errorf("unknown client type: %s", clientType)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// StreamGroup 直接创建消费组
|
|||
|
|
func StreamGroup(ctx context.Context, clientType messageClientType, configs ...interface{}) error {
|
|||
|
|
broker, err := newMessageBroker(ctx, clientType)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
return broker.streamGroup(ctx, configs...)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Publish 直接发布消息
|
|||
|
|
func Publish(ctx context.Context, clientType messageClientType, config interface{}, data interface{}) error {
|
|||
|
|
broker, err := newMessageBroker(ctx, clientType)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
return broker.publish(ctx, config, data)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// PublishDelayed 直接发布延迟消息
|
|||
|
|
func PublishDelayed(ctx context.Context, clientType messageClientType, config interface{}, data interface{}, delay int) error {
|
|||
|
|
broker, err := newMessageBroker(ctx, clientType)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
return broker.publishDelayed(ctx, config, data, delay)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Subscribe 直接订阅消息
|
|||
|
|
func Subscribe(ctx context.Context, clientType messageClientType, configs ...interface{}) error {
|
|||
|
|
broker, err := newMessageBroker(ctx, clientType)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
return broker.subscribe(ctx, configs...)
|
|||
|
|
}
|