代码示例:使用 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)
进入应用后看到主页
主页中提供的功能:
- 创建一个 Item
- 查看所有 Item
- 修改 Item
- 通过邮件发送报告给用户
2. 创建项目
在 IntelliJ 中创建一个 Maven 项目。
- Name:AWSItemTracker
- GroupId:spring-aws
- ArtifactId:AWSItemTracker
3. 添加 POM 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-aws</groupId>
<artifactId>AWSItemTracker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.10.30</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/ses -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>protocol-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- bootstrap and jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<!-- mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<configLocation>check.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</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 调用;
5. 创建 RDS table
:::info
默认情况下,数据库实例不允许访问。通过与 VPC 关联的安全组授予访问权限,该安全组允许流量进出数据库实例。(还需要检查 VPC 是否附加 Internet 网关,并将有与子网关联的路由将流量导向网关)
:::
:::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");
:::
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 文件,部署应用。