64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
|
|
package rabbit
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"datart/config"
|
||
|
|
|
||
|
|
"github.com/Azure/go-amqp"
|
||
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
||
|
|
)
|
||
|
|
|
||
|
|
type XQR struct {
|
||
|
|
ExchangeName string `json:"exchangename" yaml:"exchangename"`
|
||
|
|
QueueName string `json:"queuename" yaml:"queuename"`
|
||
|
|
RoutingKey string `json:"routingkey" yaml:"routingkey"`
|
||
|
|
QueueLength int64 `json:"queuelength" yaml:"queuelength"`
|
||
|
|
}
|
||
|
|
|
||
|
|
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 Close(ctx context.Context) error {
|
||
|
|
return client.Close(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
func genEndpoints(tag string) ([]rmq.Endpoint, error) {
|
||
|
|
confs := config.Conf().RabbitConf(tag)
|
||
|
|
endpoints := make([]rmq.Endpoint, len(confs))
|
||
|
|
|
||
|
|
for i, conf := range confs {
|
||
|
|
tlsConfig, err := conf.GetTLS().GenTLSConfig(tag)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
var options *rmq.AmqpConnOptions
|
||
|
|
var tls bool
|
||
|
|
if tlsConfig != nil {
|
||
|
|
options = &rmq.AmqpConnOptions{
|
||
|
|
SASLType: amqp.SASLTypeExternal(""),
|
||
|
|
TLSConfig: tlsConfig}
|
||
|
|
tls = true
|
||
|
|
}
|
||
|
|
|
||
|
|
endpoints[i].Address = conf.GenAddress(tls)
|
||
|
|
endpoints[i].Options = options
|
||
|
|
}
|
||
|
|
|
||
|
|
return endpoints, nil
|
||
|
|
}
|