Go SDK
本文介紹Go SDK的版本說明、使用說明和示例代碼。
使用說明
下載最新版Go SDK,解壓后進(jìn)入
aliyun-mns-go-sdk
主目錄。進(jìn)入
example
文件夾,在queue_example.go
或者topic_example.go
文件中修改endpoint為您的實(shí)例接入點(diǎn)。并在環(huán)境變量中設(shè)置ALIBABA_CLOUD_ACCESS_KEY_ID
和ALIBABA_CLOUD_ACCESS_KEY_SECRET
。接入點(diǎn)信息,您可以在控制臺的隊(duì)列詳情/主題詳情頁面的接入點(diǎn)區(qū)域查看。
示例代碼
隊(duì)列模型
本示例包括創(chuàng)建隊(duì)列、發(fā)送消息、接收和刪除消息、以及刪除隊(duì)列等操作。
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"time"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
)
func main() {
go func() {
log.Println(http.ListenAndServe("localhost:8080", nil))
}()
// Replace with your own endpoint.
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
client := ali_mns.NewClient(endpoint)
msg := ali_mns.MessageSendRequest{
MessageBody: "hello <\"aliyun-mns-go-sdk\">",
DelaySeconds: 0,
Priority: 8}
queueManager := ali_mns.NewMNSQueueManager(client)
queueName := "test-queue"
err := queueManager.CreateQueue(queueName, 0, 65536, 345600, 30, 0, 3)
time.Sleep(time.Duration(2) * time.Second)
if err != nil && !ali_mns.ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
queue := ali_mns.NewMNSQueue(queueName, client)
for i := 1; i < 10000; i++ {
ret, err := queue.SendMessage(msg)
go func() {
fmt.Println(queue.QPSMonitor().QPS())
}()
if err != nil {
fmt.Println(err)
} else {
logs.Pretty("response: ", ret)
}
endChan := make(chan int)
respChan := make(chan ali_mns.MessageReceiveResponse)
errChan := make(chan error)
go func() {
select {
case resp := <-respChan:
{
logs.Pretty("response: ", resp)
logs.Debug("change the visibility: ", resp.ReceiptHandle)
if ret, e := queue.ChangeMessageVisibility(resp.ReceiptHandle, 5); e != nil {
fmt.Println(e)
} else {
logs.Pretty("visibility changed", ret)
logs.Debug("delete it now: ", ret.ReceiptHandle)
if e := queue.DeleteMessage(ret.ReceiptHandle); e != nil {
fmt.Println(e)
}
endChan <- 1
}
}
case err := <-errChan:
{
fmt.Println(err)
endChan <- 1
}
}
}()
queue.ReceiveMessage(respChan, errChan, 30)
<-endChan
}
}
主題模型
本示例包括創(chuàng)建隊(duì)列、創(chuàng)建主題、訂閱主題和發(fā)布消息等操作。
package main
import (
"fmt"
"time"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
)
func main() {
// Replace with your own endpoint.
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
queueName := "test-queue"
topicName := "test-topic"
queueSubName := "test-sub-queue"
httpSubName := "test-sub-http"
client := ali_mns.NewClient(endpoint)
// 1. create a queue for receiving pushed messages
queueManager := ali_mns.NewMNSQueueManager(client)
err := queueManager.CreateSimpleQueue(queueName)
if err != nil && !ali_mns.ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
// 2. create the topic
topicManager := ali_mns.NewMNSTopicManager(client)
// topicManager.DeleteTopic("testTopic")
err = topicManager.CreateSimpleTopic(topicName)
if err != nil && !ali_mns.ERR_MNS_TOPIC_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
topic := ali_mns.NewMNSTopic(topicName, client)
// 3. subscribe to topic, the endpoint is queue
queueSub := ali_mns.MessageSubsribeRequest{
Endpoint: topic.GenerateQueueEndpoint(queueName),
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
// 4. subscribe to topic, the endpoint is HTTP(S)
httpSub := ali_mns.MessageSubsribeRequest{
Endpoint: "http://www.baidu.com",
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
err = topic.Subscribe(queueSubName, queueSub)
if err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
err = topic.Subscribe(httpSubName, httpSub)
if err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
/*
sub = ali_mns.MessageSubsribeRequest{
Endpoint: topic.GenerateMailEndpoint("a@b.com"),
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
err = topic.Subscribe("SubscriptionNameB", sub)
if (err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err)) {
fmt.Println(err)
return
}
*/
time.Sleep(time.Duration(2) * time.Second)
// 5. now publish message
msg := ali_mns.MessagePublishRequest{
MessageBody: "hello topic <\"aliyun-mns-go-sdk\">",
MessageAttributes: &ali_mns.MessageAttributes{
MailAttributes: &ali_mns.MailAttributes{
Subject: "AAA中文",
AccountName: "BBB",
},
},
}
_, err = topic.PublishMessage(msg)
if err != nil {
fmt.Println(err)
return
}
// 6. receive the message from queue
queue := ali_mns.NewMNSQueue(queueName, client)
endChan := make(chan int)
respChan := make(chan ali_mns.MessageReceiveResponse)
errChan := make(chan error)
go func() {
select {
case resp := <-respChan:
{
logs.Pretty("response: ", resp)
fmt.Println("change the visibility: ", resp.ReceiptHandle)
if ret, e := queue.ChangeMessageVisibility(resp.ReceiptHandle, 5); e != nil {
fmt.Println(e)
} else {
logs.Pretty("visibility changed", ret)
fmt.Println("delete it now: ", ret.ReceiptHandle)
if e := queue.DeleteMessage(ret.ReceiptHandle); e != nil {
fmt.Println(e)
}
endChan <- 1
}
}
case err := <-errChan:
{
fmt.Println(err)
endChan <- 1
}
}
}()
queue.ReceiveMessage(respChan, errChan, 30)
<-endChan
}
版本記錄
Version 1.0.6
更新日期 | 更新內(nèi)容 | 下載地址 |
2024-11-13 |
|
Version 1.0.5
更新日期 | 更新內(nèi)容 | 下載地址 |
2024-08-19 | 將 |
Version 1.0.4
更新日期 | 更新內(nèi)容 | 下載地址 |
2024-07-17 |
|
Version 1.0.3
更新日期 | 更新內(nèi)容 | 下載地址 |
2024-05-21 | 支持自定義HTTP Transport配置。 |
Version 1.0.2
更新日期 | 更新內(nèi)容 | 下載地址 |
2021-03-05 | 支持OpenService接口。 |
Version 1.0.1
更新日期 | 更新內(nèi)容 | 下載地址 |
2021-01-15 |
|
Version 1.0.0
更新日期 | 更新內(nèi)容 | 下載地址 |
2019-04-30 |
|