订阅确认指南

订阅确认功能现在支持 webhook 和电子邮件,同时支持 mongoDB 和 redis 后端。本指南展示了如何使用此功能

Webhook

注意

您应该确保消息通知已启用。默认情况下,[storage] 部分中的 message_pipeline 配置选项应设置为:message_pipeline = zaqar.notification.notifier

1. 设置配置选项“require_confirmation”并将策略添加到 policy.yaml 文件中。然后重启 Zaqar-wsgi 服务

In the config file:
[notification]
require_confirmation = True

In the policy.yaml file:
"subscription:confirm": ""
  1. 创建一个订阅。

这里使用 zaqar/samples/zaqar/subscriber_service_sample.py 作为订阅者端点的示例。所以在第 2 步之前,您应该先启动订阅者服务。可以通过以下命令简单地启动该服务

python zaqar/samples/zaqar/subscriber_service_sample.py

服务的默认端口是 5678。如果想使用新端口,命令将如下所示

python zaqar/samples/zaqar/subscriber_service_sample.py new_port_number

默认情况下,该服务不会自动确认订阅。如果想这样做,命令将如下所示

python zaqar/samples/zaqar/subscriber_service_sample.py --auto-confirm

然后创建一个订阅

curl -i -X POST http://10.229.47.217:8888/v2/queues/test/subscriptions \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4" \
-d '{"subscriber":"http://10.229.47.217:5678", "ttl":3600, "options":{}}'

响应

HTTP/1.1 201 Created
content-length: 47
content-type: application/json; charset=UTF-8
location: http://10.229.47.217:8888/v2/queues/test/subscriptions
Connection: close
{"subscription_id": "576256b03990b480617b4063"}

同时,如果订阅者示例服务没有通过“–auto confirm”启动,您将在订阅者示例服务中收到一个 POST 请求,请求如下

WARNING:root:{"UnsubscribeBody": {"confirmed": false}, "URL-Methods": "PUT",
"X-Project-ID": "51be2c72393e457ebf0a22a668e10a64",
"URL-Paths": "/v2/queues/test/subscriptions/576256b03990b480617b4063/confirm",
"URL-Expires": "2016-07-06T04:35:56", "queue_name": "test",
"SubscribeURL": ["/v2/queues/test/subscriptions/576256b03990b480617b4063/confirm"],
"SubscribeBody": {"confirmed": true},
"URL-Signature": "d4038a40589cdb61cd13d5a6997472f5be779db441dd8fe0c597a6e465f30c41",
"Message": "You have chosen to subscribe to the queue: test",
"Message_Type": "SubscriptionConfirmation"}
10.229.47.217 - - [06/Jul/2016 11:35:56] "POST / HTTP/1.1" 200 -

如果您使用“–auto confirm”启动示例服务,请直接转到第 6 步,因为第 5 步将由服务自动完成。

3. 获取订阅。请求

curl -i -X GET http://10.229.47.217:8888/v2/queues/test/subscriptions/576256b03990b480617b4063 \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4"

响应

HTTP/1.1 200 OK
content-length: 154
content-type: application/json; charset=UTF-8
Connection: close
{"confirmed": false, "age": 73, "id": "576256b03990b480617b4063",
"subscriber": "http://10.229.47.217:5678", "source": "test", "ttl": 3600, "options": {}}

您会发现“confirmed”属性默认情况下为 false。

4. 向订阅的队列发布消息。请求

curl -i -X POST http://10.229.47.217:8888/v2/queues/test/messages \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4" \
-d '{"messages": [{"ttl": 3600,"body": "test123"}]}'

响应

HTTP/1.1 201 Created
content-length: 68
content-type: application/json; charset=UTF-8
location: http://10.229.47.217:8888/v2/queues/test/messages?ids=57624dee3990b4634d71bb4a
Connection: close
{"resources": ["/v2/queues/test/messages/57624dee3990b4634d71bb4a"]}

订阅者没有收到任何消息,您将在 zaqar-wsgi 中找到一条日志信息。

2016-07-06 11:37:57.929 98400 INFO zaqar.notification.notifier
[(None,)2473911afe2642c0b74d7e1200d9bba7 51be2c72393e457ebf0a22a668e10a64 - - -]
The subscriber http://10.229.47.217:5678 is not confirmed.

5. 使用第 3 步中显示的信息确认订阅。请求

curl -i -X PUT http://10.229.47.217:8888/v2/queues/test/subscriptions/576256b03990b480617b4063/confirm \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "URL-Methods: PUT" -H "X-Project-ID: 51be2c72393e457ebf0a22a668e10a64" \
-H "URL-Signature: d28dced4eabbb09878a73d9a7a651df3a3ce5434fcdb6c3727decf6c7078b282" \
-H "URL-Paths: /v2/queues/test/subscriptions/576256b03990b480617b4063/confirm" \
-H "URL-Expires: 2016-06-16T08:35:12" -d '{"confirmed": true}'

响应

HTTP/1.1 204 No Content
location: /v2/queues/test/subscriptions/576256b03990b480617b4063/confirm
Connection: close

6. 重复第 3 步以获取订阅。请求

curl -i -X GET http://10.229.47.217:8888/v2/queues/test/subscriptions/576256b03990b480617b4063 \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4"

响应

HTTP/1.1 200 OK
content-length: 155
content-type: application/json; charset=UTF-8
Connection: close
{"confirmed": true, "age": 1370, "id": "576256b03990b480617b4063",
"subscriber": "http://10.229.47.217:5678", "source": "test", "ttl": 3600,
"options": {}}

订阅现在已确认。

7. 重复第 4 步以发布新消息。请求

curl -i -X POST http://10.229.47.217:8888/v2/queues/test/messages \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4" \
-d '{"messages": [{"ttl": 3600,"body": "test123"}]}'

响应

HTTP/1.1 201 Created
content-length: 68
content-type: application/json; charset=UTF-8
location: http://10.229.47.217:8888/v2/queues/test/messages?ids=5762526d3990b474c80d5483
Connection: close
{"resources": ["/v2/queues/test/messages/5762526d3990b474c80d5483"]}

然后在订阅者示例服务中,您将收到一个请求

WARNING:root:{"body": {"event": "BackupStarted"}, "queue_name": "test",
"Message_Type": "Notification", "ttl": 3600}
10.229.47.217 - - [06/Jul/2016 13:19:07] "POST / HTTP/1.1" 200 -

8. 取消订阅。请求

curl -i -X PUT http://10.229.47.217:8888/v2/queues/test/subscriptions/576256b03990b480617b4063/confirm \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "URL-Methods: PUT" -H "X-Project-ID: 51be2c72393e457ebf0a22a668e10a64" \
-H "URL-Signature: d28dced4eabbb09878a73d9a7a651df3a3ce5434fcdb6c3727decf6c7078b282" \
-H "URL-Paths: /v2/queues/test/subscriptions/576256b03990b480617b4063/confirm" \
-H "URL-Expires: 2016-06-16T08:35:12" -d '{"confirmed": false}'

响应

HTTP/1.1 204 No Content
location: /v2/queues/test/subscriptions/576256b03990b480617b4063/confirm
Connection: close

然后尝试发布消息。订阅者将不再收到通知。

电子邮件

1. 对于电子邮件确认方式,还需要设置配置选项“external_confirmation_url”、“subscription_confirmation_email_template”和“unsubscribe_confirmation_email_template”。

确认页面 URL 将在电子邮件订阅确认之前用于电子邮件中,此页面不在 Zaqar 服务器上托管,用户应构建自己的 Web 服务来提供此网页。

subscription_confirmation_email_template 允许用户自定义订阅确认电子邮件内容,包括主题、正文和发件人。

unsubscribe_confirmation_email_template 允许用户自定义取消订阅确认电子邮件内容,包括主题、正文和发件人。

In the config file:
[notification]
require_confirmation = True
external_confirmation_url = http://web_service_url/
subscription_confirmation_email_template = topic:Zaqar Notification - Subscription Confirmation,\
                                          body:'You have chosen to subscribe to the queue: {0}. This queue belongs to project: {1}. To confirm this subscription, click or visit this link below: {2}',\
                                          sender:Zaqar Notifications <no-reply@openstack.org>
unsubscribe_confirmation_email_template = topic: Zaqar Notification - Unsubscribe Confirmation,\
                                          body:'You have unsubscribed successfully to the queue: {0}. This queue belongs to project: {1}. To resubscribe this subscription, click or visit this link below: {2}',\
                                          sender:Zaqar Notifications <no-reply@openstack.org>

In the policy.yaml file:
"subscription:confirm": ""

2. 创建订阅。对于电子邮件确认,您应该像这样创建订阅

curl -i -X POST http://10.229.47.217:8888/v2/queues/test/subscriptions \
-H "Content-type: application/json" \
-H "Client-ID: de305d54-75b4-431b-adb2-eb6b9e546014" \
-H "X-Auth-Token: 440b677561454ea8a7f872201dd4e2c4" \
-d '{"subscriber":"your email address", "ttl":3600, "options":{}}'

响应

HTTP/1.1 201 Created
content-length: 47
content-type: application/json; charset=UTF-8
location: http://10.229.47.217:8888/v2/queues/test/subscriptions
Connection: close
{"subscription_id": "576256b03990b480617b4063"}

订阅创建后,Zaqar 将向订阅者的电子邮件地址发送一封电子邮件。该电子邮件指定了如何确认订阅。

  1. 点击电子邮件正文中的确认页面链接

4. 确认页面将自动将订阅确认请求发送到 Zaqar 服务器。用户也可以选择通过点击此页面中的取消订阅链接来取消订阅,这将导致 Zaqar 取消此订阅并发送另一封电子邮件以通知此取消订阅操作。Zaqar 提供了这两个网页的示例,可以帮助用户构建自己的页面

zaqar/sample/html/subscriptionConfirmation.html
zaqar/sample/html/unsubscriptionConfirmation.html

用户可以将这些页面放置在 Web 服务器(如 Apache)中,以便通过浏览器访问它们,因此 external_confirmation_url 将如下所示

http://127.0.0.1:8080/subscriptionConfirmation.html

对于 CORS,这里使用 zaqar/samples/html/confirmation_web_service_sample.py 作为示例 Web 服务,它将中继确认请求到 Zaqar 服务器。所以在第 3 步之前,您应该先启动 Web 服务。可以通过以下命令简单地启动该服务

python zaqar/samples/html/confirmation_web_service_sample.py

服务的默认端口是 5678。如果想使用新端口,命令将如下所示

python zaqar/samples/html/confirmation_web_service_sample.py new_port_number