当前位置: 首页 > >

RabbitMQ--go完整实例

发布时间:

RabbitMQ?go

接收和发送实例(下面是完整代码):


package main

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

var conn *amqp.Connection
var channel *amqp.Channel
var count = 0

const (
queueName = "push.msg.q"
exchange = "t.msg.ex"
mqurl ="amqp://shi:123@192.168.232.130:5672/test"
)

func main() {
go func() {
for {
push()
time.Sleep(1 * time.Second)
}
}()
receive()
fmt.Println("end")
close()
}

func failOnErr(err error, msg string) {
if err != nil {
log.Fatalf("%s:%s", msg, err)
panic(fmt.Sprintf("%s:%s", msg, err))
}
}

func mqConnect() {
var err error
conn, err = amqp.Dial(mqurl)
failOnErr(err, "failed to connect tp rabbitmq")

channel, err = conn.Channel()
failOnErr(err, "failed to open a channel")
}

func close() {
channel.Close()
conn.Close()
}

//连接rabbitmq server
func push() {

if channel == nil {
mqConnect()
}
msgContent := "hello world!"

channel.Publish(exchange, queueName, false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte(msgContent),
})
}

func receive() {
if channel == nil {
mqConnect()
}

msgs, err := channel.Consume(queueName, "", true, false, false, false, nil)
failOnErr(err, "")

forever := make(chan bool)

go func() {
//fmt.Println(*msgs)
for d := range msgs {
s := BytesToString(&(d.Body))
count++
fmt.Printf("receve msg is :%s -- %d
", *s, count)
}
}()

fmt.Printf(" [*] Waiting for messages. To exit press CTRL+C
")
<-forever
}

func BytesToString(b *[]byte) *string {
s := bytes.NewBuffer(*b)
r := s.String()
return &r
}



友情链接: