2025-09-19 16:17:46 +08:00
|
|
|
package rabbit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"datart/config"
|
|
|
|
|
|
|
|
|
|
"github.com/Azure/go-amqp"
|
|
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
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"`
|
2025-09-19 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|