go get
为例,使用如下命令:go get github.com/apache/rocketmq-clients/golang/v5
package mainimport ("context""fmt""log""os""strconv""time"rmq_client "github.com/apache/rocketmq-clients/golang/v5""github.com/apache/rocketmq-clients/golang/v5/credentials")const (Topic = "xxxxxx"// Endpoint 填写腾讯云提供的接入地址Endpoint = "xxxxxx"// AccessKey 添加配置的akAccessKey = "xxxxxx"// SecretKey 添加配置的skSecretKey = "xxxxxx")func main() {os.Setenv("mq.consoleAppender.enabled", "true")rmq_client.ResetLogger()// In most case, you don't need to create many producers, singleton pattern is more recommended.producer, err := rmq_client.NewProducer(&rmq_client.Config{Endpoint: Endpoint,Credentials: &credentials.SessionCredentials{AccessKey: AccessKey,AccessSecret: SecretKey,},},rmq_client.WithTopics(Topic),)if err != nil {log.Fatal(err)}// start producererr = producer.Start()if err != nil {log.Fatal(err)}// graceful stop producerdefer producer.GracefulStop()for i := 0; i < 10; i++ {// new a messagemsg := &rmq_client.Message{Topic: Topic,Body: []byte("this is a message : " + strconv.Itoa(i)),}// set keys and tagmsg.SetKeys("a", "b")msg.SetTag("ab")// send message in syncresp, err := producer.Send(context.TODO(), msg)if err != nil {log.Fatal(err)}for i := 0; i < len(resp); i++ {fmt.Printf("%#v\\n", resp[i])}// wait a momenttime.Sleep(time.Second * 1)}}
package mainimport ("context""fmt""log""os""time"rmq_client "github.com/apache/rocketmq-clients/golang/v5""github.com/apache/rocketmq-clients/golang/v5/credentials")const (Topic = "xxxxxx"ConsumerGroup = "xxxxxx"// Endpoint 填写腾讯云提供的接入地址Endpoint = "xxxxxx"// AccessKey 添加配置的akAccessKey = "xxxxxx"// SecretKey 添加配置的skSecretKey = "xxxxxx")var (// maximum waiting time for receive funcawaitDuration = time.Second * 5// maximum number of messages received at one timemaxMessageNum int32 = 16// invisibleDuration should > 20sinvisibleDuration = time.Second * 20// receive messages in a loop)func main() {// log to consoleos.Setenv("mq.consoleAppender.enabled", "true")rmq_client.ResetLogger()// In most case, you don't need to create many consumers, singleton pattern is more recommended.simpleConsumer, err := rmq_client.NewSimpleConsumer(&rmq_client.Config{Endpoint: Endpoint,ConsumerGroup: ConsumerGroup,Credentials: &credentials.SessionCredentials{AccessKey: AccessKey,AccessSecret: SecretKey,},},rmq_client.WithAwaitDuration(awaitDuration),rmq_client.WithSubscriptionExpressions(map[string]*rmq_client.FilterExpression{Topic: rmq_client.SUB_ALL,}),)if err != nil {log.Fatal(err)}// start simpleConsumererr = simpleConsumer.Start()if err != nil {log.Fatal(err)}// graceful stop simpleConsumerdefer simpleConsumer.GracefulStop()for {fmt.Println("start receive message")mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration)if err != nil {fmt.Println(err)}// ack messagefor _, mv := range mvs {simpleConsumer.Ack(context.TODO(), mv)fmt.Println(mv)}fmt.Println("wait a moment")fmt.Println()time.Sleep(time.Second * 1)}}
本页内容是否解决了您的问题?