Как кодировать Spring Cloud Function в Azure с несколькими конечными точками? - PullRequest
0 голосов
/ 25 октября 2019

Я пытаюсь создать 2 функции Azure с помощью Spring Cloud, но не могу заставить его работать.

@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, он всегда достигает значения firstFunction Bean при вызове конечных точек firstFunction и secondFunction.

После некоторого поиска в Google я нашел SO ответ , предлагающий перейти на 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).

Нужна помощь в том, что я делаю неправильно.

1 Ответ

0 голосов
/ 28 октября 2019

Я проверил на моей стороне, и все было в порядке.

Вы можете получить мое демо по адресу: https://github.com/AI-EVO/azuresptingfunction.git. Проект основан на официальной демоверсии: 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

enter image description here

Тест с почтальоном

Из функции привет:

enter image description here

Из функции hi:

enter image description here

...