-
Notifications
You must be signed in to change notification settings - Fork 117
/
producer.go
60 lines (54 loc) · 2.08 KB
/
producer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"time"
"strconv"
"github.com/aliyunmq/mq-http-go-sdk"
)
func main() {
// 设置HTTP接入域名(此处以公共云生产环境为例)
endpoint := "${HTTP_ENDPOINT}"
// AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
accessKey := "${ACCESS_KEY}"
// SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
secretKey := "${SECRET_KEY}"
// 所属的 Topic
topic := "${TOPIC}"
// Topic所属实例ID,默认实例为空
instanceId := "${INSTANCE_ID}"
client := mq_http_sdk.NewAliyunMQClient(endpoint, accessKey, secretKey, "")
mqProducer := client.GetProducer(instanceId, topic)
// 循环发送4条消息
for i := 0; i < 4; i++ {
var msg mq_http_sdk.PublishMessageRequest
if i%2 == 0 {
msg = mq_http_sdk.PublishMessageRequest{
MessageBody: "hello mq!", //消息内容
MessageTag: "", // 消息标签
Properties: map[string]string{}, // 消息属性
}
// 设置KEY
msg.MessageKey = "MessageKey"
// 设置属性
msg.Properties["a"] = strconv.Itoa(i)
} else {
msg = mq_http_sdk.PublishMessageRequest{
MessageBody: "hello mq timer!", //消息内容
MessageTag: "", // 消息标签
Properties: map[string]string{}, // 消息属性
}
// 设置属性
msg.Properties["a"] = strconv.Itoa(i)
// 定时消息, 定时时间为10s后, 值为毫秒级别的Unix时间戳
msg.StartDeliverTime = time.Now().UTC().Unix() * 1000 + 10 * 1000
}
ret, err := mqProducer.PublishMessage(msg)
if err != nil {
fmt.Println(err)
return
} else {
fmt.Printf("Publish ---->\n\tMessageId:%s, BodyMD5:%s, \n", ret.MessageId, ret.MessageBodyMD5)
}
time.Sleep(time.Duration(100) * time.Millisecond)
}
}