1. 概述

本教程将展示如何把我们在 使用 Spring Boot 快速搭建一个简单应用 中创建的项目,部署到 AWS Elastic Beanstalk 上。

我们将完成以下几项工作:

  • 安装并配置 AWS CLI 工具
  • 创建 Beanstalk 项目和 MySQL 部署环境
  • 配置应用连接 AWS RDS 中的 MySQL 数据库
  • 部署、测试并扩展该应用

2. AWS Elastic Beanstalk 环境配置

前置条件:你需要拥有一个 AWS 账号,并已在 Elastic Beanstalk 中创建好 Java 8 的运行环境。此外还需安装 AWS CLI,用于与你的环境进行交互。

完成上述准备后,进入项目目录并初始化 Beanstalk 应用:

cd .../spring-boot-bootstrap
eb init

此时会提示选择默认区域(region):

>
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 控制台手动创建数据库,也可以使用 CLI 命令快速创建:

eb create --single --database

随后按提示输入数据库用户名和密码即可。

数据库创建完成后,我们需要为应用配置 RDS 的连接信息。在项目中新建一个 Spring Profile 配置文件 src/main/resources/application-beanstalk.properties

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. 应用打包与配置

pom.xml 中添加一个专用于 Beanstalk 的 Maven Profile:

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

然后,在 .elasticbeanstalk/config.yml 中指定要部署的 artifact:

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

最后设置两个环境变量:

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

✅ 第一个变量用于激活 beanstalkmysql 两个 profile,第二个则是为了让应用监听 Beanstalk 默认的 5000 端口。

5. 部署与测试

万事俱备,开始构建并部署应用:

mvn clean package spring-boot:repackage
eb deploy

部署完成后,查看状态及访问地址:

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

📌 使用 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. 应用扩容

最后,我们可以将应用扩展为两个实例运行:

eb scale 2

✅ Beanstalk 会自动在两个实例间做负载均衡。

当然,生产环境下的自动扩缩容策略比较复杂,详情可参考官方文档:Auto Scaling in Beanstalk

7. 总结

通过本教程,我们完成了以下内容:

  • 安装并配置 AWS Beanstalk CLI,创建在线运行环境
  • 部署 MySQL 服务,并配置数据库连接参数
  • 构建并成功部署 Spring Boot 应用
  • 进行功能测试并实现横向扩容

📌 更多关于 Beanstalk Java 平台的细节,请查阅官方文档:Beanstalk Java Documentation

📚 示例代码已上传至 GitHub:Spring Boot Bootstrap 示例


原始标题:Deploy a Spring Boot Application to AWS Beanstalk