Node-RED - 三、通过接口操作流程
Node RED是一种基于流的编程工具,最初由IBM的新兴技术服务团队开发,现在是OpenJS基金会的一部分。
它提供了一个基于浏览器的可视化编辑器面板,在面板中通过拖拽节点的方式可以实现将将硬件设备、API和在线服务连接在一起,面板中大量节点轻松地将流连接在一起,只需单击一下即可将其部署到运行时。
- 详情进入【 官方网站】进行了解
Node-RED系列文章:
1、Node-RED - 一、添加自定义节点
2、Node-RED - 二、编辑器添加用户认证
3、Node-RED - 三、通过接口操作流程
目录
一、准备
1、本例要实现的目标说明
- 一般情况下,Node-RED不会单独做为一个系统来使用,而是与其它业务系统集成使用,通过Node-RED来完成流程的配置,然后与业务系统集成做为一个完整的系统为用户提供完成的服务。
- 本例验证了Node-RED部分管理接口及触发流程的场景。
2、Node-RED系统搭建
参见【Node-RED中添加自定义节点】
二、Admin接口
序号 | 接口 | 类型 | 说明 |
---|---|---|---|
1 | /auth/login | GET | 获取当前的身份认证方案 |
2 | /auth/token | POST | 根据身份认证信息认证通过后生成Access Token |
3 | /flows | GET | 获取当前的所有有效的流程配置 |
4 | /flow/:id | GET | 获取流程ID=:id的流程配置 |
5 | /flow/:id | PUT | 更新单个流程配置 |
6 | /flow/:id | DELETE | 删除单个流程配置 |
1、获取Token
curl http://192.168.0.100:1880/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=123456'
返回值
{
"access_token": "fQiKsGqSmrei5/57CWxEwNITCE19gJeQAeMv1C2/J5Qm9ouMAZKj1O/v+TtMBxboFow9IrTMwHuHi1GdDKaRw8VYWkTQnVz54iab+wRXhGX0i5wHlUey+TP1kSvaTCtDFbo9btYxhqSd7BWaM30dUXRg0fmJ2qFfna79yezPfls=",
"expires_in": 604800,
"token_type": "Bearer"
}
2、获取流程
1)、请求接口
curl -H "Authorization: Bearer fQiKsGqSmrei5/57CWxEwNITCE19gJeQAeMv1C2/J5Qm9ouMAZKj1O/v+TtMBxboFow9IrTMwHuHi1GdDKaRw8VYWkTQnVz54iab+wRXhGX0i5wHlUey+TP1kSvaTCtDFbo9btYxhqSd7BWaM30dUXRg0fmJ2qFfna79yezPfls=" -H "Node-RED-API-Version:v2" http://192.168.0.100:1880/flows
Headers参数
序号 | 参数 | 说明 |
---|---|---|
1 | Authorization | Bearer [token] |
2 | Node-RED-API-Version | (从0.15.0开始)使用的api版本。如果未设置,则默认为v1。 |
2)、返回值
{
"flows": [{
"id": "f98a48b00e25a9f4",
"type": "tab",
"label": "流程 1",
"disabled": false,
"info": "",
"env": []
}, {
"id": "641b56b84873fd7f",
"type": "tab",
"label": "流程2",
"disabled": false,
"info": "",
"env": []
}, {
"id": "a073f9989268cb2c",
"type": "tab",
"label": "流程3",
"disabled": false,
"info": "",
"env": []
}, {
"id": "d75f54c6e5701e16",
"type": "MySQLdatabase",
"name": "elink_xxx",
"host": "192.168.x.x",
"port": "33xx",
"db": "elink_xxx",
"tz": "",
"charset": "UTF8"
}, {
"id": "d762ebc16a83cff2",
"type": "inject",
"z": "f98a48b00e25a9f4",
"name": "输入",
"props": [{
"p": "payload"
}, {
"p": "topic",
"vt": "str"
}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "Test Beijing Hxyl",
"payloadType": "str",
"x": 310,
"y": 1000,
"wires": [
["a48c29d2767c5f76", "7714482d2deb76e0", "a10e69cfc2a317b8", "d0c1d9ed3444923c"]
]
}, {
"id": "a48c29d2767c5f76",
"type": "debug",
"z": "f98a48b00e25a9f4",
"name": "输出",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 550,
"y": 1000,
"wires": []
}, {
"id": "7714482d2deb76e0",
"type": "function",
"z": "f98a48b00e25a9f4",
"name": "定义加载文件的路径",
"func": "msg.filename = \"D:/work/test/20220612/20220612/core_chatmessage/core_chatmessage_0.json\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 600,
"y": 1120,
"wires": [
["608d4d11b14b8a46"]
]
}, {
"id": "608d4d11b14b8a46",
"type": "file in",
"z": "f98a48b00e25a9f4",
"name": "",
"filename": "filename",
"filenameType": "msg",
"format": "lines",
"chunk": false,
"sendError": false,
"encoding": "GBK",
"allProps": false,
"x": 800,
"y": 1120,
"wires": [
[]
]
}, {
"id": "04094f8aeff4e961",
"type": "mysql",
"z": "f98a48b00e25a9f4",
"mydb": "d75f54c6e5701e16",
"name": "elink_xxx",
"x": 770,
"y": 900,
"wires": [
["fdf31ed668baca47"]
]
}, {
"id": "a10e69cfc2a317b8",
"type": "function",
"z": "f98a48b00e25a9f4",
"name": "SQL定义",
"func": "var modeCount = \"SELECT COUNT(1) FROM loginMode\";\nvar newMsg = {\n \"topic\": modeCount\n}\n\nreturn newMsg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 560,
"y": 900,
"wires": [
["04094f8aeff4e961"]
]
}, {
"id": "fdf31ed668baca47",
"type": "debug",
"z": "f98a48b00e25a9f4",
"name": "debug 2",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1000,
"y": 900,
"wires": []
}, {
"id": "d0c1d9ed3444923c",
"type": "lower-case",
"z": "f98a48b00e25a9f4",
"name": "lower-case",
"x": 590,
"y": 1220,
"wires": [
["0770caf839c92b05"]
]
}, {
"id": "0770caf839c92b05",
"type": "debug",
"z": "f98a48b00e25a9f4",
"name": "自定义节点输出",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 820,
"y": 1220,
"wires": []
}, {
"id": "214d5d67dfdf7e80",
"type": "inject",
"z": "641b56b84873fd7f",
"name": "输入",
"props": [{
"p": "payload"
}, {
"p": "topic",
"vt": "str"
}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "Test Beijing Hxyl",
"payloadType": "str",
"x": 310,
"y": 1000,
"wires": [
["dbc0c8b6cf619343"]
]
}, {
"id": "dbc0c8b6cf619343",
"type": "debug",
"z": "641b56b84873fd7f",
"name": "输出",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 550,
"y": 1000,
"wires": []
}, {
"id": "d5d2fa3182691160",
"type": "inject",
"z": "a073f9989268cb2c",
"name": "输入",
"props": [{
"p": "payload"
}, {
"p": "topic",
"vt": "str"
}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "Test Beijing Hxyl",
"payloadType": "str",
"x": 310,
"y": 1000,
"wires": [
["3d9b6276e7918641", "06d86963b8a88d4e"]
]
}, {
"id": "3d9b6276e7918641",
"type": "function",
"z": "a073f9989268cb2c",
"name": "定义加载文件的路径",
"func": "msg.filename = \"D:/work/test/20220612/20220612/core_chatmessage/core_chatmessage_0.json\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 580,
"y": 940,
"wires": [
["9cc59c66dd179a57"]
]
}, {
"id": "9cc59c66dd179a57",
"type": "file in",
"z": "a073f9989268cb2c",
"name": "",
"filename": "filename",
"filenameType": "msg",
"format": "lines",
"chunk": false,
"sendError": false,
"encoding": "GBK",
"allProps": false,
"x": 840,
"y": 940,
"wires": [
[]
]
}, {
"id": "06d86963b8a88d4e",
"type": "lower-case",
"z": "a073f9989268cb2c",
"name": "lower-case",
"x": 550,
"y": 1040,
"wires": [
["a9ebebe2f5d61c87"]
]
}, {
"id": "a9ebebe2f5d61c87",
"type": "debug",
"z": "a073f9989268cb2c",
"name": "自定义节点输出",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 860,
"y": 1040,
"wires": []
}],
"rev": "f39eb409fb17453c39cfca2d99ba1ac4"
}
3、获取单个流程
1)、请求接口
curl -H "Authorization: Bearer fQiKsGqSmrei5/57CWxEwNITCE19gJeQAeMv1C2/J5Qm9ouMAZKj1O/v+TtMBxboFow9IrTMwHuHi1GdDKaRw8VYWkTQnVz54iab+wRXhGX0i5wHlUey+TP1kSvaTCtDFbo9btYxhqSd7BWaM30dUXRg0fmJ2qFfna79yezPfls=" -H "Node-RED-API-Version:v2" http://192.168.0.100:1880/flow/a073f9989268cb2c
Headers参数
序号 | 参数 | 说明 |
---|---|---|
1 | Authorization | Bearer [token] |
2 | Node-RED-API-Version | (从0.15.0开始)使用的api版本。如果未设置,则默认为v1。 |
2)、返回值
{
"id": "a073f9989268cb2c",
"label": "流程3",
"disabled": false,
"info": "",
"env": [],
"nodes": [{
"id": "d5d2fa3182691160",
"type": "inject",
"z": "a073f9989268cb2c",
"name": "输入",
"props": [{
"p": "payload"
}, {
"p": "topic",
"vt": "str"
}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "Test Beijing Hxyl",
"payloadType": "str",
"x": 310,
"y": 1000,
"wires": [
["3d9b6276e7918641", "06d86963b8a88d4e"]
]
}, {
"id": "3d9b6276e7918641",
"type": "function",
"z": "a073f9989268cb2c",
"name": "定义加载文件的路径",
"func": "msg.filename = \"D:/work/test/20220612/20220612/core_chatmessage/core_chatmessage_0.json\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 580,
"y": 940,
"wires": [
["9cc59c66dd179a57"]
]
}, {
"id": "9cc59c66dd179a57",
"type": "file in",
"z": "a073f9989268cb2c",
"name": "",
"filename": "filename",
"filenameType": "msg",
"format": "lines",
"chunk": false,
"sendError": false,
"encoding": "GBK",
"allProps": false,
"x": 840,
"y": 940,
"wires": [
[]
]
}, {
"id": "06d86963b8a88d4e",
"type": "lower-case",
"z": "a073f9989268cb2c",
"name": "lower-case",
"x": 550,
"y": 1040,
"wires": [
["a9ebebe2f5d61c87"]
]
}, {
"id": "a9ebebe2f5d61c87",
"type": "debug",
"z": "a073f9989268cb2c",
"name": "自定义节点输出",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 860,
"y": 1040,
"wires": []
}]
}
三、使用Node-RED
本例只演示通过Http接口请求触发流程,其它更多业务系统与流程配合的应用场景解决方案请参阅【官方文档-Node-RED Cookbook】
1、通过Http请求触发流程
1)、配置流程
-
第一个节点:Http输入节点,用于定义发布的http接口
-
第二个节点:function节点,不作任何处理,负责接收接口传入参数
-
第三个节点:Debug节点,负责将接口传入参数输出
-
第四个节点:Template节点,负责向接口请求方返回结果
-
第五个节点:Http Response节点,负责向接口请求方返回结果
2)、部署流程
3)、请求接口
http://127.0.0.1:1880/hello-query?name=test&age=19