本项目是一个基于Spring Boot的REST应用,结合云原生技术栈(Docker、Kubernetes、Jenkins、Prometheus、Grafana)完成限流控制、持续集成部署、指标采集与扩容验证的全流程实践。
/api/hello- 返回固定JSON数据:{"msg": "hello"}/api/health- 健康检查接口/api/stress/test- 压测接口/api/stress/status- 压测状态接口
- 使用Bucket4j + Redis实现分布式限流
 - 支持每秒100次请求的限流策略
 - 超过限流阈值返回HTTP 429状态码
 - 支持多实例统一限流(加分项)
 
- 暴露Prometheus指标:
/actuator/prometheus - 监控端口:8998
 - 主要指标:
http_server_requests_seconds_count- 请求次数http_server_requests_seconds_sum- 响应时间总和
 
- 内置压测工具
 - 支持自定义并发数、总请求数、QPS
 - 实时统计成功率、响应时间等指标
 
- 后端框架: Spring Boot 2.1.13
 - 限流: Bucket4j + Redis
 - 监控: Micrometer + Prometheus
 - 容器化: Docker
 - 编排: Kubernetes
 - CI/CD: Jenkins
 - 可视化: Grafana
 
- Java 8+
 - Maven 3.6+
 - Redis 6.0+
 
# 1. 启动Redis
redis-server
# 2. 构建项目
mvn clean package
# 3. 运行应用
java -jar target/prometheus-test-demo-0.0.1-SNAPSHOT.jar# 测试hello接口
curl http://localhost:8080/api/hello
# 测试健康检查
curl http://localhost:8080/api/health
# 查看监控指标
curl http://localhost:8998/actuator/prometheus
# 执行压测
curl -X POST "http://localhost:8080/api/stress/test?totalRequests=1000&requestsPerSecond=200"docker build -t prometheus-test-demo:latest .docker run -d \
  --name prometheus-test-demo \
  -p 8080:8080 \
  -p 8998:8998 \
  --link redis:redis \
  prometheus-test-demo:latest# 创建命名空间
kubectl create namespace prometheus-demo
# 部署应用
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
# 部署监控
kubectl apply -f k8s/servicemonitor.yaml
# 部署自动扩缩容
kubectl apply -f k8s/hpa.yaml# 查看Pod状态
kubectl get pods -n prometheus-demo
# 查看服务
kubectl get svc -n prometheus-demo
# 端口转发
kubectl port-forward svc/prometheus-test-demo-service 8080:80 -n prometheus-demo- 代码拉取: 从Git仓库拉取最新代码
 - 构建测试: Maven构建并运行单元测试
 - 镜像构建: 多阶段Docker构建
 - 镜像推送: 推送到Harbor镜像仓库
 - K8s部署: 自动部署到Kubernetes集群
 - 健康检查: 验证部署状态
 
- 手动触发
 - Git Webhook自动触发(加分项)
 
- 监控端点: 
/actuator/prometheus - 采集间隔: 15秒
 - 超时时间: 10秒
 
包含以下监控面板:
- CPU使用率
 - 内存使用率
 - JVM指标(堆内存、线程数、GC次数)
 - 请求QPS
 - 请求平均响应时间
 
- 正常负载测试: QPS < 100
 - 限流验证测试: QPS > 100
 - 高并发测试: 验证自动扩缩容
 - 长时间稳定性测试: 验证系统稳定性
 
# 正常负载测试
curl -X POST "http://localhost:8080/api/stress/test?totalRequests=500&requestsPerSecond=50"
# 限流验证测试
curl -X POST "http://localhost:8080/api/stress/test?totalRequests=1000&requestsPerSecond=200"
# 高并发测试
curl -X POST "http://localhost:8080/api/stress/test?concurrency=50&totalRequests=2000&requestsPerSecond=300"prometheus-test-demo/
├── src/
│   ├── main/
│   │   ├── java/io/daocloud/prometheustestdemo/
│   │   │   ├── controller/          # REST控制器
│   │   │   ├── config/             # 配置类
│   │   │   ├── interceptor/        # 拦截器
│   │   │   ├── annotation/         # 自定义注解
│   │   │   └── util/               # 工具类
│   │   └── resources/
│   │       └── application.properties
│   └── test/                       # 单元测试
├── k8s/                           # Kubernetes配置
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── servicemonitor.yaml
│   └── hpa.yaml
├── jenkins/                       # Jenkins配置
│   └── scripts/
├── Dockerfile                     # Docker构建文件
├── Jenkinsfile                    # Jenkins流水线
└── pom.xml                       # Maven配置
- ✅ REST接口实现(5分)
 - ✅ 限流控制(10分)
 - ✅ Prometheus指标暴露(5分)
 - ✅ 统一限流机制(+5分加分项)
 
- ✅ Dockerfile多阶段构建(5分)
 - ✅ Kubernetes YAML配置(5分)
 - ✅ Jenkins CI流水线(5分)
 - ✅ Jenkins CD流水线(5分)
 ⚠️ Git Webhook自动触发(+5分加分项)
- ✅ Prometheus指标采集(5分)
 - ✅ Grafana监控面板(5分)
 - ✅ 压测验证(5分)
 - ✅ HPA自动扩缩容(+10分加分项)
 
- Redis依赖: 应用需要Redis服务,确保Redis可用
 - 资源限制: Kubernetes部署包含资源限制配置
 - 监控端口: 应用暴露8998端口用于监控指标
 - 健康检查: 配置了liveness和readiness探针
 - 日志级别: 可通过application.properties调整日志级别
 
- Redis连接失败: 检查Redis服务状态和连接配置
 - 限流不生效: 检查Redis连接和Bucket4j配置
 - 监控指标缺失: 检查Actuator配置和Prometheus采集
 - 压测失败: 检查网络连接和并发配置
 
# 查看应用日志
kubectl logs -f deployment/prometheus-test-demo -n prometheus-demo
# 查看多个Pod日志
kubectl logs -f -l app=prometheus-test-demo -n prometheus-demo欢迎提交Issue和Pull Request来改进项目。
本项目采用MIT许可证。