我目前已经为我的一个功能编写了这段代码:
FUNCTION_URL = "https://functionapp.azurewebsites.net/api/functionname?code={function_key_secret}"
FUNCTION_URL_PAYLOAD = "&name={name}&number={number}"
def trigger_function(name, number):
function_key_secret = get_keyvault_secret(secret_item_name='function-key')
url = FUNCTION_URL.format(function_key_secret=function_key_secret)
payload = FUNCTION_URL_PAYLOAD.format(name=name,number=number)
try:
azure_response = requests.request(method='POST', url=f"{url}{payload}")
except requests.exceptions.RequestException as error:
logging.error(
'Failed to make POST to the Function: %s', error)
我正在使用此方法来触发第二个 Azure 函数,该函数设置为在 HTTP 请求上触发。但是,我认为它不起作用。HTTP 触发的函数未显示任何成功执行计数,而初始函数的成功执行计数正在上升。我还可以在日志中看到初始功能正常工作。
对于场景:Function1 = 更新 cosmosdb 表并触发 Function2 Function2(HTTP 触发器)= 使用来自 Function1 的输入(名称和数字)将数据发送到 Azure 之外的服务。
我可以手动触发function2,在日志中我会看到它有效。然而,当 Function1 执行上述代码时,我看不到相同的日志出现。不要介意 get_keyvault_secret 方法。我也在 Function1 中使用它,而且效果很好。顺便说一句,我的日志输出中没有错误。
函数.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
但是,每当从 Function1 触发 Function2 时,日志都会向我显示这一点。这是 Function1 的日志输出:
2021-06-10T07:58:17Z [Information] ManagedIdentityCredential will use App Service managed identity
2021-06-10T07:58:17Z [Information] Request URL: 'http://localhost:8081/msi/token?api-version=REDACTED&resource=REDACTED'
2021-06-10T07:58:17Z [Information] Request method: 'GET'
2021-06-10T07:58:17Z [Information] Request headers:
2021-06-10T07:58:17Z [Information] 'secret': 'REDACTED'
2021-06-10T07:58:17Z [Information] 'User-Agent': 'azsdk-python-identity/1.6.0 Python/3.7.10 (Linux-5.4.81-microsoft-standard-x86_64-with-debian-10.9)'
2021-06-10T07:58:17Z [Information] No body was attached to the request
2021-06-10T07:58:17Z [Information] Response status: 200
2021-06-10T07:58:17Z [Information] Response headers:
2021-06-10T07:58:17Z [Information] 'Date': 'Thu, 10 Jun 2021 07:58:16 GMT'
2021-06-10T07:58:21Z [Information] Executing 'Functions.EventHubTrigger' (Reason='(null)', Id=9b5e6c36-6227-45b1-9bd0-151f22aac147)
2021-06-10T07:58:21Z [Information] Trigger Details: PartionId: 0, Offset: 51570324760-51570324760, EnqueueTimeUtc: 2021-06-10T07:58:21.3150000Z-2021-06-10T07:58:21.3150000Z, SequenceNumber: 107798-107798, Count: 1
它将发送到 localhost,所以我应该将 URL 更改为发送到 localhost 的 URL 吗?当我收到 200 状态码响应时。Function2 上的日志记录没有显示任何请求已成功处理。它实际上是空的。当我对 Function2 进行手动请求时,我可以看到这样的日志:
2021-06-10T07:49:15Z [Information] Python HTTP trigger function processed a request.
2021-06-10T07:49:15Z [Information] The request body contained name & number
2021-06-10T07:49:17Z [Information] Response status: 200
编辑(2021 年 6 月 14 日):我已经从对 Function2 使用 HTTP 触发函数转移到它被 eventthub 触发。但是在我尝试通过我能找到的任何博客/论坛浏览它 2 天后,问题仍然存在。