spring-boot-deploy-aws-beanstalk
Spring BootアプリケーションをAWS Beanstalkにデプロイする
1. 概要
このチュートリアルでは、https://www.baeldung.com/spring-boot-start [Spring Bootを使用した簡単なアプリケーションのブートストラップ]チュートリアルからhttps://docs.aws.amazonにアプリケーションをデプロイする方法を示します。 .com / elastic-beanstalk / index.html#lang / en_us [AWS Elastic Beanstalk]。
この一環として、次のことを行います。
-
AWS CLIツールをインストールして設定する
-
BeanstalkプロジェクトとMySQLデプロイメントを作成する
-
AWS RDSでMySQLのアプリケーションを構成する
-
アプリケーションの展開、テスト、およびスケーリング
2. AWS Elastic Beanstalk設定
*前提条件として、AWSおよびhttps://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.environments.html[Elastic BeanstalkでJava 8環境を作成]に登録する必要があります。 。 また、https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html [AWS CLIをインストール]を使用して、環境に接続できるようにする必要があります。*
したがって、それを前提として、アプリケーションにログインして初期化する必要があります。
cd .../spring-boot-bootstrap
eb init
>
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
(default is 3):
上記のように、地域を選択するように求められます。
最後に、アプリケーションを選択できます。
>
Select an application to use
1) baeldung-demo
2) [ Create new Application ]
(default is 2):
この時点で、* CLIは* _ *。elasticbeanstalk / config.yml * ._という名前のファイルを作成します。このファイルはプロジェクトのデフォルトを保持します。
3. データベース
これで、AWS WebコンソールまたはCLIを使用してデータベースを作成できます:
eb create --single --database
指示に従ってユーザー名とパスワードを入力する必要があります。
データベースを作成したら、アプリケーションのRDS資格情報を構成しましょう。 アプリケーションで__src / main / resources / application-beanstalk.properties_を作成して、_beanstalk_talkという名前のSpringプロファイルでこれを行います。
spring.datasource.url=jdbc:mysql://${rds.hostname}:${rds.port}/${rds.db.name}
spring.datasource.username=${rds.username}
spring.datasource.password=${rds.password}
-
Springは、_RDS_HOSTNAME_という環境変数として_rds.hostname_という名前のプロパティを検索します。 同じロジックが残りにも適用されます。*
4. 応用
次に、Beanstalk __–__ specific Mavenプロファイルを_pom.xml_に追加します。
<profile>
<id>beanstalk</id>
<build>
<finalName>${project.name}-eb</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/cloud/config/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
*次に、アーティファクトをElastic Beanstalk設定ファイルに指定します_.elasticbeanstalk / config.yml _:*
deploy:
artifact: target/spring-boot-bootstrap-eb.jar
そして最後に、2つの環境変数をElastic Beanstalkに含めます。 最初のものはアクティブなSpringプロファイルを指定し、2番目のものはBeanstalkが期待するデフォルトポート5000の使用を保証します:
eb setenv SPRING_PROFILES_ACTIVE=beanstalk,mysql
eb setenv SERVER_PORT=5000
5. 展開とテスト
*これで、ビルドとデプロイの準備ができました:*
mvn clean package spring-boot:repackage
eb deploy
次に、ステータスを確認し、デプロイされたアプリケーションのDNS名を決定します。
eb status
出力は次のようになります。
Environment details for: BaeldungDemo-env
Application name: baeldung-demo
Region: us-east-2
Deployed Version: app-181216_154233
Environment ID: e-42mypzuc2x
Platform: arn:aws:elasticbeanstalk:us-east-2::platform/Java 8 running on 64bit Amazon Linux/2.7.7
Tier: WebServer-Standard-1.0
CNAME: BaeldungDemo-env.uv3tr7qfy9.us-east-2.elasticbeanstalk.com
Updated: 2018-12-16 13:43:22.294000+00:00
Status: Ready
Health: Green
これでアプリケーションをテストできます。URLを完成させるためにDNSとしてCNAMEフィールドを使用していることに注意してください。
今すぐ図書館に本を追加しましょう。
http POST http://baeldungdemo-env.uv3tr7qfy9.us-east-2.elasticbeanstalk.com/api/books title="The Player of Games" author="Iain M. Banks"
そして、すべてが順調であれば、次のようなものを取得する必要があります。
HTTP/1.1 201
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Wed, 19 Dec 2018 15:36:31 GMT
Expires: 0
Pragma: no-cache
Server: nginx/1.12.1
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
{
"author": "Iain M. Banks",
"id": 5,
"title": "The Player of Games"
}
6. アプリケーションのスケーリング
最後に、展開を拡張して2つのインスタンスを実行します。
eb scale 2
-
Beanstalkは、アプリケーションの2つのインスタンスを実行し、両方のインスタンス間でトラフィックの負荷を分散します。*
生産のための自動スケーリングはもう少しhttps://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.as.html[involved]であるため、別の日に残します。
7. 結論
このチュートリアルでは、次のことを行います。
-
AWS Beanstalk CLIをインストールして設定し、
オンライン環境 -
MySQLサービスをデプロイし、データベース接続を構成しました
プロパティ -
構成済みのSpring Bootアプリケーションを構築および展開し、
-
アプリケーションのテストとスケーリング
詳細については、https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.html [Beanstalk Java documentation]をご覧ください。
いつものように、例の完全なソースコードは、https://github.com/eugenp/tutorials/tree/master/spring-boot-bootstrap [GitHubの上]にあります。