让我们学习如何将 Java Spring Boot Web 服务器部署到 Amazon EC2。每月只需 3 美元。
使用 Azure,您可能不知道要花费多少钱。
Spring Boot 项目示例
在本教程中,我们将重点介绍如何将 Java Spring Boot 服务器部署到 Amazon EC2,因此我们不会学习如何从头开始构建 Spring Boot Web 服务。
启动 Amazon Linux EC2 实例
首先,编写下面的main.tf文件
provider "aws" {region = "ap-southeast-2"
}resource "aws_instance" "example_server" {ami = "ami-0f6a1a6507c55c9a8"instance_type = "t2.micro"key_name = "rockaws"tags = {Name = "RockSpringBootExample"}
}
运行terraform apply 来创建EC2。
ninjamac@ip-192-168-1-2 target % terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:# aws_instance.example_server will be created
Plan: 1 to add, 0 to change, 0 to destroy.Do you want to perform these actions?Terraform will perform the actions described above.Only 'yes' will be accepted to approve.Enter a value: yesaws_instance.example_server: Creating...
aws_instance.example_server: Still creating... [10s elapsed]
aws_instance.example_server: Creation complete after 16s [id=i-0d3b6e22f089bebf2]
通过 SSH 连接到 EC2 实例
ninjamac@ip-192-168-1-2 key % ssh -i rockaws.pem ec2-user@13.211.xx.xxThe authenticity of host '13.211.xx.xx (13.211.xx.xx)' can't be established.
ED25519 key fingerprint is SHA256:Q45g4PVqKnhtTnw9KQnWXnF5LoeOAFQ7qH2khVmw+Hg.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '13.211.xx.xx' (ED25519) to the list of known hosts., #_~\_ ####_ Amazon Linux 2023~~ \_#####\~~ \###|~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023~~ V~' '->~~~ /~~._. _/_/ _/_/m/'
在 Amazon Linux 上安装 Java 17
[ec2-user@ip-172-31-14-36 ~]$ sudo yum install java-17-amazon-corretto-headless -y
Amazon Linux 2023 Kernel Livepatch repository 133 kB/s | 15 kB 00:00
Dependencies resolved.
================================================================================Package Arch Version Repository Size
================================================================================
Installing:java-17-amazon-corretto-headlessx86_64 1:17.0.14+7-1.amzn2023.1 amazonlinux 91 M
Installing dependencies:alsa-lib x86_64 1.2.7.2-1.amzn2023.0.2 amazonlinux 504 kcairo x86_64 1.18.0-4.amzn2023.0.1 amazonlinux 718 kdejavu-sans-fonts noarch 2.37-16.amzn2023.0.2 amazonlinux 1.3 Mdejavu-sans-mono-fonts noarch 2.37-16.amzn2023.0.2 amazonlinux 467 kdejavu-serif-fonts noarch 2.37-16.amzn2023.0.2 amazonlinux 1.0 Mfontconfig x86_64 2.13.94-2.amzn2023.0.2 amazonlinux 273 kfonts-filesystem noarch 1:2.0.5-12.amzn2023.0.2 amazonlinux 9.5 kfreetype x86_64 2.13.2-5.amzn2023.0.1 amazonlinux 423 kgoogle-noto-fonts-common noarch 20201206-2.amzn2023.0.2 amazonlinux 15 kgoogle-noto-sans-vf-fonts noarch 20201206-2.amzn2023.0.2 amazonlinux 492 kgraphite2 x86_64 1.3.14-7.amzn2023.0.2 amazonlinux 97 kharfbuzz x86_64 7.0.0-2.amzn2023.0.2 amazonlinux 873 kjavapackages-filesystem noarch 6.0.0-7.amzn2023.0.6 amazonlinux 12 klangpacks-core-font-en noarch 3.0-21.amzn2023.0.4 amazonlinux 10 klibX11 x86_64 1.8.10-2.amzn2023.0.1 amazonlinux 659 klibX11-common noarch 1.8.10-2.amzn2023.0.1 amazonlinux 147 klibXau x86_64 1.0.11-6.amzn2023.0.1 amazonlinux 33 klibXext x86_64 1.3.6-1.amzn2023.0.1 amazonlinux 42 klibXrender x86_64 0.9.11-6.amzn2023.0.1 amazonlinux 29 klibbrotli x86_64 1.0.9-4.amzn2023.0.2 amazonlinux 315 klibjpeg-turbo x86_64 2.1.4-2.amzn2023.0.5 amazonlinux 190 klibpng x86_64 2:1.6.37-10.amzn2023.0.6 amazonlinux 128 klibxcb x86_64 1.17.0-1.amzn2023.0.1 amazonlinux 235 kpixman x86_64 0.43.4-1.amzn2023.0.4 amazonlinux 296 kxml-common noarch 0.6.3-56.amzn2023.0.2 amazonlinux 32 kTransaction Summary
================================================================================
Install 26 PackagesTotal download size: 99 M
Installed size: 260 M
Downloading Packages:
(1/26): alsa-lib-1.2.7.2-1.amzn2023.0.2.x86_64. 6.8 MB/s | 504 kB 00:00
(2/26): cairo-1.18.0-4.amzn2023.0.1.x86_64.rpm 8.4 MB/s | 718 kB 00:00
(3/26): dejavu-sans-fonts-2.37-16.amzn2023.0.2. 13 MB/s | 1.3 MB 00:00
(4/26): dejavu-sans-mono-fonts-2.37-16.amzn2023 11 MB/s | 467 kB 00:00
(5/26): dejavu-serif-fonts-2.37-16.amzn2023.0.2 26 MB/s | 1.0 MB 00:00
(6/26): fontconfig-2.13.94-2.amzn2023.0.2.x86_6 5.4 MB/s | 273 kB 00:00
(7/26): fonts-filesystem-2.0.5-12.amzn2023.0.2. 245 kB/s | 9.5 kB 00:00
(8/26): freetype-2.13.2-5.amzn2023.0.1.x86_64.r 11 MB/s | 423 kB 00:00
(9/26): google-noto-sans-vf-fonts-20201206-2.am 20 MB/s | 492 kB 00:00
(10/26): google-noto-fonts-common-20201206-2.am 542 kB/s | 15 kB 00:00
(11/26): graphite2-1.3.14-7.amzn2023.0.2.x86_64 2.1 MB/s | 97 kB 00:00
(12/26): harfbuzz-7.0.0-2.amzn2023.0.2.x86_64.r 14 MB/s | 873 kB 00:00
(13/26): javapackages-filesystem-6.0.0-7.amzn20 336 kB/s | 12 kB 00:00
(14/26): langpacks-core-font-en-3.0-21.amzn2023 440 kB/s | 10 kB 00:00
(15/26): libX11-1.8.10-2.amzn2023.0.1.x86_64.rp 16 MB/s | 659 kB 00:00
(16/26): libX11-common-1.8.10-2.amzn2023.0.1.no 4.8 MB/s | 147 kB 00:00
(17/26): libXau-1.0.11-6.amzn2023.0.1.x86_64.rp 1.4 MB/s | 33 kB 00:00
(18/26): libXext-1.3.6-1.amzn2023.0.1.x86_64.rp 1.8 MB/s | 42 kB 00:00
(19/26): libXrender-0.9.11-6.amzn2023.0.1.x86_6 1.3 MB/s | 29 kB 00:00
(20/26): libbrotli-1.0.9-4.amzn2023.0.2.x86_64. 10 MB/s | 315 kB 00:00
(21/26): libjpeg-turbo-2.1.4-2.amzn2023.0.5.x86 6.2 MB/s | 190 kB 00:00
(22/26): libpng-1.6.37-10.amzn2023.0.6.x86_64.r 4.6 MB/s | 128 kB 00:00
(23/26): libxcb-1.17.0-1.amzn2023.0.1.x86_64.rp 10 MB/s | 235 kB 00:00
(24/26): xml-common-0.6.3-56.amzn2023.0.2.noarc 1.6 MB/s | 32 kB 00:00
(25/26): pixman-0.43.4-1.amzn2023.0.4.x86_64.rp 7.5 MB/s | 296 kB 00:00
(26/26): java-17-amazon-corretto-headless-17.0. 66 MB/s | 91 MB 00:01
--------------------------------------------------------------------------------
Total 62 MB/s | 99 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing : 1/1 Installing : fonts-filesystem-1:2.0.5-12.amzn2023.0.2.noarch 1/26 Installing : dejavu-sans-mono-fonts-2.37-16.amzn2023.0.2.noarch 2/26 Installing : dejavu-sans-fonts-2.37-16.amzn2023.0.2.noarch 3/26 Installing : libpng-2:1.6.37-10.amzn2023.0.6.x86_64 4/26 Installing : dejavu-serif-fonts-2.37-16.amzn2023.0.2.noarch 5/26 Running scriptlet: xml-common-0.6.3-56.amzn2023.0.2.noarch 6/26 Installing : xml-common-0.6.3-56.amzn2023.0.2.noarch 6/26 Installing : pixman-0.43.4-1.amzn2023.0.4.x86_64 7/26 Installing : libjpeg-turbo-2.1.4-2.amzn2023.0.5.x86_64 8/26 Installing : libbrotli-1.0.9-4.amzn2023.0.2.x86_64 9/26 Installing : libXau-1.0.11-6.amzn2023.0.1.x86_64 10/26 Installing : libxcb-1.17.0-1.amzn2023.0.1.x86_64 11/26 Installing : libX11-common-1.8.10-2.amzn2023.0.1.noarch 12/26 Installing : libX11-1.8.10-2.amzn2023.0.1.x86_64 13/26 Installing : libXext-1.3.6-1.amzn2023.0.1.x86_64 14/26 Installing : libXrender-0.9.11-6.amzn2023.0.1.x86_64 15/26 Installing : javapackages-filesystem-6.0.0-7.amzn2023.0.6.noarc 16/26 Installing : graphite2-1.3.14-7.amzn2023.0.2.x86_64 17/26 Installing : google-noto-fonts-common-20201206-2.amzn2023.0.2.n 18/26 Installing : google-noto-sans-vf-fonts-20201206-2.amzn2023.0.2. 19/26 Installing : langpacks-core-font-en-3.0-21.amzn2023.0.4.noarch 20/26 Installing : cairo-1.18.0-4.amzn2023.0.1.x86_64 21/26 Installing : harfbuzz-7.0.0-2.amzn2023.0.2.x86_64 22/26 Installing : freetype-2.13.2-5.amzn2023.0.1.x86_64 23/26 Installing : fontconfig-2.13.94-2.amzn2023.0.2.x86_64 24/26 Running scriptlet: fontconfig-2.13.94-2.amzn2023.0.2.x86_64 24/26 Installing : alsa-lib-1.2.7.2-1.amzn2023.0.2.x86_64 25/26 Installing : java-17-amazon-corretto-headless-1:17.0.14+7-1.amz 26/26 Running scriptlet: java-17-amazon-corretto-headless-1:17.0.14+7-1.amz 26/26 Running scriptlet: fontconfig-2.13.94-2.amzn2023.0.2.x86_64 26/26 Running scriptlet: java-17-amazon-corretto-headless-1:17.0.14+7-1.amz 26/26 Verifying : alsa-lib-1.2.7.2-1.amzn2023.0.2.x86_64 1/26 Verifying : cairo-1.18.0-4.amzn2023.0.1.x86_64 2/26 Verifying : dejavu-sans-fonts-2.37-16.amzn2023.0.2.noarch 3/26 Verifying : dejavu-sans-mono-fonts-2.37-16.amzn2023.0.2.noarch 4/26 Verifying : dejavu-serif-fonts-2.37-16.amzn2023.0.2.noarch 5/26 Verifying : fontconfig-2.13.94-2.amzn2023.0.2.x86_64 6/26 Verifying : fonts-filesystem-1:2.0.5-12.amzn2023.0.2.noarch 7/26 Verifying : freetype-2.13.2-5.amzn2023.0.1.x86_64 8/26 Verifying : google-noto-fonts-common-20201206-2.amzn2023.0.2.n 9/26 Verifying : google-noto-sans-vf-fonts-20201206-2.amzn2023.0.2. 10/26 Verifying : graphite2-1.3.14-7.amzn2023.0.2.x86_64 11/26 Verifying : harfbuzz-7.0.0-2.amzn2023.0.2.x86_64 12/26 Verifying : java-17-amazon-corretto-headless-1:17.0.14+7-1.amz 13/26 Verifying : javapackages-filesystem-6.0.0-7.amzn2023.0.6.noarc 14/26 Verifying : langpacks-core-font-en-3.0-21.amzn2023.0.4.noarch 15/26 Verifying : libX11-1.8.10-2.amzn2023.0.1.x86_64 16/26 Verifying : libX11-common-1.8.10-2.amzn2023.0.1.noarch 17/26 Verifying : libXau-1.0.11-6.amzn2023.0.1.x86_64 18/26 Verifying : libXext-1.3.6-1.amzn2023.0.1.x86_64 19/26 Verifying : libXrender-0.9.11-6.amzn2023.0.1.x86_64 20/26 Verifying : libbrotli-1.0.9-4.amzn2023.0.2.x86_64 21/26 Verifying : libjpeg-turbo-2.1.4-2.amzn2023.0.5.x86_64 22/26 Verifying : libpng-2:1.6.37-10.amzn2023.0.6.x86_64 23/26 Verifying : libxcb-1.17.0-1.amzn2023.0.1.x86_64 24/26 Verifying : pixman-0.43.4-1.amzn2023.0.4.x86_64 25/26 Verifying : xml-common-0.6.3-56.amzn2023.0.2.noarch 26/26 Installed:alsa-lib-1.2.7.2-1.amzn2023.0.2.x86_64 cairo-1.18.0-4.amzn2023.0.1.x86_64 dejavu-sans-fonts-2.37-16.amzn2023.0.2.noarch dejavu-sans-mono-fonts-2.37-16.amzn2023.0.2.noarch dejavu-serif-fonts-2.37-16.amzn2023.0.2.noarch fontconfig-2.13.94-2.amzn2023.0.2.x86_64 fonts-filesystem-1:2.0.5-12.amzn2023.0.2.noarch freetype-2.13.2-5.amzn2023.0.1.x86_64 google-noto-fonts-common-20201206-2.amzn2023.0.2.noarch google-noto-sans-vf-fonts-20201206-2.amzn2023.0.2.noarch graphite2-1.3.14-7.amzn2023.0.2.x86_64 harfbuzz-7.0.0-2.amzn2023.0.2.x86_64 java-17-amazon-corretto-headless-1:17.0.14+7-1.amzn2023.1.x86_64 javapackages-filesystem-6.0.0-7.amzn2023.0.6.noarch langpacks-core-font-en-3.0-21.amzn2023.0.4.noarch libX11-1.8.10-2.amzn2023.0.1.x86_64 libX11-common-1.8.10-2.amzn2023.0.1.noarch libXau-1.0.11-6.amzn2023.0.1.x86_64 libXext-1.3.6-1.amzn2023.0.1.x86_64 libXrender-0.9.11-6.amzn2023.0.1.x86_64 libbrotli-1.0.9-4.amzn2023.0.2.x86_64 libjpeg-turbo-2.1.4-2.amzn2023.0.5.x86_64 libpng-2:1.6.37-10.amzn2023.0.6.x86_64 libxcb-1.17.0-1.amzn2023.0.1.x86_64 pixman-0.43.4-1.amzn2023.0.4.x86_64 xml-common-0.6.3-56.amzn2023.0.2.noarch Complete!
使用 mvn 创建 jar 文件
只需运行 mvn package 命令即可创建 Java 应用程序的 .jar 文件。
您将在目标目录中看到两个 .jar 文件:
ninjamac@ip-192-168-1-2 target % ls -al
total 44624
drwxr-xr-x 16 ninjamac staff 512 4 20 20:20 .
drwxr-xr-x 11 ninjamac staff 352 4 20 19:38 ..
drwxr-xr-x 4 ninjamac staff 128 4 20 19:38 classes
drwxr-xr-x 3 ninjamac staff 96 4 20 19:38 generated-sources
drwxr-xr-x 3 ninjamac staff 96 4 20 19:38 generated-test-sources
-rw-r--r-- 1 ninjamac staff 22820488 4 20 19:40 hello-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 ninjamac staff 3404 4 20 19:39 hello-0.0.1-SNAPSHOT.jar.original
使用 rsync 将 .jar 文件复制到 EC2
ninjamac@ip-192-168-1-2 target % rsync -avz -e "ssh -i ~/Desktop/Rock/key/rockaws.pem" ./hello-0.0.1-SNAPSHOT.jar ec2-user@13.211.xx.xx:~/
building file list ... done
hello-0.0.1-SNAPSHOT.jar
sent 20421458 bytes received 42 bytes 220772.97 bytes/sec
使用 Java 执行 .jar 文件
我们之前安装了 Java,因此可以使用它的命令行界面 (CLI) 运行 .jar 文件,使用 -jar 参数并传入 .jar 文件的路径:
[ec2-user@ip-172-31-14-36 ~]$ java -jar ./hello-0.0.1-SNAPSHOT.jar . ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.3.0)2025-04-20T12:41:34.568Z INFO 26691 --- [hello] [ main] com.learnaws.hello.HelloApplication : Starting HelloApplication v0.0.1-SNAPSHOT using Java 17.0.14 with PID 26691 (/home/ec2-user/hello-0.0.1-SNAPSHOT.jar started by ec2-user in /home/ec2-user)
2025-04-20T12:41:34.583Z INFO 26691 --- [hello] [ main] com.learnaws.hello.HelloApplication : No active profile set, falling back to 1 default profile: "default"
2025-04-20T12:41:37.221Z INFO 26691 --- [hello] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-04-20T12:41:37.244Z INFO 26691 --- [hello] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-04-20T12:41:37.244Z INFO 26691 --- [hello] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.24]
2025-04-20T12:41:37.555Z INFO 26691 --- [hello] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-04-20T12:41:37.556Z INFO 26691 --- [hello] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2851 ms
2025-04-20T12:41:39.058Z INFO 26691 --- [hello] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint beneath base path '/actuator'
2025-04-20T12:41:39.222Z INFO 26691 --- [hello] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-04-20T12:41:39.254Z INFO 26691 --- [hello] [ main] com.learnaws.hello.HelloApplication : Started HelloApplication in 5.58 seconds (process running for 6.56)
在 EC2 安全组中为 Spring Boot 启用 8080 端口
前往连接到您的 EC2 实例的安全组,并添加 8080 端口范围:
使用 IP 测试在 EC2 上运行的 Spring Boot 服务器
使用curl测试 actuator的健康状态
ninjamac@ip-192-168-1-2 target % curl 13.211.239.xx:8080/actuator/health
{"status":"UP"}%
如何为生产环境设置 Spring Boot
- 使用 Caddy 设置自定义域名和 SSL
- 将您的域名指向 EC2 IP
- 安装并设置 Caddy 作为反向代理
- 设置 CDN(例如 CloudFront 或 Cloudflare)用于缓存
保持 Java 服务器在后台运行
我们希望在系统重启和应用程序崩溃时自动启动应用程序,为此可以使用 systemd。
创建 systemd 服务文件
使用 vim 在 /etc/systemd/system/java-server.service 中创建一个服务文件:
sudo vim /etc/systemd/system/java-server.service
[Unit]
Description=Spring Boot Server[Service]
ExecStart=java -jar /home/ec2-user/hello-0.0.1-SNAPSHOT.jar
Restart=on-failure
RestartSec=1s[Install]
WantedBy=multi-user.target
使用 systemctl 启用 java-server 服务
现在您需要重新加载 systemd 并启用 java 服务器:
sudo systemctl daemon-reload
sudo systemctl enable java-server
启动 java-server 服务
sudo systemctl start java-server
使用 systemctl 检查状态
sudo systemctl status java-server
您将看到类似以下内容:
[ec2-user@ip-172-31-14-36 ~]$ sudo systemctl daemon-reload
[ec2-user@ip-172-31-14-36 ~]$ sudo systemctl enable java-server
Created symlink /etc/systemd/system/multi-user.target.wants/java-server.service → /etc/systemd/system/java-server.service.
[ec2-user@ip-172-31-14-36 ~]$ sudo systemctl start java-server
[ec2-user@ip-172-31-14-36 ~]$ sudo systemctl status java-server
● java-server.service - Spring Boot ServerLoaded: loaded (/etc/systemd/system/java-server.service; enabled; preset: >Active: active (running) since Sun 2025-04-20 12:57:18 UTC; 1s agoMain PID: 27376 (java)Tasks: 15 (limit: 1111)Memory: 46.4MCPU: 1.405sCGroup: /system.slice/java-server.service└─27376 java -jar /home/ec2-user/hello-0.0.1-SNAPSHOT.jarApr 20 12:57:18 ip-172-31-14-36.ap-southeast-2.compute.internal systemd[1]: Sto>
Apr 20 12:57:18 ip-172-31-14-36.ap-southeast-2.compute.internal systemd[1]: jav>
Apr 20 12:57:18 ip-172-31-14-36.ap-southeast-2.compute.internal systemd[1]: Sta>
现在,即使您重启系统或退出 SSH 会话,这也能确保您的 Java Web 服务器继续运行。