閱讀382 返回首頁    go 穀歌


主題+HttpEndpoint使用手冊__Python SDK_SDK使用手冊_消息服務-阿裏雲

本文檔介紹如何使用python sdk中的sample代碼,完成創建主題、創建訂閱、啟動 HttpEndpoint、發布消息、查看HttpEndpoint接收消息和刪除主題操作。

1. 準備

  • 下載最新版python sdk,解壓後進入mns_python_sdk子目錄;
  • 打開sample.cfg文件,配置AccessKeyID、AccessKeySecret和Endpoint;
    • AccessKeyId、AccessKeySecret
    • Endpoint
      • 訪問MNS的接入地址,登陸MNS控製台 單擊右上角 獲取Endpoint 查看;
      • 不同地域的接入地址不同;
    • SecurityToken
      • 阿裏雲訪問控製服務提供的短期訪問權限憑證,直接使用阿裏雲賬號或者子賬號訪問不需要配置該項,了解詳情;
  • 進入sample目錄,後續使用的腳本都在這裏;

2. 創建主題

運行 createtopic.py 創建主題;
默認創建的主題名稱是 MySampleTopic,也可以通過參數指定主題名稱;
主題詳細信息請參考詳情;

  • 運行
  1. $python createtopic.py MyTopic1
  2. Create Topic Succeed! TopicName:MyTopic1
  • 核心代碼

endpoint,accid,acckey和token從第1步的配置文件中讀取;

  1. #init my_account, my_topic
  2. my_account = Account(endpoint, accid, acckey, token)
  3. topic_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleTopic"
  4. my_topic = my_account.get_topic(topic_name)
  5. #you can get more information of TopicMeta from mns/topic.py
  6. topic_meta = TopicMeta()
  7. try:
  8. topic_url = my_topic.create(topic_meta)
  9. print "Create Topic Succeed! TopicName:%sn" % topic_name
  10. except MNSExceptionBase, e:
  11. if e.type == "TopicAlreadyExist":
  12. print "Topic already exist, please delete it before creating or use it directly."
  13. sys.exit(0)
  14. print "Create Topic Fail! Exception:%sn" % e

3. 啟動 HttpEndpoint

運行 simple_http_notify_endpoint.py 啟動 HttpEndpoint;
腳本啟動後,輸出該腳本的監聽地址,這個地址後續作為創建訂閱的Endpoint參數,用於接收 MNS 推送消息的請求;

  • 服務器功能

    • 對 MNS 推送消息請求做簽名驗證,如果錯誤,返回MNS 403;
    • 解析推送請求的 body,打印到日誌中,如果解析錯誤,返回MNS 400;
    • 如果驗權和解析 body 均正常,返回 MNS 201;
  • 運行

  1. $python simple_http_notify_endpoint.py
  2. Start Endpoint! Address: https://10.101.161.37:8080

由於 simple_http_notify_endpoint.py 的代碼較多,請直接查看sdk 中的源碼。

4. 創建訂閱

運行 subscribe.py 創建訂閱;
第一個參數指定接收消息的 HttpEndpoint,使用第3步中腳本輸出的Address;
第二個參數指定訂閱的主題名稱,如果第2步中指定了主題名稱,這裏同樣指定主題名稱;
第三個參數指定訂閱的名稱,默認是 MySampleTopic-Sub;
訂閱詳細信息請參考詳情;

  • 運行
  1. $python subscribe.py https://10.101.161.37:8080 MyTopic1 MyTopic1-Sub1
  2. Create Subscription Succeed! TopicName:MyTopic1 SubName:MyTopic1-Sub1 Endpoint:https://10.101.161.37:8080
  • 核心代碼

endpoint,accid,acckey和token從第1步的配置文件中讀取;

  1. sub_endpoint = sys.argv[1]
  2. #init my_account, my_topic, my_sub
  3. my_account = Account(endpoint, accid, acckey, token)
  4. topic_name = sys.argv[2] if len(sys.argv) > 2 else "MySampleTopic"
  5. my_topic = my_account.get_topic(topic_name)
  6. sub_name = sys.argv[3] if len(sys.argv) > 3 else "MySampleTopic-Sub"
  7. my_sub = my_topic.get_subscription(sub_name)
  8. #you can get more information of SubscriptionMeta from mns/subscription.py
  9. sub_meta = SubscriptionMeta(sub_endpoint)
  10. try:
  11. topic_url = my_sub.subscribe(sub_meta)
  12. print "Create Subscription Succeed! TopicName:%s SubName:%s Endpoint:%sn" % (topic_name, sub_name, sub_endpoint)
  13. except MNSExceptionBase, e:
  14. if e.type == "TopicNotExist":
  15. print "Topic not exist, please create topic."
  16. sys.exit(0)
  17. elif e.type == "SubscriptionAlreadyExist":
  18. print "Subscription already exist, please unsubscribe or use it directly."
  19. sys.exit(0)
  20. print "Create Subscription Fail! Exception:%sn" % e

5. 發布消息

運行publishmessage.py 發布多條消息到主題中;
如果第2步中指定了主題名稱,這裏同樣通過第一個參數指定主題名稱;
消息詳細信息請參考詳情;

  • 運行
  1. $python publishmessage.py MyTopic1
  2. ==========Publish Message To Topic==========
  3. TopicName:MyTopic1
  4. MessageCount:3
  5. Publish Message Succeed. MessageBody:I am test message 0. MessageID:F6EA56633844DBFC-1-154BDFB8059-200000004
  6. Publish Message Succeed. MessageBody:I am test message 1. MessageID:F6EA56633844DBFC-1-154BDFB805F-200000005
  7. Publish Message Succeed. MessageBody:I am test message 2. MessageID:F6EA56633844DBFC-1-154BDFB8062-200000006
  • 核心代碼

endpoint,accid,acckey和token從第1步的配置文件中讀取;

  1. #init my_account, my_topic
  2. my_account = Account(endpoint, accid, acckey, token)
  3. topic_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleTopic"
  4. my_topic = my_account.get_topic(topic_name)
  5. #publish some messages
  6. msg_count = 3
  7. print "%sPublish Message To Topic%snTopicName:%snMessageCount:%sn" % (10*"=", 10*"=", topic_name, msg_count)
  8. for i in range(msg_count):
  9. try:
  10. msg_body = "I am test message %s." % i
  11. msg = TopicMessage(msg_body)
  12. re_msg = my_topic.publish_message(msg)
  13. print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id)
  14. except MNSExceptionBase,e:
  15. if e.type == "TopicNotExist":
  16. print "Topic not exist, please create it."
  17. sys.exit(1)
  18. print "Publish Message Fail. Exception:%s" % e

6. 查看 HttpEndpoint 接收消息

第5步發布了多條消息到主題中,MNS 會將發布的消息推送給第3步啟動的 HttpEndpoint;
HttpEndpoint 在接收到消息推送請求後,會記錄兩種日誌:access_log 和 endpoint_log;

  • access log
    • 啟動 HttpEndpoint 的地方會打印access log,顯示推送消息請求的基本信息,從左往右依次是:
      RequestTime RequestVersion ReturnCode URI HTTPVersion RequestLength Host Agent MNSRequestID MNSVersion
    • 除打印到屏幕上,access log會寫到日誌文件中,文件名格式是access_log.$port,本文檔對應的日誌文件是access_log.8080;
    • 第5步發布消息對應的推送請求access log如下:
  1. $python simple_http_notify_endpoint.py
  2. Start Endpoint! Address: https://10.101.161.37:8080
  3. [17/May/2016 17:10:56]"POST" "201" "/notifications" "HTTP/1.1" "495" "10.101.161.37:8080" "Aliyun Notification Service Agent" "573AE020B2B71CFC6801A6EF" "2015-06-06"
  4. [17/May/2016 17:10:56]"POST" "201" "/notifications" "HTTP/1.1" "495" "10.101.161.37:8080" "Aliyun Notification Service Agent" "573AE020B2B71CFC6801A712" "2015-06-06"
  5. [17/May/2016 17:10:56]"POST" "201" "/notifications" "HTTP/1.1" "495" "10.101.161.37:8080" "Aliyun Notification Service Agent" "573AE020B2B71CFC6801A704" "2015-06-06"
  • endpoint log
    • 記錄每個請求的詳細信息,包含完整的header、body以及解析後消息各屬性的信息;
    • 日誌的文件名格式是 endpoint_log.$port,本文檔對應的日誌文件是endpoint_log.8080
    • 第5步發布消息對應的推送請求的endpoint log如下:
  1. $cat endpoint_log.8080
  2. ...
  3. [2016-05-17 17:10:56] [root] [INFO] [simple_http_notify_endpoint.py:47] [1096657216] Notify Message Succeed!
  4. MessageMD5 : 075C3D4AEB2D2F2D6A4C17C9D6DBBEBB
  5. TopicOwner : 1269128356620446
  6. PublishTime : 1463476256857
  7. Subscriber : 1269128356620446
  8. MessageTag :
  9. SubscriptionName : MyTopic1-Sub1
  10. MessageId : F6EA56633844DBFC-1-154BDFB8059-200000004
  11. Message : I am test message 0.
  12. TopicName : MyTopic1
  13. [2016-05-17 17:10:56] [root] [INFO] [simple_http_notify_endpoint.py:47] [1123260736] Notify Message Succeed!
  14. MessageMD5 : 3BCFB142A3CC597F5D409BFE9DB1B885
  15. TopicOwner : 1269128356620446
  16. PublishTime : 1463476256866
  17. Subscriber : 1269128356620446
  18. MessageTag :
  19. SubscriptionName : MyTopic1-Sub1
  20. MessageId : F6EA56633844DBFC-1-154BDFB8062-200000006
  21. Message : I am test message 2.
  22. TopicName : MyTopic1
  23. [2016-05-17 17:10:56] [root] [INFO] [simple_http_notify_endpoint.py:47] [1112770880] Notify Message Succeed!
  24. MessageMD5 : 8356BC7FFBD22CC971BE7FF7427202B6
  25. TopicOwner : 1269128356620446
  26. PublishTime : 1463476256863
  27. Subscriber : 1269128356620446
  28. MessageTag :
  29. SubscriptionName : MyTopic1-Sub1
  30. MessageId : F6EA56633844DBFC-1-154BDFB805F-200000005
  31. Message : I am test message 1.
  32. TopicName : MyTopic1

7. 刪除主題

運行deletetopic.py 刪除主題;
如果第2步中指定了主題名稱,這裏同樣通過第一個參數指定主題名稱;

  • 運行
  1. $python deletetopic.py MyTopic1
  2. Delete Topic Succeed! TopicName:MyTopic1
  • 核心代碼
  1. #init my_account, my_topic
  2. my_account = Account(endpoint, accid, acckey, token)
  3. topic_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleTopic"
  4. my_topic = my_account.get_topic(topic_name)
  5. try:
  6. my_topic.delete()
  7. print "Delete Topic Succeed! TopicName:%sn" % topic_name
  8. except MNSExceptionBase, e:
  9. print "Delete Topic Fail! Exception:%sn" % e

最後更新:2016-11-23 17:16:08

  上一篇:go 並發測試示例代碼__Java SDK_SDK使用手冊_消息服務-阿裏雲
  下一篇:go 主題+QueueEndpoint使用手冊__Python SDK_SDK使用手冊_消息服務-阿裏雲