我正在尝试部署一个用 Rust 实现的 azure 函数应用程序(http 触发器)。我已经使用自定义处理程序创建了一个函数应用程序,并尝试从中读取端口号 环境变量 FUNCTIONS_CUSTOMHANDLER_PORT
let port_key = "FUNCTIONS_CUSTOMHANDLER_PORT";
let port: u16 = match env::var(port_key) {
Ok(val) => val.parse().expect("Custom Handler port is not a number!"),
Err(_) => 7071,
};
但是,函数应用程序无法正常启动,并且日志中充满了如下消息: 等待 HttpWorker 初始化。请求:http://127.0.0.1:42353/ 失败并出现异常消息:连接被拒绝 (127.0.0.1:42353)
我在 azure 门户的 Function 应用程序设置中或 Kudu 的环境变量列表中找不到变量 FUNCTIONS_CUSTOMHANDLER_PORT。有谁知道我可能需要做什么才能使该变量可用于函数应用程序?
确保在
host.json
中正确配置了自定义处理程序,并在函数应用程序中验证配置。
在 Azure Functions 中配置 Azure Application Insights 以分析有关连接被拒绝原因的错误消息。
我创建了一个 Rust Azure 函数并部署到 Azure,称为 MSDOC。
代码片段:
#[tokio::main]
async fn main() {
let example1 = warp::get()
.and(warp::path("api"))
.and(warp::path("HttpTrigger"))
.and(warp::query::<HashMap<String, String>>())
.map(|p: HashMap<String, String>| match p.get("name") {
Some(name) => Response::builder().body(format!("Hello, {}. This HTTP triggered function executed successfully.", name)),
None => Response::builder().body(String::from("This HTTP triggered function executed successfully. Pass a name in the query string for a personalized response.")),
});
let port_key = "FUNCTIONS_CUSTOMHANDLER_PORT";
let port: u16 = match env::var(port_key) {
Ok(val) => val.parse().expect("Custom Handler port is not a number!"),
Err(_) => 3000,
};
warp::serve(example1).run((Ipv4Addr::LOCALHOST, port)).await
}
主机.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
},
"customHandler": {
"description": {
"defaultExecutablePath": "handler",
"workingDirectory": "",
"arguments": []
},
"enableForwardingHttpRequest": true
}
}
在将函数部署到 Linux Azure 函数应用之前,请按照 MSDOC 中提供的步骤编译 Azure 的自定义处理程序。
在
FUNCTIONS_CUSTOMHANDLER_PORT=<PortNumber>
下添加了Function App=>Settings=>Environment Variables=>App Settings
:
使用 Visual Studio 将函数部署到 Azure。
能够在门户中运行该功能:
2024-09-16T12:59:03Z [Information] Executing 'Functions.HttpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=b3ff7d0b-ae4b-4060-af60-2791b59f5fbc)
2024-09-16T12:59:03Z [Verbose] Sending invocation id: 'b3ff7d0b-ae4b-4060-af60-2791b59f5fbc
2024-09-16T12:59:03Z [Verbose] Forwarding httpTrigger invocation for function: 'HttpTrigger' invocationId: 'b3ff7d0b-aeXX9f5fbc'
2024-09-16T12:59:03Z [Verbose] Sending invocation for function: 'HttpTrigger' invocationId: 'b3ff7d0b-ae4b-XX5fbc'
2024-09-16T12:59:03Z [Verbose] Received invocation response for function: 'HttpTrigger' invocationId: 'b3ff7d0b-aXXX1b59f5fbc'
2024-09-16T12:59:03Z [Information] Executed 'Functions.HttpTrigger' (Succeeded, Id=b3ff7d0b-ae4b-4060-af60-2791b59f5fbc, Duration=18ms)