GoFrame 框架培训大纲 - 第12章:消息队列与异步任务
12.1 消息队列基础
12.1.1 消息队列概念与原理
type MessageQueue interface {
Publish(topic string, message []byte) error
Subscribe(topic string, handler func([]byte)) error
UnSubscribe(topic string) error
}
type MessageQueuePattern struct {
Decoupling bool
Asynchronous bool
Buffering bool
Reliability bool
}
type Message struct {
ID string
Topic string
Payload []byte
Timestamp time.Time
Retries int
}
12.1.2 消息队列对比
type MessageQueueComparison struct {
Name string
Protocol string
Persistence bool
Scalability int
Complexity int
MessageOrdering bool
}
var comparisons = []MessageQueueComparison{
{
Name: "RabbitMQ",
Protocol: "AMQP",
Persistence: true,
Scalability: 8,
Complexity: 7,
MessageOrdering: true,
},
{
Name: "Kafka",
Protocol: "TCP",
Persistence: true,
Scalability: 10,
Complexity: 6,
MessageOrdering: true,
},
}
12.2 RabbitMQ 集成
12.2.1 RabbitMQ 连接管理
import (
"github.com/streadway/amqp"
"github.com/gogf/gf/v2/frame/g"
)
type RabbitMQClient struct {
connection *amqp.Connection
channel *amqp.Channel
}
func NewRabbitMQClient(uri string) (*RabbitMQClient, error) {
conn, err := amqp.Dial(uri)
if err != nil {
return nil, err
}</