github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/client/mq/kafka/consumer/example_consumer_test.go (about)

     1  package consumer
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Shopify/sarama"
     6  	"github.com/weedge/lib/log"
     7  	"github.com/weedge/lib/strings"
     8  )
     9  
    10  type testMsg struct {
    11  }
    12  
    13  func (m *testMsg) Consumer(msg *sarama.ConsumerMessage) (err error) {
    14  	log.Info(fmt.Sprintf("msg: %+v", msg))
    15  
    16  	if strings.BytesToString(msg.Value) == "error" {
    17  		err = fmt.Errorf("msg:%v is err", msg)
    18  		return
    19  	}
    20  
    21  	return
    22  }
    23  
    24  func ExampleConsumerGroup_Ops() {
    25  	cg, err := NewConsumerGroup("consumer.group.test", &testMsg{}, nil,
    26  		WithVersion("2.8.0"),                                           //kafka version
    27  		WithBrokerList("127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094"), //兼容kraft mode
    28  		WithGroupId("consumer.group.test"),
    29  		WithTopicList("sarama"),
    30  		WithInitialOffset("oldest"),
    31  		WithReBalanceStrategy("sticky"),
    32  	)
    33  	if err != nil {
    34  		println(err)
    35  	}
    36  	defer cg.Close()
    37  	//cg.StartWithDeadline(time.Now().Add(10 * time.Second))
    38  	//cg.StartWithTimeOut(10 * time.Second)
    39  	cg.Start()
    40  
    41  	// output:
    42  	//
    43  }
    44  
    45  func ExampleBizConsumerGroup_Ops() {
    46  	cg, err := NewConsumerGroup("consumer.group.test", &testMsg{}, nil,
    47  		WithVersion("2.8.0"),                                           //kafka version
    48  		WithBrokerList("127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094"), //兼容kraft mode
    49  		WithGroupId("consumer.group.test"),
    50  		WithTopicList("sarama"),
    51  		WithInitialOffset("oldest"),
    52  		WithReBalanceStrategy("sticky"),
    53  	)
    54  	if err != nil {
    55  		println(err)
    56  	}
    57  	defer cg.Close()
    58  	//cg.StartWithDeadline(time.Now().Add(10 * time.Second))
    59  	//cg.StartWithTimeOut(10 * time.Second)
    60  	cg.Start()
    61  
    62  	// output:
    63  	//
    64  }