我正在使用 MacOS M1,并尝试使用 Java 和 Maven 创建 Azure 函数。
我已执行此处的所有步骤:https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij
也就是说,我已经将 JAVA_HOME 环境配置为指向我的 JDK 11 版本等,并尝试在 IntelliJ 中和使用 mvn azure-functions:run 命令运行 Azure 函数。
在这两种情况下,我都没有收到任何错误,但我能看到的唯一日志如下:
Extension bundle specified, skip install extension
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
这很奇怪,因为它似乎没有运行
func start
命令或类似的东西。它没有检测到我的函数,该函数已正确注释为 @FunctionName("HttpTriggerJava")
以及 Microsoft Azure 文档所述的所有其他详细信息。
另一方面,如果我按照文档使用 C# 创建 Azure 函数,一切都会按预期运行。以下是链接:https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-csharp?tabs=macos%2Cazure-cli
以下是我运行后得到的日志
func start
:
Determining projects to restore...
All projects are up-to-date for restore.
Determining projects to restore...
Restored /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/obj/Debug/net8.0/WorkerExtensions/WorkerExtensions.csproj (in 1.91 sec).
WorkerExtensions -> /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/obj/Debug/net8.0/WorkerExtensions/buildout/Microsoft.Azure.Functions.Worker.Extensions.dll
LocalFunctionProj -> /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/bin/output/LocalFunctionProj.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:13.89
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-10-13T19:55:52.679Z] Found /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/LocalFunctionProj.csproj. Using for user secrets file configuration.
[2024-10-13T19:56:00.382Z] Worker process started and initialized.
Functions:
HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
For detailed output, run func with --verbose flag.
[2024-10-13T19:56:05.581Z] Host lock lease acquired by instance ID
最后,如果我点击端点 http://localhost:7071/api/HttpExample 我会得到预期的响应。
有人遇到过这个问题吗?我被这个问题困扰了,我几乎一整天都在为此苦苦挣扎,但没有找到任何解决方案。我不想使用 C#。
提前致谢
该问题可能是由于函数核心工具处理 Azure 函数的方式造成的。
正如我在评论中提到的,更改函数代码中的
authLevel = AuthorizationLevel.Anonymous
即可解决问题。
代码片段:
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
final String query = request.getQueryParameters().get("name");
final String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
}
}
输出:
[INFO] Azure Functions Core Tools found.
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-10-17T14:40:02.210Z] OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Functions:
HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
For detailed output, run func with --verbose flag.
[2024-10-17T14:40:03.163Z] Worker process started and initialized.
[2024-10-17T14:40:07.153Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-10-17T14:40:14.300Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=3c8dfd9c-92f0-489e-a174-2657eba904d1)
[2024-10-17T14:40:14.449Z] Function "HttpExample" (Id: 3c8dfd9c-92f0-489e-a174-2657eba904d1) invoked by Java Worker
[2024-10-17T14:40:14.449Z] Java HTTP trigger processed a request.
[2024-10-17T14:40:14.585Z] Executed 'Functions.HttpExample' (Succeeded, Id=3c8dfd9c-92f0-489e-a174-2657eba904d1, Duration=316ms)