https://github.com/itcomusic/amqpx
An AMQP 0.9.1 Go client
https://github.com/itcomusic/amqpx
amqp go golang rabbitmq
Last synced: about 2 months ago
JSON representation
An AMQP 0.9.1 Go client
- Host: GitHub
- URL: https://github.com/itcomusic/amqpx
- Owner: itcomusic
- License: mit
- Created: 2022-07-04T09:08:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-28T16:20:11.000Z (12 months ago)
- Last Synced: 2024-11-14T01:33:22.690Z (5 months ago)
- Topics: amqp, go, golang, rabbitmq
- Language: Go
- Homepage:
- Size: 146 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - amqpx
README
# RabbitMQ Go Client
[![build-img]][build-url]
[![pkg-img]][pkg-url]
[![coverage-img]][coverage-url]This is a Go AMQP 0.9.1 client wraps [amqp091-go](https://github.com/rabbitmq/amqp091-go) with support generics
* Support of the encoding messages
* defaults encoding (json, protobuf, protojson)
* support of custom marshal/unmarshal functions
* Middleware for easy integration## Installation
Go version 1.20+
```bash
go get github.com/itcomusic/amqpx
```## Usage
```go
package mainimport (
"context"
"fmt"
"github.com/itcomusic/amqpx"
)func main() {
conn, _ := amqpx.Connect()
defer conn.Close()// simple publisher
pub := amqpx.NewPublisher[[]byte](conn, amqpx.Direct, amqpx.UseRoutingKey("routing_key"))
_ = pub.Publish(amqpx.NewPublishing([]byte("hello")).PersistentMode(), amqpx.SetRoutingKey("override_routing_key"))
// simple consumer
_ = conn.NewConsumer("foo", amqpx.D(func(ctx context.Context, req *amqpx.Delivery[[]byte]) amqpx.Action {
fmt.Printf("received message: %s\n", string(*req.Msg))
return amqpx.Ack
}))
}
```### Publisher & consumer struct
Pretty using struct and avoiding boilerplate marhsal/unmarshal. It is strict compared content-type of the message and
invalid body is rejected.```go
conn, _ := amqpx.Connect(
amqpx.UseUnmarshaler(amqpxproto.NewUnmarshaler()), // global unmarshalers
amqpx.UseMarshaler(amqpxproto.NewMarshaler())), // global marshaler
defer conn.Close()type Gopher struct {
Name string
}
// override default marshaler
pub := amqpx.NewPublisher[Gopher](conn, amqpx.Direct, amqpx.SetMarshaler(amqpxjson.Marshaler))
_ = pub.Publish(amqpx.NewPublishing(Gopher{Name: "Rob"}), amqpx.SetRoutingKey("routing_key"))
// override default unmarshaler
_ = conn.NewConsumer("bar", amqpx.D(func(ctx context.Context, req *amqpx.Delivery[Gopher]) amqpx.Action {
fmt.Printf("user-id: %s, received message: %s\n", req.Req.UserID, req.Msg.Name)
return amqpx.Ack
}), amqpx.SetUnmarshaler(amqpxjson.Unmarshaler), amqpx.SetAutoAckMode())
```### Consumer rate limiting
The Prefetch count informs the server will deliver that many messages to consumers before acknowledgments are received.
The Concurrency option limits numbers of goroutines of consumer, depends on prefetch count and auto-ack mode.```go
// prefetch count
_ = conn.NewConsumer("foo", amqpx.D(func(ctx context.Context, req *amqpx.Delivery[[]byte]) amqpx.Action {
fmt.Printf("received message: %s\n", string(*req.Msg))
return amqpx.Ack
}), amqpx.SetPrefetchCount(8))// limit goroutines
_ = conn.NewConsumer("foo", amqpx.D(func(ctx context.Context, req *amqpx.Delivery[[]byte]) amqpx.Action {
fmt.Printf("received message: %s\n", string(*req.Body))
return amqpx.Ack
}), amqpx.SetAutoAckMode(), amqpx.SetConcurrency(32))
```### Declare queue
The declare queue, exchange and binding queue.
```go
_ = conn.NewConsumer("foo", amqpx.D(func(ctx context.Context, req *amqpx.Delivery[[]byte]) amqpx.Action {
fmt.Printf("received message: %s\n", string(*req.Msg))
return amqpx.Ack
}), amqpx.DeclareQueue(amqpx.QueueDeclare{AutoDelete: true}),
amqpx.DeclareExchange(amqpx.ExchangeDeclare{Name: "exchange_name", Type: amqpx.Direct}),
amqpx.BindQueue(amqpx.QueueBind{Exchange: "exchange_name", RoutingKey: []string{"routing_key"}}))
```### Middleware
Predefined support opentelemetry using interceptor.
```go
import (
"github.com/itcomusic/amqpx"
"github.com/itcomusic/amqpx/amqpxotel"
)// global
conn, _ := amqpx.Connect(amqpx.UserInterceptor(amqpxotel.NewInterceptor())
defer conn.Close()// can use special interceptor for publisher
_ = amqpx.NewPublisher[[]byte](conn, amqpx.Direct, amqpx.SetPublishInterceptor(func(next amqpx.PublisherFunc) amqpx.PublisherFunc {
return func(ctx context.Context, m *amqpx.PublishingRequest) error {
fmt.Printf("message: %s\n", m.Body)
return next(m)
}
}))
```## License
[MIT License](LICENSE)
[build-img]: https://github.com/itcomusic/amqpx/workflows/build/badge.svg
[build-url]: https://github.com/itcomusic/amqpx/actions
[pkg-img]: https://pkg.go.dev/badge/github.com/itcomusic/amqpx.svg
[pkg-url]: https://pkg.go.dev/github.com/itcomusic/amqpx
[coverage-img]: https://codecov.io/gh/itcomusic/amqpx/branch/main/graph/badge.svg
[coverage-url]: https://codecov.io/gh/itcomusic/amqpx