Spring Cloud Function 4.0が出たので再度試してみた

はじめに

SpringのBlogを見ていたらSpring Cloud Function for Azure Functionsの記事が掲載されていました。

https://spring.io/blog/2023/02/24/spring-cloud-function-for-azure-function

Spring Cloud Function for Azure Functionsは以前試したのですが、4.0になってから若干実装の方法が変わったということで試してみました。

ローカル開発環境の準備

ローカル開発環境としては、Azure Functions Core Toolsのインストールが必要です。

learn.microsoft.com

注意点として、Azure Functions Core Toolsのバージョン4.0.5030以降が必要となります。古いバージョンのままだと、Functionsが起動した時に以下のエラーメッセージが出てうまく動きませんでした。エラーメッセージを見る限り、@Autowiredで指定した変数がうまくSpringによりDIされないといった感じでした。

[2023-03-04T12:21:12.339Z] Exception: NullPointerException: Cannot invoke "java.util.function.Function.apply(Object)" because "this.uppercase" is null
[2023-03-04T12:21:12.339Z] Stack: java.lang.reflect.InvocationTargetException
[2023-03-04T12:21:12.339Z]      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2023-03-04T12:21:12.339Z]      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
[2023-03-04T12:21:12.339Z]      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
(以下省略)

実装

Spring Cloud Function 4.0からは、FunctionInvokerを実装する必要がなく、@ComponentをつければOK。全体的な実装はSpringのBlogを参照してください。

https://spring.io/blog/2023/02/24/spring-cloud-function-for-azure-function

また、以下のエラーメッセージがでる場合があります。

[2023-03-04T12:39:53.227Z] Exception: IllegalArgumentException: Failed to locate main class
[2023-03-04T12:39:53.227Z] Stack: java.lang.IllegalStateException: Failed to initialize
[2023-03-04T12:39:53.227Z]      at org.springframework.cloud.function.adapter.azure.AzureFunctionInstanceInjector.getInstance(AzureFunctionInstanceInjector.java:78)
[2023-03-04T12:39:53.227Z]      at com.microsoft.azure.functions.worker.binding.ExecutionContextDataSource.getFunctionInstance(ExecutionContextDataSource.java:103)
[2023-03-04T12:39:53.227Z]      at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:20)

この場合は、build.gradleにてMain-Classの設定をしてください。

jar {
  manifest {
    attributes 'Main-Class' : 'com.github.miyohide.HttpTriggerDemoApplication'
  }
}

無事設定ができ、./gradlew azureFunctionsRunを実行し、トリガーを起動すると見慣れたSpringのロゴが表示され処理が動きます。

[2023-03-04T12:41:35.973Z]   .   ____          _            __ _ _
[2023-03-04T12:41:35.973Z]  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
[2023-03-04T12:41:35.973Z] ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
[2023-03-04T12:41:35.973Z]  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
[2023-03-04T12:41:35.973Z]   '  |____| .__|_| |_|_| |_\__, | / / / /
[2023-03-04T12:41:35.973Z]  =========|_|==============|___/=/_/_/_/
[2023-03-04T12:41:35.973Z]  :: Spring Boot ::                (v3.0.2)
[2023-03-04T12:41:35.980Z] ======> SOURCE: class com.github.miyohide.HttpTriggerDemoApplication
[2023-03-04T12:41:35.994Z] 3月 04, 2023 9:41:35 午後 org.springframework.boot.StartupInfoLogger logStarting
[2023-03-04T12:41:35.994Z] 情報: Starting application using Java 17.0.5 with PID 20093 (started by miyohide in /Users/miyohide/work/azure_func_java/build/azure-functions/myAzureFunc-1677673588191)
[2023-03-04T12:41:35.994Z] 3月 04, 2023 9:41:35 午後 org.springframework.boot.SpringApplication logStartupProfileInfo
[2023-03-04T12:41:35.994Z] 情報: No active profile set, falling back to 1 default profile: "default"
[2023-03-04T12:41:36.177Z] 3月 04, 2023 9:41:36 午後 org.springframework.boot.StartupInfoLogger logStarted
[2023-03-04T12:41:36.177Z] 情報: Started application in 0.266 seconds (process running for 6.74)
[2023-03-04T12:41:36.183Z] Function "example" (Id: f46e21ff-943c-46ec-a204-214703cdabb7) invoked by Java Worker
[2023-03-04T12:41:36.195Z] Executed 'Functions.example' (Succeeded, Id=f46e21ff-943c-46ec-a204-214703cdabb7, Duration=367ms)
<==========---> 83% EXECUTING [15s]