go_rabbitmq

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 5 Imported by: 4

README

go-rabbitmq

rabbitmq的消费者和生产者

在项目中安装使用

go get -u github.com/MQEnergy/go-rabbitmq

测试

1、安装依赖

go mod tidy

运行example

配置 amqp 消费者

go run examples/consumer.go

生产者

go run examples/producer.go

查看效果

2、单元测试

go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	User     string
	Password string
	Host     string
	Port     string
	Vhost    string
}

Config amqp配置

type RabbitMQ

type RabbitMQ struct {
	QueueName string // 队列名称
	Exchange  string // 交换机 可为空
	RouteKey  string // 路由键 可为空
	Addr      string // 连接地址
	Type      string // 交换机连接方式 direct topic fanout headers 可为空
	Done      chan bool

	PrefetchCount int  // 消费者消费数据限流数
	Durable       bool // 是否queue队列持久化
	// contains filtered or unexported fields
}

func New

func New(config *Config, queueName, exchange, routeKey string, exchangeType, prefetchCount int, durable bool) *RabbitMQ

New 创建一个新的消费者状态实例,并自动尝试连接到服务器

func (*RabbitMQ) Close

func (m *RabbitMQ) Close() error

Close 关闭通道和连接。

func (*RabbitMQ) Consume

func (m *RabbitMQ) Consume() (<-chan amqp.Delivery, error)

Consume 将不断地将队列项放到通道上。 需要调用delivery.Ack 当它已经成功处理,或当它失败时,调用 delivery.Nack。 忽略这个参数会导致数据在服务器上堆积。

func (*RabbitMQ) Push

func (m *RabbitMQ) Push(data []byte) error

Push 将数据推送到队列中,并等待确认。 如果在resendTimeout时间内没有收到确认信息, 它不断地重新发送消息,直到收到一个确认。 直到服务器发送确认信息。错误是只在推送操作本身失败时返回,参见UnsafePush。

func (*RabbitMQ) UnsafePush

func (m *RabbitMQ) UnsafePush(data []byte) error

UnsafePush 将push到队列而不检查确认。如果连接失败,则返回错误。 没有提供服务器是否会接收消息。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL