-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbitmq.go
159 lines (138 loc) · 3.24 KB
/
rabbitmq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gbase
import (
"os"
pb "github.com/gaspire/gbase/jobrequest"
"github.com/gaspire/gbase/util"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
// IJob is an interface for utron Jobs
type IJob interface {
Handle(req *pb.JobRequest) error
}
// RabbitMq 消息类型
type RabbitMq struct {
Connection *amqp.Connection
Channel *amqp.Channel
Queue amqp.Queue
Exchange string
}
// Close close connection
func (me *RabbitMq) Close() {
me.Connection.Close()
me.Channel.Close()
}
// NewRabbitMq 初始化rabbitmq
func NewRabbitMq(url, queue string) *RabbitMq {
conn, err := amqp.Dial(url)
util.FailOnError(err, "Failed to connect to RabbitMQ")
ch, err := conn.Channel()
util.FailOnError(err, "Failed to open a channel")
err = ch.ExchangeDeclare(
queue, // name
"fanout", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
util.FailOnError(err, "Failed to declare an exchange")
q, err := ch.QueueDeclare(
queue, // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
util.FailOnError(err, "Failed to declare a queue")
err = ch.QueueBind(
q.Name, // queue name
q.Name, // routing key
q.Name, // exchange
false,
nil,
)
util.FailOnError(err, "Failed to bind a queue")
return &RabbitMq{conn, ch, q, q.Name}
}
// Consume 处理消息
func (me *RabbitMq) Consume(job IJob, async bool) {
defer func() {
me.Close()
}()
var autoAck = true
//异步消费
if async == true {
autoAck = false
}
msgs, err := me.Channel.Consume(
me.Queue.Name, // queue
"", // consumer
autoAck, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if flag := util.FailOnError(err, "Failed to register a consumer"); flag {
return
}
log.WithFields(log.Fields{
"exchange": me.Exchange,
"queue": me.Queue.Name,
}).Info(" [*] Waiting for msgs. To exit press CTRL+C")
//处理消息
for d := range msgs {
//protobuf解码
preq := &pb.JobRequest{}
err := proto.Unmarshal(d.Body, preq)
util.FailOnError(err, "proto unmarshaling error")
if async == true {
go func(d amqp.Delivery) {
job.Handle(preq)
d.Ack(false)
}(d)
} else {
job.Handle(preq)
}
}
}
// AmqpPublish 发布消息
func AmqpPublish(queue string, data *pb.JobRequest) (err error) {
conn, err := amqp.Dial(os.Getenv("MQ_URL"))
if ok := util.FailOnError(err, "Failed to connect to RabbitMQ"); ok {
return
}
defer conn.Close()
ch, err := conn.Channel()
if ok := util.FailOnError(err, "Failed to open a channel"); ok {
return
}
defer ch.Close()
q, err := ch.QueueDeclare(
queue, // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if ok := util.FailOnError(err, "Failed to declare a queue"); ok {
return
}
body, _ := proto.Marshal(data)
err = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: body,
})
util.FailOnError(err, "Failed to publish")
return
}