// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package mq import ( "adm-dws/common.in/jsonrpc2" "encoding/json" "fmt" "log" //"github.com/go-kit/kit/transport/amqp" //"github.com/go-kit/kit/transport/amqp" "github.com/streadway/amqp" "os" "time" ) type RabbitmqClient struct { Addr string Username string Passwrod string Vhost string ExchangeName string QueueName string RouteBindKey string Connection *amqp.Connection PubChannel *amqp.Channel CallFunc func([]byte)error IsPub bool ConsumerChannelCount int } var AdsMq *RabbitmqClient var DwsMq *RabbitmqClient var Md5Mq *RabbitmqClient func SetAdsMq(client *RabbitmqClient){ AdsMq = client } func SetDwsMq(client *RabbitmqClient){ DwsMq = client } func SetMd5Mq(client *RabbitmqClient){ Md5Mq = client } func InitRabbitmq(addr, username, passwrod, vhost, exchangeName, queueName, routeBindKey string,callFunc func([]byte)error,isPub bool,consumerChannelCount int) *RabbitmqClient { // 创建连接 rabbitmqClient := &RabbitmqClient{Addr: addr, Username: username, Passwrod: passwrod, Vhost: vhost, ExchangeName: exchangeName, RouteBindKey: routeBindKey, QueueName: queueName, CallFunc:callFunc,IsPub:isPub, ConsumerChannelCount:consumerChannelCount} var err error rabbitmqClient.Connection ,err = rabbitmqClient.Connect() if err != nil{ fmt.Println("dial err :", err) os.Exit(1) } // 启动发送channel if isPub{ err = rabbitmqClient.InitPubChannel() if err != nil { os.Exit(2) } } return rabbitmqClient } func (r *RabbitmqClient) StartConsumer(){ // 启动消费channel if r.CallFunc!= nil{ for i:=0;i