Release Notes
Announcements
go get as an example, run the following command: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: Enter the access address provided by Tencent Cloud.Endpoint = "xxxxxx"// AccessKey: Add the configured AccessKey.AccessKey = "xxxxxx"// SecretKey: Add the configured SecretKey.SecretKey = "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)}}
Parameter | Description |
AccessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
SecretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
Endpoints | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
Topic | Topic name. You can copy the name from the Topic Management page in the console. |
package mainimport ("context""fmt""log""os""os/signal" "sync" "syscall""time"rmq_client "github.com/apache/rocketmq-clients/golang/v5""github.com/apache/rocketmq-clients/golang/v5/credentials")const (Topic = "xxxxxx"ConsumerGroup = "xxxxxx"// Endpoint: Enter the access address provided by Tencent Cloud.Endpoint = "xxxxxx"// AccessKey: Add the configured AccessKey.AccessKey = "xxxxxx"// SecretKey: Add the configured SecretKey.SecretKey = "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 concurrency receiveConcurrency = 6)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 func() {if r := recover(); r != nil {fmt.Println(r)}_ = simpleConsumer.GracefulStop()}()fmt.Println("start receive message")// Each Receive call will only select one broker queue to pop messages.// Enable multiple consumption goroutines to reduce message end-to-end latency.ch := make(chan struct{})wg := &sync.WaitGroup{}for i := 0; i < receiveConcurrency; i++ {wg.Add(1)go func() {defer wg.Done()for {select {case <-ch:returndefault:mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration)if err != nil {fmt.Println("receive message error: " + err.Error())}// ack messagefor _, mv := range mvs {fmt.Println(mv)if err := simpleConsumer.Ack(context.TODO(), mv); err != nil {fmt.Println("ack message error: " + err.Error())}}}}}()}exit := make(chan os.Signal, 1)signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)// wait for exit<-exitclose(ch)wg.Wait()}
Parameter | Description |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
endpoints | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
consumerGroup | Consumer group name. You can copy the name from the Group Management page in the console. |
topic | Topic name. You can copy the name from the Topic Management page in the console. |
フィードバック