package test import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq" "log" "strconv" "strings" "testing" "time" ) const FanoutExchangeName = "test_fanout_exchange" func TestFanoutExchangeSend(t *testing.T) { ch := rabbitmq.Connect("amqp://user:password@ip:port/") rabbitmq.NewExchange("amqp://user:password@ip:port/", FanoutExchangeName, "fanout") i := 0 for { time.Sleep(1) greetings := []string{"Helloworld!", strconv.Itoa(i)} ch.Publish(FanoutExchangeName, strings.Join(greetings, " "), "") i = i + 1 } } func TestFanoutExchangeReceive1(t *testing.T) { // 1.接收者,首先创建自己队列 // 2.创建交换机 // 3.将自己绑定到交换机上 // 4.接收交换机上发过来的消息 //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_fanout_exchange_receive_queue_1") //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型 rabbitmq.NewExchange("amqp://user:password@ip:port/", FanoutExchangeName, "fanout") // 队列绑定到exchange receiveMq.Bind(FanoutExchangeName, "") //4 for { //接收消息时,指定 msgs := receiveMq.Consume() go func() { for d := range msgs { log.Printf("recevie1 Received a message: %s", d.Body) } }() } } func TestFanoutExchangeReceive2(t *testing.T) { // 1.接收者,首先创建自己队列 // 2.创建交换机 // 3.将自己绑定到交换机上 // 4.接收交换机上发过来的消息 //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_fanout_exchange_receive_queue_2") //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型 rabbitmq.NewExchange("amqp://user:password@ip:port/", FanoutExchangeName, "fanout") // 队列绑定到exchange receiveMq.Bind(FanoutExchangeName, "") //4 for { //接收消息时,指定 msgs := receiveMq.Consume() go func() { for d := range msgs { log.Printf("recevie1 Received a message: %s", d.Body) } }() } }