Browse Source

增加测试方法

master v1.0.1
loshiqi 2 years ago
parent
commit
c72d66d083
2 changed files with 41 additions and 8 deletions
  1. +11
    -8
      conn.go
  2. +30
    -0
      conn_test.go

+ 11
- 8
conn.go View File

@ -7,19 +7,22 @@ import (
"log"
)
var MqPool *rocketmq.Producer
var MqPool rocketmq.Producer
func MqConnect() {
MqPool, err := rocketmq.NewProducer(
producer.WithGroupName("ProductService"),
producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"127.0.0.1:9876"})),
func MqConnect(group string, resolver []string) {
p, err := rocketmq.NewProducer(
producer.WithGroupName(group),
producer.WithNsResolver(primitive.NewPassthroughResolver(resolver)),
//producer.WithCreateTopicKey("product"),
producer.WithRetry(1))
if err != nil {
log.Println("New ProductService producer failed", err)
log.Println("New "+group+" producer failed", err)
return
}
err = MqPool.Start()
err = p.Start()
if err != nil {
log.Println("ProductService producer start failed", err)
log.Println(group+" producer start failed", err)
return
}
MqPool = p
}

+ 30
- 0
conn_test.go View File

@ -0,0 +1,30 @@
package rocketmq
import (
"encoding/json"
"github.com/apache/rocketmq-client-go/v2/primitive"
"log"
"testing"
// "tgo/helper"
"context"
)
func Test_Send(t *testing.T) {
MqConnect("ProductService", []string{"127.0.0.1:9876"})
log.Println("2", MqPool)
mq_data := map[string]interface{}{
"a": "a",
}
topic := "product"
tag := "collect_product"
msg_str, _ := json.Marshal(mq_data)
msg1 := primitive.NewMessage(topic, msg_str)
msg1.WithTag(tag)
res, err := MqPool.SendSync(context.Background(), msg1)
if err != nil {
log.Printf("send tag sync message error:%s\n", err)
} else {
log.Printf("send tag sync message success. result=%s\n", res.String())
}
}

Loading…
Cancel
Save