rabbitmq 操作库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

  1. package test
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. const DirectExchangeName = "test_direct_exchange"
  11. const DirectExchangeRoutingKey1 = "direct_exchange_routing_key_1"
  12. const DirectExchangeRoutingKey2 = "direct_exchange_routing_key_2"
  13. func TestDirectExchangeSend(t *testing.T) {
  14. ch := rabbitmq.Connect("amqp://user:password@ip:port/")
  15. rabbitmq.NewExchange("amqp://user:password@ip:port/", DirectExchangeName, "direct")
  16. i := 0
  17. for {
  18. time.Sleep(1)
  19. greetings := []string{"HelloWorld!", strconv.Itoa(i)}
  20. if i%2 == 1 {
  21. //如果是奇数
  22. ch.Publish("exchange", strings.Join(greetings, " "), DirectExchangeRoutingKey1)
  23. } else {
  24. ch.Publish("exchange", strings.Join(greetings, " "), DirectExchangeRoutingKey2)
  25. }
  26. i = i + 1
  27. }
  28. }
  29. func TestDirectExchangeReceive1(t *testing.T) {
  30. // 1.接收者,首先自己队列
  31. // 2.创建交换机
  32. // 3.将自己绑定到交换机上
  33. // 4.接收交换机上发过来的消息
  34. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  35. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_direct_exchange_receive_queue_1")
  36. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  37. rabbitmq.NewExchange("amqp://user:password@ip:port/", "exchange", "direct")
  38. receiveMq.Bind(DirectExchangeName, DirectExchangeRoutingKey1)
  39. //4
  40. for {
  41. //接收消息时,指定
  42. messages := receiveMq.Consume()
  43. go func() {
  44. for msg := range messages {
  45. log.Printf("recevie1 Received a message: %s", msg.Body)
  46. }
  47. }()
  48. }
  49. }
  50. func TestDirectExchangeReceive2(t *testing.T) {
  51. // 1.接收者,首先自己队列
  52. // 2.创建交换机
  53. // 3.将自己绑定到交换机上
  54. // 4.接收交换机上发过来的消息
  55. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  56. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_direct_exchange_receive_queue_2")
  57. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  58. rabbitmq.NewExchange("amqp://user:password@ip:port/", "exchange", "direct")
  59. receiveMq.Bind(DirectExchangeName, DirectExchangeRoutingKey2)
  60. //4
  61. for {
  62. //接收消息时,指定
  63. messages := receiveMq.Consume()
  64. go func() {
  65. for msg := range messages {
  66. log.Printf("recevie1 Received a message: %s", msg.Body)
  67. }
  68. }()
  69. }
  70. }