使用function.json在java中配置azure函数

时间:2017-10-12 21:02:41

标签: java azure azure-functions

我正在尝试创建一个用Java编写的Azure函数,并使用function.json文件进行配置(而不是使用注释)。

开始使用MS tutorial(可以正常使用)。

接下来,我尝试修改类以删除Function注释,并在“没有注释的相同函数”部分中添加一个如here所示的function.json。

类源代码现在是:

public class Function {

    public static String hello(String req, ExecutionContext context) {
        return String.format("Hi, %s!", req);
    }

}

我的function.json文件是

{
    "scriptFile": "ServiceBusQueueMsgToLogWriter-1.0-SNAPSHOT.jar",
    "entryPoint": "com.oneadvanced.adv365.mgdsvc.azure.func.test1.Function.hello",
    "bindings": [
    {
        "type": "httpTrigger",
        "name": "req",
        "direction": "in",
        "authLevel": "anonymous",
        "methods": [ "post" ]
    },
    {
        "type": "http",
        "name": "$return",
        "direction": "out"
    }
    ]
}

运行命令的输出:

mvn clean package

包括输出:

AI: INFO 12-10-2017 21:50, 1: Configuration file has been successfully found as resource
AI: INFO 12-10-2017 21:51, 1: Configuration file has been successfully found as resource
[INFO]
[INFO] Step 1 of 6: Searching for Azure Function entry points
[INFO] Reflections took 33 ms to scan 1 urls, producing 0 keys and 0 values
[INFO] 0 Azure Function entry point(s) found.
[INFO]
[INFO] Step 2 of 6: Generating Azure Function configurations
[INFO] No Azure Functions found. Skip configuration generation.

这让我想知道function.json文件是不是在正确的位置......

有没有人知道将function.json文件放在用Java编写的Azure函数的正确位置?

我试过了:

  • 在项目的根目录
  • 在src / main / resources中(我认为这是典型的Java / Maven项目中此类事情的标准位置)
  • 与Java源文件相同的文件夹

每种情况都有相同的结果:(

对于我应该做的事情,我会感激不尽。

谢谢,安迪

1 个答案:

答案 0 :(得分:0)

  

给定功能应用程序中所有功能的代码都存在于   包含主机配置文件和一个或多个的根文件夹   子文件夹,每个子文件夹都包含单独函数的代码。

示例结构

wwwroot
 | - host.json
 | - mynodefunction
 | | - function.json
 | | - index.js
 | | - node_modules
 | | | - ... packages ...
 | | - package.json
 | - mycsharpfunction
 | | - function.json
 | | - run.csx

您可以在official doc找到上述说明。

但是,我注意到您的function.json文件未设置disabled属性。 将disabled属性设置为false以启用提及here的功能。

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "jaygong/test",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "disabled": false
}

更新答案:

我尝试使用您通过配置function.json提供的步骤创建我自己的azure功能,然后我再现了您的问题!

我的功能代码:

package cn.gjj;

public class Function {
    public static String hello(String in) {
        return in;
    }
}

我的功能测试代码:

package cn.gjj;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Unit test for Function class.
 */
public class FunctionTest {
    /**
     * Unit test for hello method.
     */
    @Test
    public void testHello() throws Exception {
        final Function function = new Function();

        final String ret = function.hello("function");

        assertEquals("function", ret);
    }
}

我的function.json文件:

{
  "scriptFile": "JayGongTestAzureFunction-1.0-SNAPSHOT.jar",
  "entryPoint": "cn.gjj.Function.echo",
  "bindings": [
    {
      "type": "httpTrigger",
      "name": "req",
      "direction": "in",
      "authLevel": "anonymous",
      "methods": [ "post" ]
    },
    {
      "type": "http",
      "name": "$return",
      "direction": "out"
    }
  ]
}

当我运行命令mvn clean package时,日志显示与您相同。

AI: INFO 17-10-2017 14:07, 1: Configuration file has been successfully found as resource
AI: INFO 17-10-2017 14:07, 1: Configuration file has been successfully found as resource
[INFO]
[INFO] Step 1 of 6: Searching for Azure Function entry points
[INFO] Reflections took 31 ms to scan 1 urls, producing 0 keys and 0 values
[INFO] 0 Azure Function entry point(s) found.
[INFO]
[INFO] Step 2 of 6: Generating Azure Function configurations
[INFO] No Azure Functions found. Skip configuration generation.
[INFO]
[INFO] Step 3 of 6: Validating generated configurations
[INFO] No configurations found. Skip validation.
[INFO]
[INFO] Step 4 of 6: Saving empty host.json
[INFO] Successfully saved to E:\TestAzureFunction\JayGongTestAzureFunction\target\azure-functions\jaygongtestazurefunction-20171017132623892\host.json
[INFO]
[INFO] Step 5 of 6: Saving configurations to function.json
[INFO] No configurations found. Skip save.
[INFO]
[INFO] Step 6 of 6: Copying JARs to staging directory E:\TestAzureFunction\JayGongTestAzureFunction\target\azure-functions\jaygongtestazurefunction-20171017132623892

在仔细比较function.json配置方式和annotations方式后,我发现Project/target/azure-functions/functionName/文件夹的目录不同。

annotations方式:

enter image description here

function.json配置方式:

enter image description here

请注意,如果在function.json后找不到mvn compile文件,请在pom.xml文件中添加以下关键行。

<resources>
  <resource>
    <directory>${project.basedir}</directory>
       <includes>
          <include>host.json</include>
          <include>local.settings.json</include>
          **<include>function.json</include>**
       </includes>
    </resource>
</resources>

<强> 解决方案:

创建一个职位文件夹并将function.json文件移入其中。然后它适用于我。

enter image description here

请试一试。

希望它对你有所帮助。