Skip to content

Commit ef3cca0

Browse files
committed
feat: add Fly.io deployment support with optimized configuration
- Fix Dockerfile Java version mismatch (upgrade from Java 21 to Java 23) - Optimize docker-compose.yml with PORT environment variable - Add fly.toml with production-ready configuration: * 1GB RAM allocation for stable Spring Boot operation * 2 minimum machines for true zero-downtime deployments * Rolling deployment strategy * Health checks on /actuator/health * Auto-scaling and cost optimization settings * Prometheus metrics endpoint - Add comprehensive FLY_DEPLOYMENT.md guide covering: * Step-by-step deployment instructions * Database setup (Fly Postgres, Neon, Supabase options) * Security best practices * Monitoring and troubleshooting * Cost estimation This enables production deployment to Fly.io with enterprise-grade reliability.
1 parent 39af734 commit ef3cca0

File tree

4 files changed

+390
-2
lines changed

4 files changed

+390
-2
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build stage
2-
FROM maven:3.9-eclipse-temurin-21 AS build
2+
FROM maven:3.9-eclipse-temurin-23 AS build
33
WORKDIR /app
44

55
# Copy pom.xml and download dependencies
@@ -11,7 +11,7 @@ COPY src ./src
1111
RUN mvn clean package -DskipTests
1212

1313
# Runtime stage
14-
FROM eclipse-temurin:21-jre-alpine
14+
FROM eclipse-temurin:23-jre-alpine
1515
WORKDIR /app
1616

1717
# Create non-root user

FLY_DEPLOYMENT.md

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
# Fly.io 部署指南
2+
3+
本指南将帮助你将 HaloLight API Java 部署到 Fly.io。
4+
5+
## 前置要求
6+
7+
1. 安装 [Fly CLI](https://fly.io/docs/hands-on/install-flyctl/)
8+
2. 注册 Fly.io 账号并登录:
9+
```bash
10+
fly auth login
11+
```
12+
13+
## 部署步骤
14+
15+
### 1. 验证 Docker 镜像可用性(可选)
16+
17+
在部署前,确认 Java 23 Alpine 镜像可用:
18+
19+
```bash
20+
docker pull eclipse-temurin:23-jre-alpine
21+
```
22+
23+
如果镜像不可用,请修改 `Dockerfile` 第 14 行:
24+
```dockerfile
25+
# 替换为非 Alpine 版本
26+
FROM eclipse-temurin:23-jre
27+
```
28+
29+
### 2. 创建 Fly 应用
30+
31+
```bash
32+
# 在项目根目录执行
33+
fly launch --no-deploy
34+
35+
# 或者如果 fly.toml 已存在
36+
fly apps create halolight-api-java
37+
```
38+
39+
**重要配置选项:**
40+
- Region: 选择离你用户最近的区域(如 `hkg` 香港、`sin` 新加坡)
41+
- Database: 选择 "No" (我们将手动创建 PostgreSQL)
42+
43+
### 3. 创建 PostgreSQL 数据库
44+
45+
```bash
46+
# 创建 Fly Postgres 实例
47+
fly postgres create --name halolight-db --region hkg
48+
49+
# 或者使用 Neon/Supabase 等外部数据库
50+
```
51+
52+
如果使用 Fly Postgres,获取连接信息:
53+
```bash
54+
fly postgres connect -a halolight-db
55+
```
56+
57+
### 4. 设置 Secrets(环境变量)
58+
59+
```bash
60+
# 数据库配置
61+
fly secrets set DATABASE_URL="jdbc:postgresql://halolight-db.internal:5432/halolight"
62+
fly secrets set DATABASE_USERNAME="postgres"
63+
fly secrets set DATABASE_PASSWORD="your-secure-password"
64+
65+
# JWT 配置(生成 256 位随机密钥)
66+
fly secrets set JWT_SECRET="$(openssl rand -base64 32)"
67+
68+
# 可选配置
69+
fly secrets set SPRING_PROFILES_ACTIVE="prod"
70+
fly secrets set JWT_EXPIRATION="86400000"
71+
fly secrets set JWT_REFRESH_EXPIRATION="604800000"
72+
fly secrets set CORS_ALLOWED_ORIGINS="https://yourdomain.com,https://www.yourdomain.com"
73+
```
74+
75+
### 5. 部署应用
76+
77+
```bash
78+
fly deploy
79+
```
80+
81+
首次部署可能需要 5-10 分钟(Maven 构建 + Docker 镜像构建)。
82+
83+
### 6. 验证部署
84+
85+
```bash
86+
# 查看部署状态
87+
fly status
88+
89+
# 查看健康检查
90+
fly checks list
91+
92+
# 访问应用
93+
fly open
94+
95+
# 查看日志
96+
fly logs
97+
```
98+
99+
访问以下端点验证:
100+
- 健康检查:`https://your-app.fly.dev/actuator/health`
101+
- API 文档:`https://your-app.fly.dev/swagger-ui.html`
102+
- Prometheus 指标:`https://your-app.fly.dev/actuator/prometheus`
103+
104+
## 常用命令
105+
106+
```bash
107+
# 查看应用信息
108+
fly info
109+
110+
# 查看实时日志
111+
fly logs -a halolight-api-java
112+
113+
# SSH 进入容器
114+
fly ssh console
115+
116+
# 扩容(调整实例数量)
117+
fly scale count 3
118+
119+
# 调整 VM 大小
120+
fly scale vm shared-cpu-2x --memory 2048
121+
122+
# 查看密钥
123+
fly secrets list
124+
125+
# 回滚到上一个版本
126+
fly releases
127+
fly deploy --image <previous-image>
128+
```
129+
130+
## 配置优化
131+
132+
### 生产环境建议
133+
134+
**fly.toml 优化:**
135+
```toml
136+
[vm]
137+
size = "shared-cpu-2x" # 升级到 2 核
138+
memory_mb = 2048 # 2GB 内存
139+
140+
[machines]
141+
min_machines_running = 2 # 保证高可用
142+
max_machines_running = 10 # 限制最大实例数
143+
```
144+
145+
**JVM 参数优化:**
146+
147+
修改 `Dockerfile` 最后一行:
148+
```dockerfile
149+
ENTRYPOINT ["java", \
150+
"-XX:+UseContainerSupport", \
151+
"-XX:MaxRAMPercentage=75.0", \
152+
"-XX:InitialRAMPercentage=50.0", \
153+
"-Xlog:gc*:stdout:time", \
154+
"-jar", "app.jar"]
155+
```
156+
157+
### 数据库连接池配置
158+
159+
`application-prod.properties` 或环境变量中:
160+
```properties
161+
spring.datasource.hikari.maximum-pool-size=10
162+
spring.datasource.hikari.minimum-idle=5
163+
spring.datasource.hikari.connection-timeout=30000
164+
spring.datasource.hikari.idle-timeout=600000
165+
spring.datasource.hikari.max-lifetime=1800000
166+
```
167+
168+
## 监控和告警
169+
170+
### 查看指标
171+
172+
```bash
173+
# 查看 CPU/内存使用
174+
fly dashboard
175+
176+
# Prometheus 指标
177+
curl https://your-app.fly.dev/actuator/prometheus
178+
```
179+
180+
### 配置告警
181+
182+
在 Fly.io Dashboard 配置:
183+
- 健康检查失败告警
184+
- CPU 使用率超过 80%
185+
- 内存使用率超过 85%
186+
- 请求延迟超过 5s
187+
188+
## 故障排查
189+
190+
### 健康检查失败
191+
192+
```bash
193+
# 查看日志
194+
fly logs
195+
196+
# 检查健康端点
197+
fly ssh console
198+
wget -O- http://localhost:8080/actuator/health
199+
```
200+
201+
### 内存不足(OOMKilled)
202+
203+
```bash
204+
# 增加内存
205+
fly scale memory 2048
206+
207+
# 或调整 JVM 堆大小
208+
# 在 Dockerfile ENTRYPOINT 添加:
209+
# -Xmx1536m -Xms768m
210+
```
211+
212+
### 数据库连接失败
213+
214+
```bash
215+
# 验证数据库连接
216+
fly ssh console
217+
nc -zv halolight-db.internal 5432
218+
219+
# 检查环境变量
220+
fly ssh console -C "env | grep DATABASE"
221+
```
222+
223+
### 构建失败
224+
225+
```bash
226+
# 本地测试构建
227+
docker build -t halolight-api-java .
228+
229+
# 清理 fly 缓存
230+
fly deploy --no-cache
231+
```
232+
233+
## 成本估算
234+
235+
基于 Fly.io 定价(2025 年):
236+
237+
| 配置 | 每月成本(USD) |
238+
|------|----------------|
239+
| shared-cpu-1x (1GB RAM) × 2 实例 | ~$15 |
240+
| shared-cpu-2x (2GB RAM) × 2 实例 | ~$30 |
241+
| Fly Postgres (单节点) | ~$5-15 |
242+
| 流量(前 100GB 免费) | $0.02/GB |
243+
244+
**提示:** 使用 `auto_stop_machines = true` 可在空闲时自动停机,节省成本。
245+
246+
## 安全最佳实践
247+
248+
1. **永远不要提交敏感信息**
249+
- 使用 `fly secrets` 管理密钥
250+
- 不要在 `fly.toml` 中硬编码密码
251+
252+
2. **使用 HTTPS**
253+
- Fly.io 自动提供免费 SSL 证书
254+
- 配置 `force_https = true`
255+
256+
3. **限流保护**
257+
- 项目已集成 Bucket4j 限流
258+
- 根据需要调整速率限制
259+
260+
4. **数据库安全**
261+
- 使用强密码
262+
- 启用 SSL 连接
263+
- 定期备份
264+
265+
5. **监控和日志**
266+
- 启用 Actuator 健康检查
267+
- 集成 Prometheus 监控
268+
- 配置日志聚合(Papertrail/Logflare)
269+
270+
## 外部数据库选项
271+
272+
如果不使用 Fly Postgres,可以使用:
273+
274+
### Neon(推荐)
275+
```bash
276+
# 在 Neon Console 创建数据库
277+
# 获取连接字符串,格式:postgresql://user:pass@host/db
278+
fly secrets set DATABASE_URL="jdbc:postgresql://ep-xxx.neon.tech/halolight?sslmode=require"
279+
```
280+
281+
### Supabase
282+
```bash
283+
# 在 Supabase Dashboard 获取连接信息
284+
fly secrets set DATABASE_URL="jdbc:postgresql://db.xxx.supabase.co:5432/postgres?sslmode=require"
285+
```
286+
287+
### Railway
288+
```bash
289+
# 在 Railway 创建 PostgreSQL 服务
290+
fly secrets set DATABASE_URL="jdbc:postgresql://containers-us-west-xxx.railway.app:6543/railway"
291+
```
292+
293+
## 下一步
294+
295+
- [ ] 配置自定义域名:`fly certs add yourdomain.com`
296+
- [ ] 设置 CI/CD:参考 `.github/workflows/`
297+
- [ ] 配置数据库备份策略
298+
- [ ] 集成日志聚合服务
299+
- [ ] 配置 APM(Application Performance Monitoring)
300+
301+
## 参考资料
302+
303+
- [Fly.io Documentation](https://fly.io/docs/)
304+
- [Spring Boot Deployment Guide](https://spring.io/guides/gs/spring-boot-docker/)
305+
- [Fly.io Postgres](https://fly.io/docs/postgres/)
306+
- [Project README](./README.md)

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525
postgres:
2626
condition: service_healthy
2727
environment:
28+
PORT: 8080
2829
DATABASE_URL: jdbc:postgresql://postgres:5432/halolight
2930
DATABASE_USERNAME: postgres
3031
DATABASE_PASSWORD: postgres

0 commit comments

Comments
 (0)