AzureにSpring Bootアプリをデプロイする
1.はじめに
Microsoft Azureは現在、非常に強固なJavaサポートを特徴としています。
このチュートリアルでは、Spring BootアプリケーションをAzureプラットフォームで動作させる方法を段階的に説明します。
2. Mavenの依存関係と設定
まず、
クラウドサービスを利用するにはAzureサブスクリプションが必要です
。現在、私たちは無料アカウントhttps://azure.microsoft.com/en-us/free/[ここ]にサインアップすることができます。
次に、プラットフォームにログインしてhttps://docs.microsoft.com/en-us/cli/azure/index?view=azure-cli-latest[Azure CLI]を使用してサービスプリンシパルを作成します。
> az login
To sign in, use a web browser to open the page \
https://microsoft.com/devicelogin and enter the code XXXXXXXX to authenticate.
> az ad sp create-for-rbac --name "app-name" --password "password"
{
"appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
"displayName": "app-name",
"name": "http://app-name",
"password": "password",
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
今、私たちはMaven
settings.xml
** で、Azureサービスプリンシパル認証設定を構成します。
<server>
<id>azure-auth</id>
<configuration>
<client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client>
<tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant>
<key>password</key>
<environment>AZURE</environment>
</configuration>
</server>
-
azure-webapp-maven-plugin
を使用して、Spring BootアプリケーションをMicrosoftプラットフォームにアップロードするときは、上記の認証設定に依存します。**
次のMavenプラグインを
pom.xml
に追加しましょう。
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<!-- ... -->
</configuration>
</plugin>
ここ
。
このプラグインには設定可能なプロパティがいくつかあり、それらについては次の紹介で取り上げます。
3. AzureにSpring Bootアプリをデプロイする
環境を設定したので、Spring BootアプリケーションをAzureにデプロイしてみましょう。
“
/hello
”にアクセスすると、“
hello azure!
”と応答します。
@GetMapping("/hello")
public String hello() {
return "hello azure!";
}
このプラットフォームでは、TomcatとJettyの両方に対してJava Web Appのデプロイが可能になりました。
azure-webapp-maven-plugin
を使用すると、デフォルト(ROOT)アプリケーションとしてサポートされているWebコンテナに直接アプリケーションをデプロイすることも、FTP経由でデプロイすることもできます。
アプリケーションをWebコンテナにデプロイするので、WARアーカイブとしてパッケージ化する必要があります。ちなみに、/spring-boot-war-tomcat-deploy[Spring Boot WARをTomcatにデプロイする方法]というリンクを紹介する記事があります。
3.1. Webコンテナの配置
WindowsインスタンスでTomcatにデプロイする場合は、
azure-webapp-maven-plugin
に次の設定を使用します。
<configuration>
<javaVersion>1.8</javaVersion>
<javaWebContainer>tomcat 8.5</javaWebContainer>
<!-- ... -->
</configuration>
Linuxインスタンスの場合は、次の設定を試してください。
<configuration>
<linuxRuntime>tomcat 8.5-jre8</linuxRuntime>
<!-- ... -->
</configuration>
Azure認証を忘れないでください。
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<appName>spring-azure</appName>
<resourceGroup>baeldung</resourceGroup>
<!-- ... -->
</configuration>
アプリケーションをAzureにデプロイすると、アプリケーションサービスとして表示されます。そのため、ここではApp Serviceの名前としてプロパティ
<appName>
を指定しました。また、リソースとしてのApp Serviceはhttps://docs.microsoft.com/ja-jp/azure/azure-resource-manager/resource-group-overview[リソースグループコンテナ]によって保持される必要がある
<resourceGroup>
も必要です。
-
これでazure-webappを使用してトリガーを実行する準備が整いました:Mavenターゲットをデプロイする** と、出力が表示されます。
> mvn clean package azure-webapp:deploy
...[INFO]Start deploying to Web App spring-baeldung...[INFO]Authenticate with ServerId: azure-auth[INFO][Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc]\
Instance discovery was successful[INFO]Target Web App doesn't exist. Creating a new one...[INFO]Creating App Service Plan 'ServicePlanssssssss-bbbb-0000'...[INFO]Successfully created App Service Plan.[INFO]Successfully created Web App.[INFO]Starting to deploy the war file...[INFO]Successfully deployed Web App at \
https://spring-baeldung.azurewebsites.net
...
-
これで、
https://spring-baeldung.azurewebsites.net/hello
** にアクセスして、返信を見ることができます。
展開プロセス中に、Azureは自動的にApp Service Planを作成しました。 Azure App Serviceの詳細については、https://docs.microsoft.com/en-us/azure/app-service/azure-web-sites-web-hosting-plans-in-depth-overview[公式文書]を参照してください。予定。 App Serviceプランをすでに持っている場合は、プロパティ
<appServicePlanName>
を設定して、新しいプランを作成しないようにすることができます。
<configuration>
<!-- ... -->
<appServicePlanName>ServicePlanssssssss-bbbb-0000</appServicePlanName>
</configuration>
3.2. FTPの展開
FTP経由でデプロイするには、次の設定を使います。
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<appName>spring-baeldung</appName>
<resourceGroup>baeldung</resourceGroup>
<javaVersion>1.8</javaVersion>
<deploymentType>ftp</deploymentType>
<resources>
<resource>
<directory>${project.basedir}/target</directory>
<targetPath>webapps</targetPath>
<includes>
<include>** .war</include>
</includes>
</resource>
</resources>
</configuration>
上記の設定では、プラグインにWARファイルを
$ \ {project.basedir}/target
ディレクトリに配置させ、それをTomcatコンテナの
webapps
ディレクトリにデプロイさせます。
最終成果物の名前が
azure-0.1.warだとします。
展開を開始すると、次のような出力が表示されます。
> mvn clean package azure-webapp:deploy
...[INFO]Start deploying to Web App spring-baeldung...[INFO]Authenticate with ServerId: azure-auth[INFO][Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc]\
Instance discovery was successful[INFO]Target Web App doesn't exist. Creating a new one...[INFO]Creating App Service Plan 'ServicePlanxxxxxxxx-xxxx-xxxx'...[INFO]Successfully created App Service Plan.[INFO]Successfully created Web App.
...[INFO]Finished uploading directory: \/xxx/.../target/azure-webapps/spring-baeldung -->/site/wwwroot[INFO]Successfully uploaded files to FTP server: \
xxxx-xxxx-xxx-xxx.ftp.azurewebsites.windows.net[INFO]Successfully deployed Web App at \
https://spring-baeldung.azurewebsites.net
ここでは、アプリケーションをTomcat用のデフォルトのWebアプリケーションとしてデプロイしなかったため、「https://spring-baeldung.azurewebsites.net/azure-0.1/hello」からのみアクセスできます。サーバーは期待どおりに「hello azure!」と応答します。
4.カスタムアプリケーション設定でデプロイする
ほとんどの場合、私たちのSpring Bootアプリケーションはサービスを提供するためにデータアクセスを必要とします。 Azureは現在、SQL Server、MySQL、PostgreSQLなどのデータベースをサポートしています。
簡単にするために、その構成は他のAzureデータベースサービスと非常に似ているため、データソースとしてそのアプリ内MySQLを使用します。
4.1. Azureでアプリ内MySQLを有効にする
アプリ内MySQLを有効にしたWebアプリを作成するためのワンライナーはありませんので、まずCLIを使用してWebアプリを作成する必要があります。
az group create --location japanwest --name bealdung-group
az appservice plan create --name baeldung-plan --resource-group bealdung-group --sku B1
az webapp create --name baeldung-webapp --resource-group baeldung-group \
--plan baeldung-plan --runtime java|1.8|Tomcat|8.5
それから
ポータルの
AppでMySQLを有効にします。
リンク:/uploads/azure1-100×84.png%20100w[]
アプリ内MySQLを有効にすると、デフォルトのデータベース、データソースURL、およびデフォルトのアカウント情報が、ファイルシステムの
/home/data/mysql
ディレクトリの下の
MYSQLCONNSTR
xxx.txt__という名前のファイルで見つかります。
4.2. Azureのアプリ内MySQLを使用したSpring Bootアプリケーション
ここでは、デモのために
User
エンティティと
register
および
list
__Usersに使用される2つのエンドポイントを作成します。
@PostMapping("/user")
public String register(@RequestParam String name) {
userRepository.save(userNamed(name));
return "registered";
}
@GetMapping("/user")
public Iterable<User> userlist() {
return userRepository.findAll();
}
ローカル環境でH2データベースを使用し、それをAzure上のMySQLに切り替えます。一般に、
application.properties
ファイルでデータソースプロパティを設定します。
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
Azureデプロイの間、
__
<appSettings>
で
azure-webapp-maven-plugin__を構成する必要があります。
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<javaVersion>1.8</javaVersion>
<resourceGroup>baeldung-group</resourceGroup>
<appName>baeldung-webapp</appName>
<appServicePlanName>bealdung-plan</appServicePlanName>
<appSettings>
<property>
<name>spring.datasource.url</name>
<value>jdbc:mysql://127.0.0.1:55738/localdb</value>
</property>
<property>
<name>spring.datasource.username</name>
<value>uuuuuu</value>
</property>
<property>
<name>spring.datasource.password</name>
<value>pppppp</value>
</property>
</appSettings>
</configuration>
これでデプロイを開始できます。
> mvn clean package azure-webapp:deploy
...[INFO]Start deploying to Web App custom-webapp...[INFO]Authenticate with ServerId: azure-auth[INFO][Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc]\
Instance discovery was successful[INFO]Updating target Web App...[INFO]Successfully updated Web App.[INFO]Starting to deploy the war file...[INFO]Successfully deployed Web App at \
https://baeldung-webapp.azurewebsites.net
ログから、デプロイメントが終了したことがわかります。
新しいエンドポイントをテストしましょう。
> curl -d "" -X POST https://baeldung-webapp.azurewebsites.net/user\?name\=baeldung
registered
> curl https://baeldung-webapp.azurewebsites.net/user[{"id":1,"name":"baeldung"}]----
サーバーの応答はそれをすべて言います。できます!
[[container-deploy]]
=== 5. Azureにコンテナ化されたSpring Bootアプリをデプロイする
前のセクションでは、サーブレットコンテナ(この場合はTomcat)にアプリケーションをデプロイする方法を示しました。スタンドアロンの実行可能jarとしてデプロイするのはどうですか?
今のところ、Spring Bootアプリケーションをコンテナ化する必要があるかもしれません。
具体的には、それをドッキングしてAzureにアップロードすることができます。
link:/dockerizing-spring-boot-application[Spring Boot Appをドッキングする方法]についての記事はすでに持っていますが、ここでは別のMavenプラグインを利用しようとしています
ドッカー化を自動化するための__docker-maven-plugin__:**
[source,xml,gutter:,true]
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<!– … -→
</configuration>
</plugin>
最新版はhttps://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22com.spotify%22%20AND%20a%3A%22docker-maven-plugin%22[hereこちらにあります。]。 ==== 5.1. Azure Container Registry まず、** dockerイメージをアップロードするにはAzureのContainer Registryが必要です。** それでは、作成しましょう。 [source,shell,gutter:,true]
az acr create –admin-enabled –resource-group baeldung-group \
–location japanwest –name baeldungadr –sku Basic
Container Registryの認証情報も必要です。これは、次の方法で問い合わせることができます。 [source,shell,gutter:,true]
az acr credential show –name baeldungadr –query passwords[0]{
“additionalProperties”: {},
“name”: “password”,
“value”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
}
次に、Mavenの__settings.xml__に次のサーバー認証設定を追加します。 [source,xml,gutter:,true]
<server>
<id>baeldungadr</id>
<username>baeldungadr</username>
<password>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</password>
</server>
==== 5.2. Mavenプラグインの設定 次のMavenプラグイン設定を__pom.xml__に追加しましょう。 [source,xml,gutter:,true]
<properties>
<!– … -→
<azure.containerRegistry>baeldungadr</azure.containerRegistry>
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<registryUrl>
https://${docker.image.prefix}</registryUrl>
;
<serverId>${azure.containerRegistry}</serverId>
<dockerDirectory>docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!– … -→
</plugins>
</build>
上記の構成では、dockerイメージ名、レジストリURL、およびFTP展開のプロパティと同様のプロパティを指定しました。 プラグインは__Dockerfile__を見つけるために__ <dockerDirectory> __の値を使用することに注意してください。 __Dockerfile__を__docker__ディレクトリに置きます。その内容は次のとおりです。 [source,text,gutter:,true]
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME/tmp
ADD azure-0.1.jar app.jar
RUN sh -c ‘touch/app.jar’
EXPOSE 8080
ENTRYPOINT[“sh”, “-c”, “java -Djava.security.egd=file:/dev/./urandom -jar/app.jar”]—-
5.3. DockerインスタンスでSpring Boot Appを実行する
これでDockerイメージを構築してAzureレジストリにプッシュできます。
> mvn docker:build -DpushImage
...[INFO]Building image baeldungadr.azurecr.io/azure-0.1
...
Successfully built aaaaaaaaaaaa
Successfully tagged baeldungadr.azurecr.io/azure-0.1:latest[INFO]Built baeldungadr.azurecr.io/azure-0.1[INFO]Pushing baeldungadr.azurecr.io/azure-0.1
The push refers to repository[baeldungadr.azurecr.io/azure-0.1]...
latest: digest: sha256:0f0f... size: 1375
アップロードが完了したら、
baeldungadr
レジストリを確認しましょう。リポジトリリストに画像が表示されます。
リンク:/uploads/azure2-100×73.png%20100w[]
これで、イメージのインスタンスを実行する準備が整いました。
リンク:/uploads/azure3-100×109.png%20100w[]
インスタンスが起動したら、アプリケーションの提供するサービスにそのパブリックIPアドレスを介してアクセスできます。
> curl http://a.x.y.z:8080/hello
hello azure!
5.4. Dockerコンテナの展開
Azure、Docker Hub、私たちの個人レジストリのいずれからでも、コンテナレジストリがあるとします。
次の
azure-webapp-maven-plugin
の設定を利用して、Spring Boot Webアプリをコンテナにデプロイすることもできます。
<configuration>
<containerSettings>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<registryUrl>https://${docker.image.prefix}</registryUrl>
<serverId>${azure.containerRegistry}</serverId>
</containerSettings>
<!-- ... -->
<configuration>
mvn azure-webapp:deploy
を実行すると、プラグインはWebアプリケーションアーカイブを指定されたイメージのインスタンスにデプロイするのに役立ちます。
その後、インスタンスのIPアドレスまたはAzure App ServiceのURLを介してWebサービスにアクセスできます。
6.まとめ
この記事では、Spring BootアプリケーションをAzureにデプロイ可能なWARまたはコンテナ内の実行可能なJARとしてデプロイする方法を紹介しました。
azure-webapp-maven-plugin
のほとんどの機能については説明しましたが、まだ検討していない機能がいくつかあります。詳細についてはhttps://github.com/Microsoft/azure-maven-plugins/tree/master/azure-webapp-maven-plugin[ここ]をチェックしてください。
いつものように、コードサンプルの完全な実装はhttps://github.com/eugenp/tutorials/tree/master/azure[on Github]にあります。