sarama-cluster消费者运行时报panic: non-positive interval for NewTicker

笔者是go新手,照着例子运行customer代码如下:

package main

import (
	_"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	_"syscall"

	"github.com/Shopify/sarama"
	"github.com/bsm/sarama-cluster"
)

var (
	groupId    = "g3"
	brokerList = "single.com:9092"
	topicList  = "testCus"
	offset     = "oldest"
	verbose    = false

	logger = log.New(os.Stderr, "", log.LstdFlags)
)

func main() {
	var Address = []string{"single.com:9092"}
	topic := []string{"testCus"}
	// Init config
	config := cluster.NewConfig()
	if verbose {
		sarama.Logger = logger
	} else {
		config.Consumer.Return.Errors = true
		config.Group.Return.Notifications = true
	}

	switch offset {
	case "oldest":
		config.Consumer.Offsets.Initial = sarama.OffsetOldest
	case "newest":
		config.Consumer.Offsets.Initial = sarama.OffsetNewest
	default:
	}

	// Init consumer, consume errors & messages
	consumer, err := cluster.NewConsumer(Address, groupId, topic, config)
	if err != nil {
		log.Printf("%s: sarama-cluster.NewConsumer err, message=%s \n", groupId, err)
		return
	}
	defer consumer.Close()

    signals := make(chan os.Signal, 1)
    signal.Notify(signals, os.Interrupt)

    // 接收错误
    go func() {
        for err := range consumer.Errors() {
            log.Printf("Error: %s\n", err.Error())
        }
    }()

    // 打印一些rebalance的信息
    go func() {
        for ntf := range consumer.Notifications() {
            log.Printf("Rebalanced: %+v\n", ntf)
        }
    }()

    // 消费消息
    for {
        select {
        case msg, ok := <-consumer.Messages():
            if ok {
                fmt.Fprintf(os.Stdout, "%s : %s/%d/%d\t%s\t%s\n", "aaa",msg.Topic, msg.Partition, msg.Offset, msg.Key, msg.Value)
                consumer.MarkOffset(msg, "")   // 提交offset
            }
        case <-signals:
            return
        }
    }
}

结果报错如下:

2020/02/23 19:36:04 Rebalanced: &{Type:rebalance start Claimed:map[] Released:map[] Current:map[]}
panic: non-positive interval for NewTicker

goroutine 14 [running]:
time.NewTicker(0x0, 0x0)
	D:/Go/src/time/tick.go:23 +0x14e
github.com/bsm/sarama-cluster.(*Consumer).cmLoop(0xc000120000, 0xc0000d8420)
	C:/Users/Administrator/go/pkg/mod/github.com/bsm/[email protected]+incompatible/consumer.go:452 +0x51
github.com/bsm/sarama-cluster.(*loopTomb).Go.func1(0xc0000e45c0, 0xc000034be0)
	C:/Users/Administrator/go/pkg/mod/github.com/bsm/[email protected]+incompatible/util.go:73 +0xb0
created by github.com/bsm/sarama-cluster.(*loopTomb).Go
	C:/Users/Administrator/go/pkg/mod/github.com/bsm/[email protected]+incompatible/util.go:69 +0x6d
exit status 2

到处谷哥,最后修改如下:

	//ticker := time.NewTicker(c.client.config.Consumer.Offsets.CommitInterval)
	ticker := time.NewTicker(c.client.config.Consumer.Offsets.AutoCommit.Interval)

这个库应该很多人用才对,为什么网上很少人说这个问题?

参考链接:

https://github.com/lovoo/goka/issues/211

你可能感兴趣的:(go)