【问题标题】:How to code Spring Cloud Function in Azure with multiple endpoints?如何在 Azure 中使用多个端点编写 Spring Cloud Function?
【发布时间】:2019-10-25 04:23:26
【问题描述】:

我正在尝试使用 Spring Cloud 创建 2 个 Azure Functions,但无法使其工作。

@Configuration
public class FirstFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
  @FunctionName("firstFunction")
  public void run(
      @HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
      final ExecutionContext context)
  {
    handleRequest(Optional.empty(), context);
  }

  @Bean
  @Lazy
  Function<Optional<Void>, String> firstFunction()
  {
    return context ->
    {
      // do firstFunction stuff;
    };
  }
}



@Configuration
public class SecondFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
  @FunctionName("secondFunction")
  public void run(
      @HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
      final ExecutionContext context)
  {
    handleRequest(Optional.empty(), context);
  }

  @Bean
  @Lazy
  Function<Optional<Void>, String> secondFunction()
  {
    return context ->
    {
      // do secondFunction stuff;
    };
  }
}



@SpringBootApplication
public class Application
{
  public static void main(final String[] args)
  {
    SpringApplication.run(Application.class, args);
  }
}

使用依赖于spring-cloud-function-dependencies 2.0.1.RELEASE 的上述代码,当同时调用firstFunctionsecondFunction 端点时,它总是命中firstFunction Bean

在谷歌上搜索后,我发现这个SO answer 建议转移到2.1

但是,当我尝试更改为 2.1.1.RELEASE 时,我遇到了无法找到主类的异常:

System.Private.CoreLib: Exception while executing function: Functions.extractContent. System.Private.CoreLib: Result: Failure
Exception: IllegalArgumentException: Failed to locate main class
Stack: java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry
in META-INF/MANIFEST.MF (in that order).

我做错了什么需要一些帮助。

【问题讨论】:

    标签: java spring azure-functions spring-cloud spring-cloud-function


    【解决方案1】:

    我在我身边进行了测试,一切正常。

    您可以通过以下地址获得我的演示:https://github.com/AI-EVO/azuresptingfunction.git。项目基于官方demo:https://github.com/Azure-Samples/hello-spring-function-azure

    我的改变:

    HelloFunction.java

    @SpringBootApplication
    public class HelloFunction {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(HelloFunction.class, args);
        }
    
        @Bean("hello")
        public Function<User, Greeting> hello() {
            return user -> new Greeting("Hello! Welcome, " + user.getName());
        }
    
        @Bean("hi")
        public Function<User, Greeting> hi() {
            return user -> new Greeting("Hi! Welcome, " + user.getName());
        }
    }
    

    修改HelloHandler.java

    public class HelloHandler extends AzureSpringBootRequestHandler<User, Greeting> {
    
        @FunctionName("hello")
        public Greeting execute(
                @HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
                ExecutionContext context) {
    
            context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
            return handleRequest(request.getBody().get(), context);
        }
    }
    

    添加 HiHandler.java

    public class HiHandler extends AzureSpringBootRequestHandler<User, Greeting> {
    
        @FunctionName("hi")
        public Greeting execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
                HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
                ExecutionContext context) {
    
            context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
            return handleRequest(request.getBody().get(), context);
        }
    }
    

    运行函数:

    mvn azure-functions:run

    用邮递员测试

    来自函数你好:

    来自函数hi:

    【讨论】:

    • 谢谢你!我还尝试将 2 个 bean 放在一个单独的类中,并用 Configuration 进行注释。我认为我的原始代码的问题在于我将 bean 定义、自动装配(如 AzureSpringBootRequestHandler 中所暗示的那样)和配置都混合在一个类中。
    • 知道为什么默认测试用例的构建失败了吗?
    • 如果我需要从 Function hello 方法访问 ExecutionContext 怎么办?我尝试自动装配上下文并简单地将其作为参数提供。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-10-20
    相关资源
    最近更新 更多