GitHub user loringgit edited a discussion: Golang Client 同时消费topic的问题
看了example例子,消费消息的步骤是:
1.先创建Consumer(rmq_client.NewSimpleConsumer),可以指定多个topic
2.调用Start
3.通过Receive 轮询topic获取消息,Receive方法中有个轮询的逻辑貌似没起到轮询的作用,代码如下:
```
func (sc *defaultSimpleConsumer) Receive(ctx context.Context, maxMessageNum
int32, invisibleDuration time.Duration) ([]*MessageView, error) {
if !sc.isOn() {
return nil, fmt.Errorf("simple consumer is not running")
}
if maxMessageNum <= 0 {
return nil, fmt.Errorf("maxMessageNum must be greater than 0")
}
sc.subscriptionExpressionsLock.RLock()
topics := make([]string, 0, len(sc.subscriptionExpressions))
for k := range sc.subscriptionExpressions {
topics = append(topics, k)
}
sc.subscriptionExpressionsLock.RUnlock()
// All topic is subscribed.
if len(topics) == 0 {
return nil, fmt.Errorf("there is no topic to receive message")
}
next := atomic.AddInt32(&sc.topicIndex, 1)
idx := utils.Mod(next+1, len(topics))
topic := topics[idx]
```
上面这段代码中的topics 是map的key,是随机的,这里通过topicIndex 方式轮询的作用可能不生效
想问一下,一个NewSimpleConsumer下同时消费多个topic的情况下正确使用姿势是什么?如果只开启一个goroutine取轮询多个topic,有的topic的处理时间会很长(即使没有消息,也需要轮询),会影响其他topic的处理?
GitHub link: https://github.com/apache/rocketmq/discussions/9277
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]