如何编写将部分传入消息传递给服务总线队列的 Azure HTTP 触发器函数?
Posted
技术标签:
【中文标题】如何编写将部分传入消息传递给服务总线队列的 Azure HTTP 触发器函数?【英文标题】:How to code an Azure HTTP Trigger function that passes parts of its incoming message to a Service Bus Queue? 【发布时间】:2020-12-16 22:09:24 【问题描述】:我正在设置一些 Azure HTTP 触发器函数来接收 JSON 数据。
我想访问将一些数据向前传递到服务总线队列以进行下游处理。
我已经能够将出站绑定添加到服务总线,但无法放置代码以从 HTTp 触发器函数将消息放置到队列中,而不会收到 500 内部服务器错误。这是由于函数的异步性质造成的吗?
如何实现 call-HTTP-Trigger-that-passes-data-onwards-to-a -ServiceB-Bus-Queue 功能?
module.exports = async function (context, req)
context.log('javascript HTTP trigger function processed a request.');
if (req.query.name || (req.body && req.body.name))
context.log('Incoming data from Webhook' + req);
context.bindings.outputSbMsg = "Message placed on Service Bus Queue";
context.log('Message placed on Service Bus Queue');
context.res =
// status: 200, /* Defaults to 200 */
body: "Hello " + (req.query.name || req.body.name)
;
else
context.res =
status: 400,
body: "Please pass a name on the query string or in the request body"
;
;
我确实像你一样尝试过部署,但不幸的是,即使只是运行 GET 查询,我仍然会收到 500 Internal Server Error。
以下是当前文件的样子:
function.json
"bindings": [
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
,
"type": "http",
"direction": "out",
"name": "res"
,
"type": "serviceBus",
"direction": "out",
"connection": "iotcwebhookdispatchersbns_SERVICEBUS",
"name": "outboundSBQMsg",
"queueName": "observations-sbq"
]
index.js
module.exports = async function (context, req)
context.log('JavaScript HTTP trigger function processed a request.');
var message = "This is a test SBQ message - from an Azure function!";
context.bindings.outboundSBQMsg = message;
context.res =
status: 200,
body: "This code is to send message to service bus."
;
;
local.settings.json 文件
"IsEncrypted": false,
"Values":
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"iotcwebhookdispatchersbns_SERVICEBUS": "Endpoint=sb://iotc-webhook-dispatcher-sbns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=MiIv52YQ6plMVt35vPvx9U5DmsQbTvUXamaeZ/fA2Eg="
在配置出站服务总线队列时,我发现可以从现有命名空间中选择服务总线队列命名空间很奇怪。我无法在下一步中选择实际的队列,即使之前已经创建了 observations-sbq。
输入其他队列名称,如 testqueue,也不起作用。它没有被创建,并且出现同样的 500 Internal Server 错误,即使直接在门户中测试该功能也是如此。
错误信息:
这是否意味着 local.settings.json 文件没有被使用/部署?
日志文件摘录:
2020-08-28T07:03:46.194 [信息] JavaScript HTTP 触发器函数处理了一个请求。 2020-08-28T07:03:46.249 [错误] 执行“Functions.iotc-webhook-dispatcher”(失败,Id=e248ae01-7a82-485e-bcfe-00ebee6496e0,持续时间=14ms) Microsoft Azure WebJobs SDK ServiceBus 连接字符串“iotcwebhookdispatchersbns_SERVICEBUS”缺失或为空。 2020-08-28T07:07:08.256 [信息] 执行“Functions.iotc-webhook-dispatcher”(原因=“此函数是通过主机 API 以编程方式调用的。”,ID=222a675f-8b16-4720-b807-bcc9834d7ddc) 2020-08-28T07:07:08.818 [信息] JavaScript HTTP 触发器函数处理了一个请求。 2020-08-28T07:07:10.107 [错误] 执行“Functions.iotc-webhook-dispatcher”(失败,Id=222a675f-8b16-4720-b807-bcc9834d7ddc,持续时间=1067ms) Microsoft Azure WebJobs SDK ServiceBus 连接字符串“iotcwebhookdispatchersbns_SERVICEBUS”丢失或为空。
【问题讨论】:
你的代码好像没问题,能不能把你的function.json文件显示一下? 您是在本地运行代码还是在 azure 上运行代码? 【参考方案1】:下面的代码在我这边可以正常工作:
function.json
"bindings": [
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
,
"type": "http",
"direction": "out",
"name": "res"
,
"name": "outputSbQueue",
"type": "serviceBus",
"queueName": "testqueue",
"connection": "str",
"direction": "out"
]
index.js
module.exports = async function (context, req)
context.log('JavaScript HTTP trigger function processed a request.');
var message = "This is a test message.";
context.bindings.outputSbQueue = message;
context.res =
status: 200,
body: "This code is to send message to service bus."
;
;
local.settings.json
"IsEncrypted": false,
"Values":
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"str":"Endpoint=sb://testbowman.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx="
然后你会发现一条消息进入了服务总线队列:
【讨论】:
@TobiasPersson 所以你在 azure 中运行? @TobiasPersson 它应该有一些错误输出,你能显示出来吗? 我在 Visual Studio Code 中编写代码并将其上传到现有的 Function App/Function。 我将文件的内容添加到上面的原始帖子中。我在其中看不到任何奇怪的东西,现在您的代码和我的代码之间存在差异。 “选择现有的服务总线队列”问题呢? @TobiasPersson 好的,所以请去检查细节错误。首先,转到https://yourfunctionappname.scm.azurewebsites.net/DebugConsole
,然后点击LogFiles\Application\Functions\Function\yourtriggername>
,你会在那里找到日志文件。以上是关于如何编写将部分传入消息传递给服务总线队列的 Azure HTTP 触发器函数?的主要内容,如果未能解决你的问题,请参考以下文章