このトピックでは、SDK for Go のリリースノート、使用方法、およびサンプルコードについて説明します。
使用方法
-
最新バージョンの SDK for Go をダウンロードして解凍し、
aliyun-mns-go-sdkルートディレクトリに移動します。 -
exampleディレクトリに移動します。queue_example.goまたはtopic_example.goファイルで、プレースホルダーのエンドポイントをご利用のインスタンスのエンドポイントに置き換えます。次に、ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数を設定します。ご利用のインスタンスのエンドポイントは、Simple Message Queue コンソールの キューの詳細 または トピックの詳細 ページの エンドポイント セクションで確認できます。このセクションでは、パブリックエンドポイントと内部エンドポイントの両方が提供されます。
サンプルコード
キューモデル
この例では、キューを作成し、メッセージを送信し、メッセージを受信して削除し、最後にキューを削除します。
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))
}()
// ご利用のエンドポイントに置き換えてください。
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
}
}
トピックモデル
この例では、キューとトピックを作成し、トピックをサブスクライブして、メッセージを公開します。
package main
import (
"fmt"
"time"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
)
func main() {
// ご利用のエンドポイントに置き換えてください。
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. プッシュされたメッセージを受信するためのキューを作成します
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. トピックを作成します
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. トピックをサブスクライブします。エンドポイントはキューです
queueSub := ali_mns.MessageSubsribeRequest{
Endpoint: topic.GenerateQueueEndpoint(queueName),
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
// 4. トピックをサブスクライブします。エンドポイントは 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. メッセージを公開します
msg := ali_mns.MessagePublishRequest{
MessageBody: "hello topic <\"aliyun-mns-go-sdk\">",
MessageAttributes: &ali_mns.MessageAttributes{
MailAttributes: &ali_mns.MailAttributes{
Subject: "AAA Chinese characters",
AccountName: "BBB",
},
},
}
_, err = topic.PublishMessage(msg)
if err != nil {
fmt.Println(err)
return
}
// 6. キューからメッセージを受信します
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.11
|
リリース日 |
説明 |
ダウンロード |
|
2025-03-20 |
|
Version 1.0.10
|
リリース日 |
説明 |
ダウンロード |
|
2025-03-17 |
|
Version 1.0.9
|
リリース日 |
説明 |
ダウンロード |
|
2025-03-11 |
issues#26 を修正しました。これは、認証情報パッケージに |
Version 1.0.8
|
リリース日 |
説明 |
ダウンロード |
|
2025-02-06 |
キューの作成時およびプロパティの更新時に |
Version 1.0.7
|
リリース日 |
説明 |
ダウンロード |
|
2025-01-23 |
|
Version 1.0.6
|
リリース日 |
説明 |
ダウンロード |
|
2024-11-13 |
|
Version 1.0.5
|
リリース日 |
説明 |
ダウンロード |
|
2024-08-19 |
ビルドの失敗を修正するために、 |
Version 1.0.4
|
リリース日 |
説明 |
ダウンロード |
|
2024-07-17 |
|
Version 1.0.3
|
リリース日 |
説明 |
ダウンロード |
|
2024-05-21 |
カスタム HTTP Transport 設定をサポートしました。 |
Version 1.0.2
|
リリース日 |
説明 |
ダウンロード |
|
2021-03-05 |
OpenService API をサポートしました。 |
Version 1.0.1
|
リリース日 |
説明 |
ダウンロード |
|
2021-01-15 |
|
Version 1.0.0
|
リリース日 |
説明 |
ダウンロード |
|
2019-04-30 |
|