Deploy a Spring Boot Application to AWS Beanstalk – 将Spring Boot应用程序部署到AWS Beanstalk上

最后修改: 2019年 1月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll show how to deploy an application from our Bootstrap a Simple Application using Spring Boot tutorial to AWS Elastic Beanstalk.

在本教程中,我们将展示如何将使用Spring Boot启动一个简单应用程序教程中的应用程序部署到AWS Elastic Beanstalk

As part of this we’ll:

作为其中的一部分,我们将

  • Install and configure AWS CLI tools
  • Create a Beanstalk project and MySQL deployment
  • Configure the application for MySQL in AWS RDS
  • Deploy, test, and scale the application

2. AWS Elastic Beanstalk Configuration

2.AWS Elastic Beanstalk配置

As a pre-requisite, we should have registered ourselves on AWS and created a Java 8 environment on Elastic Beanstalk. We also need to install the AWS CLI which will allow us to connect to our environment.

作为一个前提条件,我们应该已经在 AWS 上注册,并且在 Elastic Beanstalk 上创建了一个 Java 8 环境。我们还需要安装AWS CLI,这将使我们能够连接到我们的环境。

So, given that, we need to log in and initialize our application:

所以,鉴于此,我们需要登录并初始化我们的应用程序。

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):

As shown above, we are prompted to select a region.

如上图所示,我们被提示要选择一个区域。

Finally, we can select the application:

最后,我们可以选择应用程序。

>
Select an application to use
1) baeldung-demo
2) [ Create new Application ]
(default is 2): 

At this time, the CLI will create a file named .elasticbeanstalk/config.yml. This file will retain the defaults for the project.

此时,CLI将创建一个名为.elasticbeanstalk/config.yml.的文件,该文件将保留项目的默认设置。

3. Database

3.数据库

Now, we can create the database on the AWS Web Console or with the CLI using:

现在,我们可以在AWS Web控制台或使用CLI来创建数据库。

eb create --single --database

We’ll need to follow the instructions to provide a username and password.

我们需要按照指示提供一个用户名和密码。

With our database created, let’s configure now the RDS credentials for our application. We’ll do so in a Spring profile named beanstalk by creating src/main/resources/application-beanstalk.properties in our application:

在创建了数据库之后,让我们现在为我们的应用程序配置RDS凭证。我们将通过在应用程序中创建src/main/resources/application-beanstalk.properties,在一个名为beanstalk的Spring配置文件中进行配置。

spring.datasource.url=jdbc:mysql://${rds.hostname}:${rds.port}/${rds.db.name}
spring.datasource.username=${rds.username}
spring.datasource.password=${rds.password}

Spring will search for the property named rds.hostname as an environmental variable called RDS_HOSTNAME. The same logic will apply to the rest.

Spring将搜索名为rds.hostname的属性,作为一个名为RDS_HOSTNAME的环境变量。同样的逻辑将适用于其余部分。

4. Application

4.应用

Now, we’ll add a Beanstalkspecific Maven profile to pom.xml:

现在,我们要在pom.xml中添加一个Beanstalk专用的Maven配置文件。

<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>

Next, we’ll specify the artifact into the Elastic Beanstalk configuration file .elasticbeanstalk/config.yml:

接下来,我们将在Elastic Beanstalk配置文件.elasticbeanstalk/config.yml中指定工件:

deploy:
  artifact: target/spring-boot-bootstrap-eb.jar

And finally, we’ll include two environmental variables into Elastic Beanstalk. The first one will specify the active Spring profiles, and the second one will ensure the use of the default port 5000 expected by Beanstalk:

最后,我们将把两个环境变量纳入Elastic Beanstalk。第一个变量将指定活跃的Spring配置文件,第二个变量将确保使用Beanstalk所期望的默认端口5000。

eb setenv SPRING_PROFILES_ACTIVE=beanstalk,mysql
eb setenv SERVER_PORT=5000

5. Deployment and Testing

5.部署和测试

Now we are ready to build and deploy:

现在我们已经准备好构建和部署了:

mvn clean package spring-boot:repackage
eb deploy

Next, we’ll check the status and determine the DNS name of the deployed application:

接下来,我们将检查状态并确定部署的应用程序的DNS名称。

eb status

And our output should be something like:

而我们的输出应该是这样的。

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

We can now test the application – notice the use of the CNAME field as DNS to complete the URL.

我们现在可以测试应用程序–注意使用CNAME字段作为DNS来完成URL。

Let’s add a book to our library now:

让我们现在为我们的图书馆添加一本书。

http POST http://baeldungdemo-env.uv3tr7qfy9.us-east-2.elasticbeanstalk.com/api/books title="The Player of Games" author="Iain M. Banks"

And, if all is well, we should get something like:

而且,如果一切顺利,我们应该得到类似的东西。

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. Scaling the Application

6.扩展应用程序

Lastly, we scale the deployment to run two instances:

最后,我们扩展部署,以运行两个实例。

eb scale 2

Beanstalk will now run 2 instances of the application and load balance traffic across both instances.

Beanstalk现在将运行应用程序的2个实例,并在两个实例之间进行负载平衡。

Automatic scaling for production is a bit more involved, so we’ll leave that for another day.

用于生产的自动缩放有点多涉及,所以我们将把这个问题留到以后再讨论。

7. Conclusion

7.结论

In this tutorial, we:

在本教程中,我们。

  • Installed and configured the AWS Beanstalk CLI and configured an online environment
  • Deployed a MySQL service and configured the database connection properties
  • Built and deployed our configured Spring Boot application, and
  • Tested and scaled the application

For more details, check out the Beanstalk Java documentation.

欲了解更多详情,请查看Beanstalk Java文档

As always, the complete source code of our examples is here, over on GitHub.

一如既往,我们的例子的完整源代码在这里,在GitHub上