簡體   English   中英

如何在 Azure 中使用多個端點對 Spring Cloud Function 進行編碼?

[英]How to code Spring Cloud Function in Azure with multiple endpoints?

我正在嘗試使用 Spring 雲創建 2 個 Azure 函數,但我無法使其工作。

@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時,我遇到了一個異常,它無法找到主要的 class:

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).

需要一些關於我做錯了什么的幫助。

我在我身邊測試,一切正常。

您可以在以下位置獲得我的演示: 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

在此處輸入圖像描述

用 postman 測試

來自 function 你好:

在此處輸入圖像描述

來自 function 嗨:

在此處輸入圖像描述

這里拋出異常。 Spring Cloud Function 試圖從環境變量然后系統屬性中找到“MAIN_CLASS”設置。 如果設置不存在,它將嘗試從每個依賴項 jar 中的“/META-INF/MANIFEST.MF”文件中搜索屬性。

您可以將 package 時的屬性設置為 jar ,就像這里的示例(gradle)一樣。

如果您不設置該屬性,則可能需要檢查您的依賴關系。 它需要包含 'org.springframework.cloud:spring-cloud-starter-function-web:2.1.1.RELEASE' 或 'org.springframework.cloud:spring-cloud-starter-function-webflux:2.1.1.RELEASE '。 依賴項在“/META-INF/MANIFEST.MF”文件中包含默認的“Main-Class”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM