7. 部署基于Spring Cloud的微服务项目
架构升级:将单体Spring Boot应用扩展为Spring Cloud微服务集群,利用AWS托管服务实现服务发现、配置中心和API网关。
微服务架构拓扑
关键组件与AWS服务映射
Spring Cloud组件 | AWS托管服务 | 优势 |
---|---|---|
Eureka服务注册中心 | AWS Cloud Map | 免运维、自动健康检查、与ECS深度集成 |
Config配置中心 | AWS Systems Manager (SSM) | 安全存储加密配置、版本控制、低成本 |
Spring Cloud Gateway | Amazon API Gateway | 百万级并发、WAF集成、精细化流量管理 |
Ribbon负载均衡 | ALB/NLB | 跨可用区负载均衡、动态端口映射 |
Zipkin分布式追踪 | AWS X-Ray | 自动追踪微服务链路、性能瓶颈可视化 |
部署步骤
步骤1:容器化微服务
每个微服务独立Docker镜像:
# 用户服务Dockerfile示例
FROM eclipse-temurin:17
ARG JAR_FILE=target/user-service-*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
步骤2:配置服务注册与发现
方案A:使用AWS Cloud Map(推荐)
- 创建命名空间:
aws servicediscovery create-private-dns-namespace \ --name spring-cloud \ --vpc vpc-0abcdef123456
- 在ECS任务定义中注册服务:
"serviceRegistries": [ { "registryArn": "arn:aws:servicediscovery:us-east-1:123456789:service/srv-xxxx" } ]
方案B:自建Eureka Server
// Eureka服务端配置
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
ECS服务发现配置:
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8761/eureka
步骤3:集中化配置管理
使用AWS SSM Parameter Store
- 存储加密配置:
aws ssm put-parameter \ --name "/spring-cloud/dev/db-password" \ --value "s3cr3tP@ss" \ --type SecureString
- Spring Boot集成:
<!-- pom.xml --> <dependency> <groupId>io.awspring.cloud</groupId> <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId> </dependency>
# bootstrap.yml spring: config: import: "aws-parameterstore:/spring-cloud/dev/"
步骤4:API网关与路由
方案A:Spring Cloud Gateway
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
- id: order-service
uri: lb://order-service
predicates:
- Path=/api/orders/**
方案B:Amazon API Gateway
- 创建REST API并导入OpenAPI定义
- 配置ECS VPC Link实现私有服务访问
- 设置路由映射:
/users -> ALB-user-service /orders -> ALB-order-service
步骤5:服务通信与弹性
OpenFeign + AWS ALB
// 订单服务调用用户服务
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
User getUser(@PathVariable Long id);
}
ALB配置:
- 每个微服务对应独立目标组
- 开启HTTP/2协议提升性能
步骤6:分布式追踪
集成AWS X-Ray
- 添加依赖:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-xray-recorder-sdk-spring</artifactId> </dependency>
- 启用X-Ray:
@SpringBootApplication @EnableXRay public class UserServiceApp { ... }
- 在ECS任务角色添加
AWSXRayDaemonWriteAccess
权限
8. 微服务运维实践
监控看板
- 指标监控:CloudWatch → ECS服务CPU/Memory
- 日志聚合:
logging: group: /ecs/spring-cloud stream: user-service-${ECS_CONTAINER_METADATA_URI}
- 链路追踪:X-Ray服务地图自动生成拓扑
自动化部署
AWS CodePipeline流程:
安全加固
- 网络隔离:每个微服务部署到独立安全组
- 密钥管理:IAM角色访问SSM/RDS
- WAF防护:在API Gateway启用OWASP规则集
9. 成本优化策略
资源类型 | 优化方案 | 预期节省 |
---|---|---|
计算资源 | Fargate Spot实例 + 自动扩缩容 | 70%↓ |
数据库 | RDS Proxy + 读写分离 | 40%↓ |
API网关 | 按调用量计费 + 缓存响应 | 60%↓ |
服务发现 | Cloud Map按命名空间计费 | 90%↓ |
总结
通过结合Spring Cloud与AWS托管服务:
- 服务治理:Cloud Map替代Eureka,减少运维负担
- 配置安全:SSM Parameter Store实现加密配置管理
- 网关选择:轻量级Spring Cloud Gateway或企业级API Gateway
- 可观测性:X-Ray自动追踪跨服务调用链路
示例项目参考:
Spring Cloud微服务AWS部署模板
AWS微服务参考架构
最终架构价值:
✅ 100%托管基础设施
✅ 按需付费的精细化成本控制
✅ 自动恢复的高可用架构
✅ 开箱可用的安全合规能力