Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/icpd/rabbitmq
Wrap RabbitMQ ( amqp091-go ) to enable automatic reconnection in case of disconnection. 针对 RabbitMQ(amqp091-go) 的封装,使其能够断线自动重连
https://github.com/icpd/rabbitmq
amqp091-go channel connect rabbitmq reconnect
Last synced: 11 days ago
JSON representation
Wrap RabbitMQ ( amqp091-go ) to enable automatic reconnection in case of disconnection. 针对 RabbitMQ(amqp091-go) 的封装,使其能够断线自动重连
- Host: GitHub
- URL: https://github.com/icpd/rabbitmq
- Owner: icpd
- Created: 2023-03-25T07:49:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-31T16:51:16.000Z (about 1 year ago)
- Last Synced: 2024-10-10T22:35:14.747Z (27 days ago)
- Topics: amqp091-go, channel, connect, rabbitmq, reconnect
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 11
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rabbitmq
Wrap `github.com/rabbitmq/amqp091-go` to enable automatic reconnection in case of disconnection.
## Feature
- Add automatic reconnection handling
- Retry sending messages when failed
- Shield the usage details of `github.com/rabbitmq/amqp091-go` and reduce users' cognitive load.## Usage
### Publish
```go
package mainimport (
"context"
"fmt"
"log""github.com/icpd/rabbitmq"
)func main() {
// 1. Initialize a rabbitmq object
r := rabbitmq.NewRabbitmq(
"amqp://admin:[email protected]:5672",
nil,
)// 2. Create a connection to the rabbitmq service
if err := r.Connect(); err != nil {
log.Fatal(err)
}// 3. Declare an exchange with the rabbitmq service
exchange := rabbitmq.ExchangeOptions{
Name: "example_exchange", // Exchange name
Type: rabbitmq.ExchangeTypeFanout, // Exchange type
Durable: true, // Whether it is durable
}
if err := r.Exchange(exchange); err != nil {
log.Fatal(err)
}// 4. Send a message to the exchange, not blocking
// Notice: Please ensure that the exchange has been created before publishing a message
err := r.Publish(
context.Background(),
[]byte(fmt.Sprintf("hello %d", i)), // Message body to be sent
rabbitmq.Exchange(exchange), // Set the message exchange target
)
if err != nil {
log.Fatal(err)
}select {}
}```
### Subscribe
```go
package mainimport (
"log""github.com/icpd/rabbitmq"
)func main() {
// 1. Initialize a rabbitmq object
r := rabbitmq.NewRabbitmq(
"amqp://admin:[email protected]:5672",
nil,
)// 2. Create a connection to the rabbitmq service
if err := r.Connect(); err != nil {
log.Fatal(err)
}// 3. Exchange configuration
exchange := rabbitmq.ExchangeOptions{
Name: "example_exchange", // Exchange name
Type: rabbitmq.ExchangeTypeFanout, // Exchange type
Durable: true, // Whether it is durable
}// 4. Create a subscription
// Subscribing and consuming internally starts a goroutine that consumes data so it won't block the main goroutine.
err := r.Subscribe(func(msg []byte) error {
log.Println("receive:", string(msg))
return nil
},
rabbitmq.Queue("example_queue"), // Set the name of the consumption queue
rabbitmq.Exchange(exchange), // Set the exchange that the queue needs to bind to
)
if err != nil {
log.Fatal(err)
}select {}
}```
For more usage examples, please refer to the `_example` directory.## Others
https://github.com/wagslane/go-rabbitmq
https://github.com/pmorelli92/bunnify