关于 aws-rds-spring-boot-integration
面向开发和运维数据库应用的技能,介绍如何将 AWS RDS 与 Spring Boot 集成,涵盖连接、安全、迁移及连通性检查。
主要功能
- 01Aurora、MySQL 和 PostgreSQL 配置
- 02HikariCP 连接池设置
- 03SSL/TLS 与 IAM 数据库认证
- 04Flyway 数据库迁移
- 05环境配置与连接检查
适用场景
- 01配置 Spring Boot 应用连接 RDS
- 02为 Aurora 设置读写分离
- 03排查 RDS 连接问题
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ giuseppe-trisciuoglio/ developer-kit --skill aws-rds-spring-boot-integration安装后在 Claude Code 中输入/aws-rds-spring-boot-integration确认已加载。
附带文件references/advanced-configuration.mdreferences/troubleshooting.md
详细介绍
AWS RDS Spring Boot 集成
概述
使用 Spring Boot 应用配置 AWS RDS 数据库(Aurora、MySQL、PostgreSQL)。提供数据源配置、HikariCP 连接池、SSL 连接、特定环境配置以及 AWS Secrets Manager 集成的模式。
适用场景
配置适用于 RDS 工作负载的 HikariCP 连接池、使用 Aurora 副本实现读写分离、设置 IAM 数据库身份验证、启用 SSL/TLS 连接、使用 Flyway 管理数据库迁移,或排查 RDS 连接问题时使用。
说明
按照以下步骤使用 Spring Boot 配置 AWS RDS:
-
添加依赖项 — 引入 Spring Data JPA、数据库驱动程序(MySQL/PostgreSQL)和 Flyway
-
配置数据源 — 在 application.yml 中设置连接属性
-
配置 HikariCP — 针对 RDS 工作负载优化连接池设置
-
设置 SSL — 启用与 RDS 的加密连接
-
配置配置文件 — 设置特定环境的配置(dev/prod)
-
添加迁移 — 创建 Flyway 脚本以管理架构
-
验证连接 — 运行健康检查,确认数据库连接正常
如果验证失败:检查安全组规则,确认凭据正确,确保可从你的网络访问 RDS,并确认 SSL 证书配置。
-
运行迁移 — 仅在连接验证通过后应用 Flyway 迁移
快速开始
步骤 1:添加依赖项
Maven(pom.xml):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
Gradle(build.gradle):
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
// Aurora MySQL
runtimeOnly 'com.mysql:mysql-connector-j:8.2.0'
// Aurora PostgreSQL (alternative)
runtimeOnly 'org.postgresql:postgresql'
// Flyway
implementation 'org.flywaydb:flyway-core'
}
步骤 2:基本数据源配置
使用下方 示例 部分中的配置。对于 PostgreSQL,请修改:
- 驱动程序:
org.postgresql.Driver - URL:
jdbc:postgresql://...,并添加?ssl=true&sslmode=require - 方言:
org.hibernate.dialect.PostgreSQLDialect
步骤 3:设置环境变量
# Production environment variables
export DB_PASSWORD=YourStrongPassword123!
export SPRING_PROFILES_ACTIVE=prod
# For development
export SPRING_PROFILES_ACTIVE=dev
数据库迁移设置
创建 Flyway 迁移文件:
src/main/resources/db/migration/
├── V1__create_users_table.sql
├── V2__add_phone_column.sql
└── V3__create_orders_table.sql
V1__create_users_table.sql:
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
示例
示例 1:Aurora MySQL 配置
spring:
datasource:
url: jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops
username: admin
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
maximum-pool-size: 20
minimum-idle: 5
connection-timeout: 20000
jpa:
hibernate:
ddl-auto: validate
open-in-view: false
示例 2:使用 SSL 的 Aurora PostgreSQL
spring.datasource.url=jdbc:postgresql://myapp-aurora-pg-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:5432/devops?ssl=true&sslmode=require
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.hikari.maximum-pool-size=30
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
示例 3:读写分离配置
@Configuration
public class DataSourceConfiguration {
@Bean
@Primary
public DataSource dataSource(
@Qualifier("writerDataSource") DataSource writerDataSource,
@Qualifier("readerDataSource") DataSource readerDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("writer", writerDataSource);
targetDataSources.put("reader", readerDataSource);
RoutingDataSource routingDataSource = new RoutingDataSource();
routingDataSource.setTargetDataSources(targetDataSources);
routingDataSource.setDefaultTargetDataSource(writerDataSource);
return routingDataSource;
}
}
限制和警告
- HikariCP 连接池大小必须符合 RDS 实例的连接数限制
- 安全组必须允许来自应用 IP 范围的流量
- 使用 AWS Secrets Manager,而不要将凭据硬编码
- 启用存储自动扩展,防止存储空间耗尽
最佳实践
- HikariCP:启用连接泄漏检测,并针对故障转移场景配置超时
- 安全性:启用 SSL/TLS;尽可能使用 IAM 数据库身份验证
- 性能:禁用 open-in-view;使用合适的索引和批量操作
- 监控:启用 Spring Boot Actuator 和数据库健康检查
测试
使用以下健康检查端点验证连接:
@RestController
@RequestMapping("/api/health")
public class DatabaseHealthController {
@Autowired
private DataSource dataSource;
@GetMapping("/db-connection")
public ResponseEntity<Map<String, Object>> testDatabaseConnection() {
Map<String, Object> response = new HashMap<>();
try (Connection connection = dataSource.getConnection()) {
response.put("status", "success");
response.put("database", connection.getCatalog());
response.put("connected", true);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.put("status", "failed");
response.put("error", e.getMessage());
response.put("connected", false);
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
}
}
}
curl http://localhost:8080/api/health/db-connection
支持
如需详细的故障排查和高级配置,请参阅: