-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer.go
162 lines (147 loc) · 4.16 KB
/
consumer.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
160
161
162
package rocketmq
import (
"context"
"fmt"
v2 "github.com/apache/rocketmq-client-go/v2"
"github.com/apache/rocketmq-client-go/v2/consumer"
"github.com/apache/rocketmq-client-go/v2/primitive"
"github.com/go-bamboo/pkg/log"
otelext "github.com/go-bamboo/pkg/otel"
"github.com/go-bamboo/pkg/queue"
"github.com/go-bamboo/pkg/rescue"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/metrics"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
type (
ConsumeHandle func(ctx context.Context, topic string, key, message []byte) error
ConsumeHandler interface {
Consume(ctx context.Context, topic string, key, message []byte) error
}
rocketQueue struct {
c *Conf
handler ConsumeHandler
sub v2.PushConsumer
tracer trace.Tracer
propagator propagation.TextMapPropagator
subCounter metrics.Counter // 发送次数
}
rocketQueues struct {
queues []*rocketQueue
}
)
func MustNewQueue(c *Conf, handler ConsumeHandler) queue.MessageQueue {
q, err := NewQueue(c, handler)
if err != nil {
log.Fatal(err)
}
return q
}
func NewQueue(c *Conf, handler ConsumeHandler) (queue.MessageQueue, error) {
q := rocketQueues{}
for i := 0; i < int(c.Conns); i++ {
cc, err := newKafkaQueue(c, handler)
if err != nil {
log.Error(err)
return nil, err
}
q.queues = append(q.queues, cc)
}
return &q, nil
}
func (q rocketQueues) Name() string {
return "rocketmq"
}
func (q rocketQueues) Start(ctx context.Context) error {
for _, qq := range q.queues {
qq.Start(ctx)
}
return nil
}
func (q rocketQueues) Stop(ctx context.Context) error {
for _, qq := range q.queues {
qq.Stop(ctx)
}
return nil
}
func newKafkaQueue(config *Conf, handler ConsumeHandler) (k *rocketQueue, err error) {
model := consumer.Clustering
if config.Broadcast {
model = consumer.BroadCasting
}
namesrvAdd, err := primitive.NewNamesrvAddr(config.Addrs...)
if err != nil {
log.Error(err)
return nil, err
}
cs, err := v2.NewPushConsumer(
consumer.WithGroupName(config.GroupId),
consumer.WithNameServer(namesrvAdd),
consumer.WithCredentials(primitive.Credentials{
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
}),
consumer.WithConsumerModel(model),
//consumer.WithNamespace(config.Namespace),
consumer.WithConsumeMessageBatchMaxSize(1), //
)
if err != nil {
return nil, errors.FromError(err)
}
k = &rocketQueue{
c: config,
handler: handler,
sub: cs,
tracer: otel.Tracer("rocketmq"),
propagator: propagation.NewCompositeTextMapPropagator(otelext.Metadata{}, propagation.Baggage{}, otelext.TraceContext{}),
}
return
}
func (c *rocketQueue) Start(context.Context) error {
log.Infof("start consumer topic:%v", c.c.Topic)
if err := c.consumeGroupTopic(c.c.Topic, c.c.Expression); err != nil {
return err
}
if err := c.sub.Start(); err != nil {
log.Error(err)
return err
}
log.Infof("start rocket consumer.")
return nil
}
func (c *rocketQueue) Stop(context.Context) error {
if err := c.sub.Shutdown(); err != nil {
return err
}
log.Infof("stop rocket consumer.")
return nil
}
func (c *rocketQueue) consumeGroupTopic(topic, expression string) error {
selector := consumer.MessageSelector{Type: consumer.TAG, Expression: expression}
err := c.sub.Subscribe(topic, selector, c.handleMsg)
if err != nil {
return err
}
return nil
}
func (c *rocketQueue) handleMsg(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
defer rescue.Recover()
for _, msg := range msgs {
cctx, span := c.tracer.Start(ctx, "sub:"+msg.Topic, trace.WithSpanKind(trace.SpanKindConsumer))
c.propagator.Inject(cctx, &MessageExtTextMapCarrier{msg: msg})
span.SetAttributes(
attribute.String("kafka.topic", msg.Topic),
attribute.String("kafka.key", string(msg.GetKeys())),
)
if err := c.handler.Consume(ctx, msg.Topic, []byte(msg.GetKeys()), msg.Body); err != nil {
span.RecordError(err)
se := errors.FromError(err)
log.Errorw(fmt.Sprintf("%+v", err), "code", se.Code, "reason", se.Reason)
return consumer.ConsumeRetryLater, err
}
}
return consumer.ConsumeSuccess, nil
}