feat(broker): lab5 setup rabbitmq connection and durable topology
This commit is contained in:
@@ -2,6 +2,7 @@ package broker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
)
|
)
|
||||||
@@ -17,3 +18,41 @@ type RabbitPublisher struct {
|
|||||||
ch *amqp.Channel
|
ch *amqp.Channel
|
||||||
confirms chan amqp.Confirmation
|
confirms chan amqp.Confirmation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRabbitPublisher(url string) (*RabbitPublisher, error) {
|
||||||
|
p := &RabbitPublisher{url: url}
|
||||||
|
if err := p.connect(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *RabbitPublisher) connect() error {
|
||||||
|
var err error
|
||||||
|
p.conn, err = amqp.Dial(p.url)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("подключение к rabbitmq: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.ch, err = p.conn.Channel()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("открытие канала rabbitmq: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.ch.ExchangeDeclare("attachments", "direct", true, false, false, false, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("объявление exchange: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = p.ch.QueueDeclare("attachments.process", true, false, false, false, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("объявление очереди: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.ch.QueueBind("attachments.process", "process", "attachments", false, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("привязка очереди: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user