55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package rabbit
|
|
|
|
import (
|
|
"context"
|
|
"datart/config"
|
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
|
)
|
|
|
|
type XQK struct {
|
|
Exchange string `json:"exchange" yaml:"exchange"`
|
|
Queue string `json:"queue" yaml:"queue"`
|
|
Key string `json:"key" yaml:"key"`
|
|
QueueCap int64 `json:"queuecap" yaml:"queuecap"`
|
|
}
|
|
|
|
var client *rabbitClient
|
|
|
|
func init() {
|
|
endpoints, err := genEndpoints("default")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
client = new(rabbitClient)
|
|
client.env = rmq.NewClusterEnvironment(endpoints)
|
|
conn, err := client.env.NewConnection(context.Background())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
client.conn = conn
|
|
}
|
|
|
|
func CloseDefault(ctx context.Context) error {
|
|
return client.Close(ctx)
|
|
}
|
|
|
|
func DefaultManagement() *rmq.AmqpManagement {
|
|
return client.Management()
|
|
}
|
|
|
|
func genEndpoints(tag string) ([]rmq.Endpoint, error) {
|
|
confs := config.Conf().RabbitConf(tag)
|
|
endpoints := make([]rmq.Endpoint, len(confs))
|
|
|
|
for i, conf := range confs {
|
|
var options *rmq.AmqpConnOptions
|
|
|
|
endpoints[i].Address = conf.GenAddress(false)
|
|
endpoints[i].Options = options
|
|
}
|
|
|
|
return endpoints, nil
|
|
}
|