向Orion Context Broker上报NGSIv2数据无实体问题排查
- 需求:将OMA LWM2M协议客户端接入Orion Context Broker
- 故障现象:客户端已成功连接作为NGSI桥接的FIWARE IoT Agent,但调用
localhost:1026/v2/entities接口时,查询不到对应客户端生成的NGSI实体 - 排查依据:提供的Orion docker-compose配置、IoT Agent config.js配置
Orion Context Broker(docker-compose.yml)
version: "3" services: orion: image: fiware/orion ports: - "1026:1026" depends_on: - mongo command: -dbhost mongo mongo: image: mongo:4.4 command: --nojournal
FIWARE IoT Agent for LWM2M(config.js)
/* * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U * * This file is part of fiware-iotagent-lib * * fiware-iotagent-lib is free software: you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * fiware-iotagent-lib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License along with fiware-iotagent-lib. * If not, seehttp://www.gnu.org/licenses/. * * For those usages not covered by the GNU Affero General Public License * please contact with::[contacto@tid.es] */ var config = {}; config.lwm2m = { logLevel: 'DEBUG', port: 5683, defaultType: 'Device', ipProtocol: 'udp4', serverProtocol: 'udp4', delayedObservationTimeout: 50, formats: [ { name: 'application-vnd-oma-lwm2m/text', value: 1541 }, { name: 'application-vnd-oma-lwm2m/tlv', value: 1542 }, { name: 'application-vnd-oma-lwm2m/json', value: 1543 }, { name: 'application-vnd-oma-lwm2m/opaque', value: 1544 } ], writeFormat: 'application-vnd-oma-lwm2m/text', types: [] }; config.ngsi = { logLevel: 'DEBUG', timestamp: true, contextBroker:{ host: 'localhost', port: '1026', ngsiVersion: 'v2' }, server: { port: 59441 }, deviceRegistry: { type: 'mongodb' }, mongodb: { host: 'localhost', port: '27017', db: 'iotagentlm2m' }, types: {}, service: 'smartGondor', subservice: '/gardens', providerUrl: 'http://localhost:4041', deviceRegistrationDuration: 'P1Y', defaultType: 'Thing' }; module.exports = config;
以下配置错误按优先级排序,逐一修复即可解决实体不显示的问题:
网络寻址配置错误
Orion运行在Docker容器中,当前IoT Agent配置里Context Broker地址填的localhost指向IoT Agent自身的运行环境,无法连通容器内的Orion服务,实体同步请求根本发不出去。
修复规则:- 如果IoT Agent直接运行在Docker宿主机上,把
config.ngsi.contextBroker.host改为宿主机物理内网IP,Windows/macOS环境可填host.docker.internal - 如果IoT Agent也运行在同一套docker-compose编排的容器网络中,host直接填Orion的服务名
orion即可 - 同理,MongoDB的host配置也要按上述规则调整,不要填localhost
- 如果IoT Agent直接运行在Docker宿主机上,把
查询请求缺少多租户请求头
配置里IoT Agent指定了服务smartGondor、子服务路径/gardens,FIWARE组件默认通过请求头做租户数据隔离,不带请求头直接调用/v2/entities只会查询默认租户下的实体,无法匹配到IoT Agent写入的租户数据。
正确查询命令参考:curl -H 'Fiware-Service: smartGondor' -H 'Fiware-ServicePath: /gardens' http://localhost:1026/v2/entities配置端口不匹配
config.ngsi.server.port配置的Agent北向服务监听端口是59441,但config.ngsi.providerUrl填的回调地址端口是4041,两个端口不一致,就算实体注册成功,后续Orion的订阅回调也会请求失败,间接导致实体同步异常。需要把两个端口改为一致,确保Orion可以正常访问到Agent的北向服务。设备映射规则缺失
当前配置里config.lwm2m.types为空数组、config.ngsi.types为空对象,没有配置LWM2M资源到NGSI属性的映射规则,也没有提前通过IoT Agent的设备管理接口预注册设备,Agent无法识别客户端上报的数据,不会自动在Orion中创建对应实体。需要补全设备类型映射,或提前调用Agent接口完成设备预配置,明确设备ID、类型、上报属性对应的LWM2M资源路径。验证方式
修复配置后重启所有服务,将IoT Agent日志级别保持为DEBUG,观察设备上线后Agent向Orion发送实体创建请求的日志,确认返回2xx状态码后,再用带租户头的查询接口即可获取到对应实体。
内容的提问来源于stack exchange,提问作者Antonio

