|
@@ -22,20 +22,6 @@ type MessageConsumer struct { |
|
@@ -22,20 +22,6 @@ type MessageConsumer struct { |
22
|
afterHandles []TopicHandle
|
22
|
afterHandles []TopicHandle
|
23
|
}
|
23
|
}
|
24
|
|
24
|
|
25
|
-// func NewMessageConsumer() *MessageConsumer {
|
|
|
26
|
-// topics := []string{}
|
|
|
27
|
-// for key := range TopicHandleRouters {
|
|
|
28
|
-// topics = append(topics, key)
|
|
|
29
|
-// }
|
|
|
30
|
-// return &MessageConsumer{
|
|
|
31
|
-// ready: make(chan bool),
|
|
|
32
|
-// kafkaHosts: configs.Cfg.Servers,
|
|
|
33
|
-// groupId: configs.Cfg.ConsumerId,
|
|
|
34
|
-// topicsHandles: TopicHandleRouters,
|
|
|
35
|
-// topics: topics,
|
|
|
36
|
-// }
|
|
|
37
|
-// }
|
|
|
38
|
-
|
|
|
39
|
//实现对应的接口
|
25
|
//实现对应的接口
|
40
|
var _ sarama.ConsumerGroupHandler = (*MessageConsumer)(nil)
|
26
|
var _ sarama.ConsumerGroupHandler = (*MessageConsumer)(nil)
|
41
|
|
27
|
|
|
@@ -128,10 +114,12 @@ func (r *Runer) Start(ctx context.Context) { |
|
@@ -128,10 +114,12 @@ func (r *Runer) Start(ctx context.Context) { |
128
|
for {
|
114
|
for {
|
129
|
select {
|
115
|
select {
|
130
|
case <-ctx.Done():
|
116
|
case <-ctx.Done():
|
131
|
- err := r.consumerGroup.Close()
|
|
|
132
|
- logs.Warning("ctx cancel;consumerGroup.Close();err:%s", err)
|
117
|
+ logs.Warning("ctx cancel;consumerGroup.Close()")
|
|
|
118
|
+ r.consumerGroup.Close()
|
133
|
return
|
119
|
return
|
134
|
default:
|
120
|
default:
|
|
|
121
|
+
|
|
|
122
|
+ }
|
135
|
if err := r.consumerGroup.Consume(ctx, r.msgConsumer.topics, r.msgConsumer); err != nil {
|
123
|
if err := r.consumerGroup.Consume(ctx, r.msgConsumer.topics, r.msgConsumer); err != nil {
|
136
|
logs.Error("consumerGroup err:%s \n", err)
|
124
|
logs.Error("consumerGroup err:%s \n", err)
|
137
|
//等待重试
|
125
|
//等待重试
|
|
@@ -140,47 +128,7 @@ func (r *Runer) Start(ctx context.Context) { |
|
@@ -140,47 +128,7 @@ func (r *Runer) Start(ctx context.Context) { |
140
|
}
|
128
|
}
|
141
|
r.msgConsumer.ready = make(chan struct{})
|
129
|
r.msgConsumer.ready = make(chan struct{})
|
142
|
}
|
130
|
}
|
143
|
- }
|
|
|
144
|
}
|
131
|
}
|
145
|
func (r *Runer) IsReady() <-chan struct{} {
|
132
|
func (r *Runer) IsReady() <-chan struct{} {
|
146
|
return r.msgConsumer.ready
|
133
|
return r.msgConsumer.ready
|
147
|
} |
134
|
} |
148
|
-
|
|
|
149
|
-//StartConsumer 启动
|
|
|
150
|
-//返回 Consumer关闭方法 和 error
|
|
|
151
|
-// func StartConsumer(ctx context.Context) (func(), error) {
|
|
|
152
|
-// consumer := NewMessageConsumer()
|
|
|
153
|
-// config := sarama.NewConfig()
|
|
|
154
|
-// config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
|
|
|
155
|
-// config.Consumer.Offsets.Initial = sarama.OffsetNewest
|
|
|
156
|
-// config.Version = sarama.V0_11_0_2
|
|
|
157
|
-// consumerGroup, err := sarama.NewConsumerGroup(consumer.kafkaHosts, consumer.groupId, config)
|
|
|
158
|
-// if err != nil {
|
|
|
159
|
-// return func() {}, err
|
|
|
160
|
-// }
|
|
|
161
|
-// wg := &sync.WaitGroup{}
|
|
|
162
|
-// wg.Add(1)
|
|
|
163
|
-// go func() {
|
|
|
164
|
-// defer wg.Done()
|
|
|
165
|
-// for {
|
|
|
166
|
-// if err := ctx.Err(); err != nil {
|
|
|
167
|
-// logs.Error("ctx err:%s \n", err)
|
|
|
168
|
-// return
|
|
|
169
|
-// }
|
|
|
170
|
-// if err := consumerGroup.Consume(ctx, consumer.topics, consumer); err != nil {
|
|
|
171
|
-// logs.Error("consumerGroup err:%s \n", err)
|
|
|
172
|
-// }
|
|
|
173
|
-// consumer.ready = make(chan bool)
|
|
|
174
|
-// }
|
|
|
175
|
-// }()
|
|
|
176
|
-// //等待 consumerGroup 设置完成
|
|
|
177
|
-// <-consumer.ready
|
|
|
178
|
-// logs.Info("Sarama consumer up and running!...")
|
|
|
179
|
-// return func() {
|
|
|
180
|
-// wg.Wait()
|
|
|
181
|
-// if err := consumerGroup.Close(); err != nil {
|
|
|
182
|
-// logs.Error("consumerGroup.Close err %s", err)
|
|
|
183
|
-// }
|
|
|
184
|
-// logs.Info("consumerGroup.Close")
|
|
|
185
|
-// }, nil
|
|
|
186
|
-// } |
|
|