文件
克隆/下载
README.md 828 Bytes
一键复制 编辑 原始数据 按行查看 历史

gorabbitmq

Installation

go get -u gitee.com/wiscity/go-rabbitmq

Quickstart

package main

import (
	"fmt"

	"gitee.com/wiscity/go-rabbitmq"
)

func main() {
	addr = "amqp://admin:admin@127.0.0.1:5672/"
    # 建立连接
	mq := rabbitmq.New(addr)

    // 定义队列和交换机
	queueExchange := rabbitmq.QueueExchange{
		QueueName:    "myname",
		RoutingKey:   "iot.report.opendoor.insert",
		ExchangeName: "iot.device.report.opendoor",
		ExchangeType: "topic",
	}

    # 建立生产者
    publisher := mq.NewPublisher(queueExchange)
    # 发送消息
    publisher.Pub([]byte("message"))

    # 建立消费者
    consumer := mq.NewConsumer(queueExchange)
    # 消费消息
    consumer.Receive(func(msg amqp.Delivery) {
        msg.Ack(false)
        fmt.Println(string(msg.Body))
    })

}

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化