package main
import (
"context"
"fmt"
"github.com/streadway/amqp"
"learning_tools/rabbitmq/mq"
"time"
)
/**
不使用交换器直接向队列写入消息
*/
func main() {
ctx, cancel := context.WithCancel(context.Background())
//消费者
go mq.SampleConsumption(ctx, "q1", "c1")
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
panic(err)
}
// 开启 channel
channel, err := conn.Channel()
if err != nil {
panic(err)
}
/**
创建一个队列
*/
//创建一个队列 q1
qd, err := channel.QueueDeclare("q1",
true,
false,
false,
true,
nil);
if err != nil {
panic(err)
}
/**
开始发送消息到队列
*/
for i := 0; i < 20; i++ {
time.Sleep(time.Second)
if err = channel.Publish("",
qd.Name, //现在route key是队列名称
false,
false,
amqp.Publishing{
Timestamp: time.Now(),
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(fmt.Sprintf("%d:,Hello Rabbit MQ!", i)),
}); err != nil {
panic(err)
}
}
time.Sleep(time.Second * 23)
cancel()
time.Sleep(time.Second * 1)
}
在使用过程发现默认交换器(AMQP default) 有数据写入 ,说明我们在生成消息时候 不填入交换器名称时候,使用的默认交换器。
下一章使用fanout类型交换器