关于golang:rabbitMq-交换机介绍-系列五-Header-golang-版本

57次阅读

共计 2160 个字符,预计需要花费 6 分钟才能阅读完成。

4 种不同的交换机类型

直连交换机:Direct exchange
扇形交换机:Fanout exchange
主题交换机:Topic exchange
首部交换机:Headers exchange

交换机具体含意参考 https://www.jianshu.com/p/469…

Header 交换机

以下代码参数具体含意能够参考 https://segmentfault.com/a/11…

Demo

生产者示例 producer.go
package main

import (
    "fmt"
    "github.com/streadway/amqp"
    "time"
)

// 因:疾速实现逻辑,故:不处理错误逻辑
func main() {conn, _ := amqp.Dial("amqp://fruit:fruit.15672@62.234.142.188:5672/fruitrq1")
    ch, _ := conn.Channel()
    body := "Hello World!" + time.Now().Format("2006-01-02 15:04:05")
    fmt.Println(body)
    var exchange_name string = "j_exch_head"
    var routing_key string = "jkey"
    var etype string = amqp.ExchangeHeaders
    // 申明交换器
    ch.ExchangeDeclare(exchange_name, etype, true, false, false, false, nil)

    ch.Publish(
        exchange_name, // exchange 这里为空则不抉择 exchange
        routing_key,   // routing key
        false,         // mandatory
        false,         // immediate
        amqp.Publishing{
            ContentType: "text/plain",
            Body:        []byte(body),
            Headers:     amqp.Table{"x-match": "any", "mail": "470047253@qq.com", "author": "Jhonny"}, // 头部信息 any: 匹配一个即可 all: 全副匹配
            //Expiration:  "3000", // 设置过期工夫
        })

    // defer 关键字
    defer conn.Close() // 压栈 后进先出
    defer ch.Close()   // 压栈 后进先出}
$ go run producer.go
Hello World! 2021-03-18 16:08:13
消费者示例 consumer.go
package main

import (
    "github.com/streadway/amqp"
    "log"
)

func main() {conn, _ := amqp.Dial("amqp://fruit:fruit.15672@62.234.142.188:5672/fruitrq1")
    ch, _ := conn.Channel()
    var exchange_name string = "j_exch_head"
    var routing_key string = "jkey"
    var queue_name string = "j_queue"
    var etype string = amqp.ExchangeHeaders // 头部交换机

    ch.QueueDeclare(queue_name, true, false, true, false, nil)
    // 申明交换器
    ch.ExchangeDeclare(exchange_name, etype, true, false, false, false, nil)


    ch.QueueBind(
        queue_name,    // queue name
        routing_key,   // routing key: Headers 头部交换机跟 routing_key 没关系
        exchange_name, // exchange
        false,
        amqp.Table{"mail": "470047253@qq.com"}, //  头部信息 any: 匹配一个即可 all: 全副匹配
    )

    // 监听
    msgs, _ := ch.Consume(
        queue_name, // queue name
        "",         // consumer
        true,       // auto-ack
        false,      // exclusive
        false,      // no-local
        false,      // no-wait
        nil,        // args
    )

    forever := make(chan bool)

    go func() {
        for d := range msgs {//println("tset")
            log.Printf("[x] %s", d.Body)
        }
    }()

    log.Printf("[*] Waiting for logs. To exit press CTRL+C")
    <-forever

}
$ go run consumer.go
2021/03/18 15:52:30  [*] Waiting for logs. To exit press CTRL+C
2021/03/18 15:52:34  [x] Hello World! 2021-03-18 15:52:34
2021/03/18 15:53:14  [x] Hello World! 2021-03-18 15:53:14
2021/03/18 16:08:13  [x] Hello World! 2021-03-18 16:08:13

正文完
 0