代码示例:使用 Spring Boot、Amazon RDS、AWS SES 和适用于 Java 的 AWS 开发工具包 2.x 创建 Java Web 应用程序

1. 介绍

涉及到的 AWS 服务:

  • Amazon Relational Database Service (Amazon RDS)(项目中使用 MySQL 数据库引擎)
  • Amazon Simple Email Service (the AWS SDK for Java SDK version 2 is used to access Amazon SES)
  • AWS Elastic Beanstalk

这个示例可以从 Web 页面提交数据到 AWS RDS 服务,并可以从 RDS 中获取数据进行展示。
使用 AWS SES 服务将 RDS 中的数据以 Excel 形式通过邮件发送给用户。
使用 AWS Elastic Beanstalk 部署应用。

用户从登陆页面进入应用 (user/password)
Java Web 应用调用 RDS 服务示例 - 图1
进入应用后看到主页
Java Web 应用调用 RDS 服务示例 - 图2
主页中提供的功能:

  • 创建一个 Item
  • 查看所有 Item
  • 修改 Item
  • 通过邮件发送报告给用户

2. 创建项目

在 IntelliJ 中创建一个 Maven 项目。

  • Name:AWSItemTracker
  • GroupId:spring-aws
  • ArtifactId:AWSItemTracker

3. 添加 POM 依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>spring-aws</groupId>
  7. <artifactId>AWSItemTracker</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.4.RELEASE</version>
  14. <relativePath /> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>software.amazon.awssdk</groupId>
  24. <artifactId>bom</artifactId>
  25. <version>2.10.30</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. </dependencies>
  30. </dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.junit.jupiter</groupId>
  34. <artifactId>junit-jupiter-api</artifactId>
  35. <version>5.4.2</version>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.junit.jupiter</groupId>
  40. <artifactId>junit-jupiter-engine</artifactId>
  41. <version>5.4.2</version>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.junit.platform</groupId>
  46. <artifactId>junit-platform-commons</artifactId>
  47. <version>1.4.0</version>
  48. </dependency>
  49. <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
  50. <dependency>
  51. <groupId>org.junit.platform</groupId>
  52. <artifactId>junit-platform-launcher</artifactId>
  53. <version>1.4.0</version>
  54. <scope>test</scope>
  55. </dependency>
  56. <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/ses -->
  57. <dependency>
  58. <groupId>software.amazon.awssdk</groupId>
  59. <artifactId>ses</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.mockito</groupId>
  63. <artifactId>mockito-all</artifactId>
  64. <version>1.10.19</version>
  65. <scope>test</scope>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.assertj</groupId>
  69. <artifactId>assertj-core</artifactId>
  70. <version>3.8.0</version>
  71. <scope>test</scope>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.mockito</groupId>
  75. <artifactId>mockito-core</artifactId>
  76. <version>2.13.0</version>
  77. <scope>test</scope>
  78. </dependency>
  79. <dependency>
  80. <groupId>javax.mail</groupId>
  81. <artifactId>javax.mail-api</artifactId>
  82. <version>1.6.2</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>software.amazon.awssdk</groupId>
  86. <artifactId>protocol-core</artifactId>
  87. </dependency>
  88. <dependency>
  89. <groupId>junit</groupId>
  90. <artifactId>junit</artifactId>
  91. <version>4.5</version>
  92. <scope>test</scope>
  93. </dependency>
  94. <dependency>
  95. <groupId>javax.mail</groupId>
  96. <artifactId>javax.mail-api</artifactId>
  97. <version>1.5.5</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>com.sun.mail</groupId>
  101. <artifactId>javax.mail</artifactId>
  102. <version>1.5.5</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.springframework.boot</groupId>
  106. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  107. </dependency>
  108. <!-- bootstrap and jquery -->
  109. <dependency>
  110. <groupId>org.webjars</groupId>
  111. <artifactId>bootstrap</artifactId>
  112. <version>3.3.7</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>org.webjars</groupId>
  116. <artifactId>jquery</artifactId>
  117. <version>3.2.1</version>
  118. </dependency>
  119. <!-- mysql connector -->
  120. <dependency>
  121. <groupId>mysql</groupId>
  122. <artifactId>mysql-connector-java</artifactId>
  123. <scope>runtime</scope>
  124. </dependency>
  125. <dependency>
  126. <groupId>net.sourceforge.jexcelapi</groupId>
  127. <artifactId>jxl</artifactId>
  128. <version>2.6.10</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>commons-io</groupId>
  132. <artifactId>commons-io</artifactId>
  133. <version>2.6</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>org.springframework.boot</groupId>
  137. <artifactId>spring-boot-starter-web</artifactId>
  138. </dependency>
  139. <dependency>
  140. <groupId>org.springframework.boot</groupId>
  141. <artifactId>spring-boot-starter-security</artifactId>
  142. </dependency>
  143. <dependency>
  144. <groupId>org.springframework.security</groupId>
  145. <artifactId>spring-security-test</artifactId>
  146. <scope>test</scope>
  147. </dependency>
  148. <dependency>
  149. <groupId>org.springframework.boot</groupId>
  150. <artifactId>spring-boot-starter-test</artifactId>
  151. <scope>test</scope>
  152. <exclusions>
  153. <exclusion>
  154. <groupId>org.junit.vintage</groupId>
  155. <artifactId>junit-vintage-engine</artifactId>
  156. </exclusion>
  157. </exclusions>
  158. </dependency>
  159. </dependencies>
  160. <build>
  161. <plugins>
  162. <plugin>
  163. <groupId>org.apache.maven.plugins</groupId>
  164. <artifactId>maven-compiler-plugin</artifactId>
  165. <version>3.1</version>
  166. <configuration>
  167. <source>${java.version}</source>
  168. <target>${java.version}</target>
  169. </configuration>
  170. </plugin>
  171. <plugin>
  172. <groupId>org.apache.maven.plugins</groupId>
  173. <artifactId>maven-checkstyle-plugin</artifactId>
  174. <version>3.1.0</version>
  175. <configuration>
  176. <configLocation>check.xml</configLocation>
  177. <encoding>UTF-8</encoding>
  178. <consoleOutput>true</consoleOutput>
  179. <failsOnError>true</failsOnError>
  180. <linkXRef>false</linkXRef>
  181. </configuration>
  182. <executions>
  183. <execution>
  184. <id>validate</id>
  185. <phase>validate</phase>
  186. <goals>
  187. <goal>check</goal>
  188. </goals>
  189. </execution>
  190. </executions>
  191. </plugin>
  192. </plugins>
  193. </build>
  194. </project>

4. 项目代码

  • 后端代码:
    • SecuringWebApplication:SpringBoot 项目启动类;
    • WebSecurityConfig:Spring 配置类,对项目资源的做了认证授权;并且将项目的有效用户名和密码保存在内存中;
    • MainController:Restful API 接口;
    • WorkItem:实体类;
    • ConnectionHelper:创建一个与 RDS MySQL 实例的链接(配置 URL、User、Password);
    • InjectWorkService:插入数据到 MySQL 实例;
    • RetrieveItems:从 MySQL 实例查询数据;
    • SendMessage:使用 AWS SDK 调用 AWS SES API,发送一封带 Excel 文档附件的邮件;
    • WriteExcel:从 MySQL 获取数据,并使用 Java Excel API 生成一个 Excel 报告文件;
  • 前端代码:
    • login.html:登录页面;
    • index.html:系统主页;
    • add.html:新建一个 Item 页面;
    • items.html:展示和修改 Items;
    • layout.html:以上所有 Views 的页面布局;
    • items.js:items.html 调用;
    • contact_me.js:add.html 调用;

image.png

5. 创建 RDS table

image.png
image.png
image.png
image.png
image.png
image.png
image.png :::info 默认情况下,数据库实例不允许访问。通过与 VPC 关联的安全组授予访问权限,该安全组允许流量进出数据库实例。(还需要检查 VPC 是否附加 Internet 网关,并将有与子网关联的路由将流量导向网关) ::: image.png
image.png
image.png :::info 修改 ConnectionHelper 数据库配置:

url = "jdbc:mysql://awstracker.<url to rds>.amazonaws.com/awstracker";
...
Class.forName("com.mysql.jdbc.Driver").newInstance();
        return DriverManager.getConnection(instance.url, "root","root1234");

:::

image.png

CREATE TABLE awstracker.work(
    idwork VARCHAR(45) PRIMARY KEY,
    date Date,
    description VARCHAR(400),
    guide VARCHAR(45),
    status VARCHAR(400),
    username VARCHAR(45),
    archive BOOLEAN
)  ENGINE=INNODB;

# 从 Workbench 添加一条数据
idwork - 4ea93f34-a45a-481e-bdc6-26c003bb93fc
date - 2020-01-20
description - Need to test all examples
guide - AWS Devloper Guide
status - Tested all of the Amazon S3 examples
username - user
archive - 0

6. 构建 JAR 包

通过 mvn package 命令构建 web 应用的 JAR 包,然后通过 Elastic Beanstalk 上传可执行的 JAR 文件,部署应用。

7. 部署应用到 Elastic Beanstalk

image.png
image.png
image.png
image.png
image.png