【问题标题】:Azure Java function -502-Bad GatewayAzure Java 函数 -502-Bad Gateway
【发布时间】:2018-11-09 23:05:12
【问题描述】:

我已经按照以下链接创建了 Java azure 函数:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven

Java 类:

package com.khan;

import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/hello
     * 2. curl {your host}/api/hello?name=HTTP%20Query
     */
    @FunctionName("hello")
    public HttpResponseMessage<String> hello(
            @HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
                
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");
        String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponse(400, "Please pass a name on the query string or in the request body");
        } else {
            return request.createResponse(200, "Hello, " + name);
        }
                
    }
}

函数创建和部署成功

现在当我尝试使用 curl 访问时

curl -w '\n' https://sundaydemo-20180526141357482.azurewebsites.net -d AzureFunctions

or postman 

https://sundaydemo-20180526141357482.azurewebsites.net/api/hello

然后出现以下错误,我想知道是否有人遇到同样的错误。

502-坏网关

指定的 CGI 应用程序遇到错误,服务器终止了进程。

日志:

2018-05-31T02:02:50  Welcome, you are now connected to log-streaming service.
2018-05-31T02:03:50  No new trace in the past 1 min(s).
2018-05-31T02:04:50  No new trace in the past 2 min(s).
2018-05-31T02:05:50  No new trace in the past 3 min(s).
2018-05-31T02:06:50  No new trace in the past 4 min(s).
2018-05-31T02:07:50  No new trace in the past 5 min(s).
2018-05-31T02:08:50  No new trace in the past 6 min(s).
2018-05-31T02:09:17.161 [Information] Executing 'Functions.hello' (Reason='This function was programmatically called via the host APIs.', Id=b43d17c9-35c0-4c84-ab7e-26a8ec721fe9)
2018-05-31T02:10:50  No new trace in the past 1 min(s).
2018-05-31T02:11:50  No new trace in the past 2 min(s).
2018-05-31T02:12:50  No new trace in the past 3 min(s).
2018-05-31T02:13:50  No new trace in the past 4 min(s).
2018-05-31T02:14:17.183 [Error] Timeout value of 00:05:00 exceeded by function 'Functions.hello' (Id: 'b43d17c9-35c0-4c84-ab7e-26a8ec721fe9'). Initiating cancellation.
2018-05-31T02:14:17.451 [Error] Microsoft.Azure.WebJobs.Host: Timeout value of 00:05:00 was exceeded by function: Functions.hello.
2018-05-31T02:14:17.572 [Error] Executed 'Functions.hello' (Failed, Id=b43d17c9-35c0-4c84-ab7e-26a8ec721fe9)
2018-05-31T02:15:50  No new trace in the past 1 min(s).
2018-05-31T02:16:50  No new trace in the past 2 min(s).
2018-05-31T02:17:50  No new trace in the past 3 min(s).
2018-05-31T02:18:50  No new trace in the past 4 min(s).

还尝试删除所有并添加 CORS *

存储:

【问题讨论】:

  • 你有什么进展吗?
  • 这个问题应该用新的mvn插件解决了,你可以试试。

标签: java azure azure-functions


【解决方案1】:

看来你遇到了Function Runtime 2.0 Preview Breaking Change

在门户上,函数运行时已更新为 2.0.11857.0(检查您的函数应用设置面板)。虽然 maven 插件还没有更新赶上来。

所以旧的mvn插件构建的代码与最新的运行时不兼容。

解决方法是将您的函数运行时固定到前一个。转到应用程序设置面板并将FUNCTIONS_EXTENSION_VERSIONbeta 更改为2.0.11776-alpha

更多详情请见issue discussion,新插件即将推出。

Update-6/15

新插件已发布。见notice for jave language worker

  • 使用最新的 Maven Azure Functions Archetype 创建新项目

    mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype 
    
  • 更新现有项目以使用最新的 Azure Function mvn 插件

    1. 功能代码(*.java)

      之前

      import com.microsoft.azure.serverless.functions.annotation.*;
      import com.microsoft.azure.serverless.functions.*;
      

      之后(删除无服务器)

      import com.microsoft.azure.functions.annotation.*;
      import com.microsoft.azure.functions.*;
      
    2. pom.xml

      1)之前

      <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>azure-functions-java-core</artifactId>
          <version>1.0.0-beta-2/3</version>
      </dependency>
      

      之后

      <dependency>
          <groupId>com.microsoft.azure.functions</groupId>
          <artifactId>azure-functions-java-library</artifactId>
          <version>1.0.0-beta-4</version>
      </dependency>
      

      2)在excludeArtifactIds中再找到一个azure-functions-java-core,改成azure-functions-java-library

      3)找到azure-functions-maven-plugin,将版本改为1.0.0-beta-2。在maven 上查看最新版本。

【讨论】:

  • 谢谢!切换到 azure-functions-java-library 1.0.0-beta-4 使其工作。但是函数不再在本地工作。我已将 azure-functions-core-tools npm 包从 2.0.1-beta.28 更新到 2.0.1-beta.29,但没有帮助。我已经安装了 .NET Core Runtime (v.2.1.0)。
  • @PavelS。如果don't work 表示未找到工作职能,则为预期。旧的 mvn 插件不会生成 function.jsonazure-functions-java-library。等待新插件。或者你可以复制函数文件夹(里面有function.json)到目标。
  • @PavelS。新插件可用。
  • 感谢使用新版本后,它在本地和 azure 上运行良好
猜你喜欢
  • 2018-03-16
  • 2020-04-05
  • 2020-07-12
  • 2023-03-20
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多