关于golang:rabbitMq-交换机介绍-golang-版本

48次阅读

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

rabbitMq 有四种类型的交换机 fanout、direct、topic、headers

一 不必交换机的队列

生产者示例 producer.go

package main

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

func main() {conn, err := amqp.Dial("amqp://user:password@host:ip/vhost")
    if err != nil {log.Fatalf("%s", err)
    }
    defer conn.Close()

    ch, err := conn.Channel()
    if err != nil {log.Fatalf("%s", err)
    }
    defer ch.Close()

    body := "Hello World!" + time.Now().Format("2006-01-02 15:04:05")
    fmt.Println(body)

    ch.QueueDeclare(
        "j_test_delay", // name
        false,          // durable
        false,          // delete when unused
        false,          // exclusive
        false,          // no-wait
        nil,            // arguments
    )

    err = ch.Publish(
        "",             // exchange 这里为空则不抉择 exchange"j_test_delay", // routing key
        false,          // mandatory
        false,          // immediate
        amqp.Publishing{
            ContentType: "text/plain",
            Body:        []byte(body),
        })

}

消费者 consumer.go

package main

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

func main() {conn, err := amqp.Dial("amqp://user:password@host:ip/vhost")
    if err != nil {log.Fatalf("%s", err)
    }
    defer conn.Close()

    ch, err := conn.Channel()
    if err != nil {log.Fatalf("%s", err)
    }
    defer ch.Close()

    body := "Hello World!" + time.Now().Format("2006-01-02 15:04:05")
    fmt.Println(body)

    ch.QueueDeclare(
        "j_test_delay", // name
        false,          // durable
        false,          // delete when unused
        false,          // exclusive
        false,          // no-wait
        nil,            // arguments
    )

    err = ch.Publish(
        "",             // exchange 这里为空则不抉择 exchange"j_test_delay", // routing key
        false,          // mandatory
        false,          // immediate
        amqp.Publishing{
            ContentType: "text/plain",
            Body:        []byte(body),
        })

}

正文完
 0