我创建了 Azure 函数,它接受 http 请求并将 json 文件返回到 blob 中。我想知道如何在我的 azure 函数中实现负载均衡器,以防在我的应用程序中,如果有太多请求,它不应该失败或花费太多时间,因为我正在处理 5-10gb 数据。
示例代码:
app = func.FunctionApp()
@app.function_name(name="HttpTrigger1")
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
def test_function(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
最佳答案
感谢@PeterBons的评论。
正如 @PeterBons 提到的,您可以使用 Azure 函数的内置缩放功能。
- 默认情况下,Azure Functions 会监视应用程序的负载并根据需要自动创建主机实例。
- 此扩展功能对于处理请求负载非常有用。
- 如果您的函数应用使用消耗计划,则将根据收到的请求数量动态添加和删除 Functions 主机的实例。
如果是高级或应用服务计划,您可以根据要求扩展资源。
引用文献:
关于python - 如何在我的 Azure Function App 中实现负载均衡器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76554365/