Orion Context Broker中QuantumLeap订阅创建后无法查询求助
问题情况
执行POST请求创建QuantumLeap订阅后,收到HTTP 201 Created响应,Orion服务日志也明确记录订阅已创建,但使用GET请求查询订阅时返回空数组。
相关请求与日志
创建订阅的请求命令
curl -iX POST 'http://localhost:1026/v2/subscriptions/' \ -H 'Content-Type: application/json' \ -H 'Fiware-Service: myClassRoom' \ -H 'Fiware-ServicePath: /' \ -d '{ "description": "Notify QuantumLeap when the temperature changes", "subject": { "entities": [{ "idPattern": "SensTemp.*"}], "condition": { "attrs": [ "temperature"] } }, "notification": { "http": { "url": "http://quantum-leap:8668/v2/notify" }, "attrs": ["temperature"], "metadata": ["dateCreated", "dateModified"] }, "expires": "2025-02-03T08:00:00.00Z", "throttling": 5 }'
创建响应
HTTP/1.1 201 Created
Connection: Keep-Alive
Content-Length: 0
Location: /v2/subscriptions/62e3a39b8aa7c71c4b3e6c05
Fiware-Correlator: 0b597668-0f1e-11ed-b343-0242ac120005
Date: Fri, 29 Jul 2022 09:08:43 GMT
原查询订阅的请求命令(存在错误)
curl -X GET --url 'http://localhost:1026/v2/subscriptions/
Orion服务日志
srdjan-orion-1 | time=2022-07-29T09:18:31.342Z | lvl=INFO | corr=69e57bcc-0f1f-11ed-abf0-0242ac120005 | trans=1659082790-602-00000000170 | from=172.18.0.1 | srv=myclassroom | subsrv=/ | comp=Orion | op=logTracing.cpp[148]:logInfoRequestWithPayload | msg=Request received: POST /v2/subscriptions/, request payload (377 bytes): { "description": "Notify QuantumLeap when the temperature changes", "subject": { "entities": [ { "idPattern": "SensTemp" } ], "condition": { "attrs": [ "temperature" ] } }, "notification": { "http": { "url": "http://quantum-leap:8668/v2/notify" }, "attrs": [ "temperature" ], "metadata": ["dateCreated", "dateModified"] },"expires": "2025-02-03T08:00:00.00Z", "throttling": 5 }, response code: 201
问题原因及解决方案
1. 查询请求缺少Fiware上下文头部
Orion的订阅与Fiware-Service和Fiware-ServicePath强绑定,创建时指定了这两个头部,查询时必须携带相同的头部才能访问对应上下文的订阅。原查询命令未携带这些头部,默认会查询空服务上下文,自然无法找到已创建的订阅。
2. 查询命令URL格式错误
原查询命令的URL未闭合单引号,且路径末尾多余斜杠,导致请求本身存在语法错误。
正确的查询命令
查询所有订阅
curl -X GET 'http://localhost:1026/v2/subscriptions' \ -H 'Fiware-Service: myClassRoom' \ -H 'Fiware-ServicePath: /'
根据订阅ID查询单个订阅(使用创建响应中Location头的ID)
curl -X GET 'http://localhost:1026/v2/subscriptions/62e3a39b8aa7c71c4b3e6c05' \ -H 'Fiware-Service: myClassRoom' \ -H 'Fiware-ServicePath: /'
额外检查点
- 注意
Fiware-Service的大小写:Orion日志中记录的srv=myclassroom为全小写,虽然Orion对服务名大小写不敏感,但查询时保持与创建请求一致可避免潜在问题。 - 可直接查看Orion绑定的MongoDB数据库,检查
csubs集合中是否存在对应服务的订阅文档,确认订阅确实已持久化。
内容的提问来源于stack exchange,提问作者Srdjan Zec

