2025-09-19 16:17:46 +08:00
|
|
|
package rabbit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type rabbitClient struct {
|
|
|
|
|
env *rmq.Environment
|
|
|
|
|
conn *rmq.AmqpConnection
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func NewClient(ctx context.Context, tag string) (*rabbitClient, error) {
|
2025-09-19 16:17:46 +08:00
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
endpoints, err := genEndpoints(tag)
|
2025-09-19 16:17:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
cli := new(rabbitClient)
|
|
|
|
|
cli.env = rmq.NewClusterEnvironment(endpoints)
|
|
|
|
|
conn, err := cli.env.NewConnection(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
client.conn = conn
|
|
|
|
|
|
|
|
|
|
return cli, nil
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *rabbitClient) Management() *rmq.AmqpManagement {
|
|
|
|
|
return c.conn.Management()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *rabbitClient) NewPublisher(ctx context.Context, destination rmq.ITargetAddress,
|
|
|
|
|
options rmq.IConsumerOptions) (*rmq.Publisher, error) {
|
|
|
|
|
|
|
|
|
|
return c.conn.NewPublisher(ctx, destination, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *rabbitClient) NewConsumer(ctx context.Context, queueName string,
|
|
|
|
|
options rmq.IConsumerOptions) (*rmq.Consumer, error) {
|
|
|
|
|
|
|
|
|
|
return c.conn.NewConsumer(ctx, queueName, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *rabbitClient) Close(ctx context.Context) error {
|
|
|
|
|
return c.env.CloseConnections(ctx)
|
|
|
|
|
}
|