Azure

Azure Spring Apps でJavaアプリケーションをデプロイしてみる

はじめに

Azure Spring Appsを立ち上げてJavaアプリケーションをデプロイして思います。

Azure Spring AppsはAzure Spring Cloudを呼ばれているものです。名前が変わったようです。Azure Spring AppsはJavaのアプリケーション動作させるイメージがありました。クイックスタートを参考に薦めていきたいと思います。

クイックスタートた微妙に古いのでバージョンの設定に気をつけましょう。

クイックスタート - 初めてのアプリケーションを Azure Spring Apps にデプロイする | Microsoft Learn
クイックスタート - 初めてのアプリケーションを Azure Spring Apps にデプロイする | Microsoft Learn

アプリケーションを Azure Spring Apps にデプロイする方法について説明します。

docs.microsoft.com

Spring Cloudの説明は割愛します。

Spring Cloud
Spring Cloud

Spring Boot の概要から各機能の詳細までが網羅された公式リファレンスドキュメントです。開発者が最初に読むべきドキュメントです。

spring.pleiades.io

前提条件

JDK 8 または JDK 11 をインストールするとあります。

JDK 8はEclipse Temurinバージョン

JDK 11はMicrosoft Build of OpenJDKバージョン

を利用します。

一応、Azureの開発ではMicrosoft Build of OpenJDKがお勧めされています。

そこで下記よりJDK 11(microsoft-jdk-11.0.15-windows-x64.msi)をダウンロードしてインストールします。

Microsoft Build of OpenJDK をダウンロードする | Microsoft Learn
Microsoft Build of OpenJDK をダウンロードする | Microsoft Learn

Microsoft Build of OpenJDK をダウンロードする場所について説明します。

docs.microsoft.com

Set JAVA_HOME variableをインストールしてJAVA_HOMEを設定します。

Azure Spring Appsを起動

Azure Spring Appsを起動します。必要項目を入力します。

ログを取得する場合んは有効にします。必要ない場合はチェックを外します。

Application Insightsも利用しない場合はチェックを外します。

ネットワークの設定も行えます。

あとは立ち上げるだけです。

起動後は下記のように確認できます。

Spring プロジェクト

どうやらSpring プロジェクトというのを作成していきます。

下記のように設定していきます。Spring Bootのバージョンは2.7.1を指定します。Javaのバージョンは11を指定します。

設定後にGENERATEを作成します。hellospring.zipがダウンロードされます。

 

デプロイ

今回はVSCodeを利用してデプロイしていきます。

Build and Deploy Java Spring Boot Apps to Azure Spring Cloud with Visual Studio Code
Build and Deploy Java Spring Boot Apps to Azure Spring Cloud with Visual Studio Code

Java Spring app tutorial showing how to build and deploy a Java Spring Boot microservices to Azure S ...

code.visualstudio.com

Maven

最初に、Maven をインストールします。

ダウンロードします。

Maven – Download Apache Maven
Maven – Download Apache Maven

maven.apache.org

解凍したファイルをC:\に配置します。

PATHを通します。

一時反映

SET PATH=%PATH%;C:\apache-maven-3.8.6\bin

永続化(管理者権限)

SETX /M PATH "%PATH%;C:\apache-maven-3.8.6\bin"
mvn --version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: C:\apache-maven-3.8.6
Java version: 11.0.15, vendor: Microsoft, runtime: C:\Program Files\Microsoft\jdk-11.0.15.10-hotspot
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"

 

VSCode

ダウンロードしたSpring プロジェクトを解凍します。

解凍後にPowershellなどで移動して code . でVSCodeで起動します。

右下に出てくる Extension Pack for Java をインストールします。

インストールの完了を確認できます。

インストール後にビルドが走ります。数分かかります。

src/main/java/com/example/hellospring/HelloController.java を追加します。

package com.example.hellospring;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Azure Spring Apps!";
    }

}

F5で実行してみます。さてエラーが発生します。

PS C:\hellospring>  & 'C:\Program Files\Microsoft\jdk-11.0.15.10-hotspot\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:55725' '@C:\Users\user1\AppData\Local\Temp\cp_bw6ha9hg5d79dds74xqxztcvk.argfile' 'com.example.hellospring.HellospringApplication' 
23:06:29.394 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
        at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82)  
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)       
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
        at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:344)        
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.example.hellospring.HellospringApplication.main(HellospringApplication.java:10)
23:06:29.404 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=configserver: property to your configuration.
        If configuration is not required add spring.config.import=optional:configserver: instead.
        To disable this check, set spring.cloud.config.enabled=false or
        spring.cloud.config.import-check.enabled=false.

解決方法はpom.xmlの編集を行います。解決法としてはレガシーな解決方法にようです。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
No spring.config.import property has been defined - Stack Overflow
No spring.config.import property has been defined - Stack Overflow

While creating Spring Boot cloud config application getting below error. Any help on this? No spring ...

stackoverflow.com

再度、F5を実行します。

/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)

2022-07-18 23:17:25.507  INFO 1712 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2022-07-18 23:17:25.687  INFO 1712 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2022-07-18 23:17:25.687  WARN 1712 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
2022-07-18 23:17:25.692  INFO 1712 --- [           main] c.e.hellospring.HellospringApplication   : No active profile set, falling back to 1 default profile: "default"
2022-07-18 23:17:26.845  INFO 1712 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=9391f086-ec09-32d1-9c35-173b41e8a25e
2022-07-18 23:17:27.241  INFO 1712 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

 

http://localhost:8080/ にアクセスします。

Azure Spring Appsにデプロイ

Azure Spring Appsにデプロイするためには拡張機能をインストールする必要があります。

これをインストールしないとVSCodeからデプロイできません。

インストール後は図のように表示されます。

右クリックしてアプリを作成します。

アプリ名を入力します。

Java 11を設定します。

ターミナルでビルドします。

mvn clean package

あとは出来上がったjarファイルをデプロイしてあげます。

以上でデプロイは完了です。

アクセス

デプロイ後にエンドポイントにアクセスするか聞かれます。

無い場合は作成するか聞かれます。

作成されると自動的にブラウザからアクセスされます。

問題なくアプリケーションがデプロイされ動作していることが確認できました。

まとめ

Azure Spring Appsのデプロイを試して見ました。VSCodeでデプロイするまでには少々時間がかかりました。

正直なところJavaアプリケーションをデプロイするのに便利なのかわかりません。ただ、Spring Cloudを使っている人には良いのかと思います。

なかなか開発者ではないので掴みにくい部分でした。

-Azure
-,