java_demo/README.md

240 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Jenkins Demo - Spring Boot 3 CI/CD 实践项目
这是一个用于实践Jenkins CI/CD流程的Spring Boot 3示例项目。项目包含完整的代码拉取、编译构建、单元测试、代码扫描、Docker打包和部署流程。
## 🚀 项目特性
- **Spring Boot 3.2.6** + **JDK 17**
- **RESTful API** - 提供用户管理相关接口
- **单元测试** - 完整的JUnit 5测试覆盖
- **代码质量** - 集成SonarQube代码扫描
- **Docker支持** - 多阶段构建优化
- **Jenkins Pipeline** - 完整的CI/CD流程
- **健康检查** - 应用程序监控端点
## 📋 API接口
### 健康检查
- `GET /api/health` - 应用健康状态
- `GET /api/info` - 应用信息
- `GET /api/welcome` - 欢迎信息
### 用户管理
- `GET /api/users` - 获取所有用户
- `GET /api/users/{id}` - 获取指定用户
- `POST /api/users` - 创建新用户
- `PUT /api/users/{id}` - 更新用户信息
- `DELETE /api/users/{id}` - 删除用户
- `POST /api/users/{id}/activate` - 激活用户
- `POST /api/users/{id}/deactivate` - 停用用户
- `GET /api/users/stats` - 用户统计信息
## 🛠️ 技术栈
- **后端框架**: Spring Boot 3.2.6
- **Java版本**: JDK 17
- **构建工具**: Maven 3.9+
- **测试框架**: JUnit 5 + MockMvc
- **代码覆盖率**: JaCoCo
- **容器化**: Docker + Docker Compose
- **CI/CD**: Jenkins Pipeline
## 🏃‍♂️ 快速开始
### 本地开发
1. **克隆项目**
```bash
git clone <repository-url>
cd jenkins-demo
```
2. **编译项目**
```bash
mvn clean compile
```
3. **运行测试**
```bash
mvn test
```
4. **启动应用**
```bash
mvn spring-boot:run
```
5. **访问应用**
- 应用地址: http://localhost:8080
- 健康检查: http://localhost:8080/api/health
- 用户列表: http://localhost:8080/api/users
### Docker运行
1. **构建镜像**
```bash
docker build -t jenkins-demo:latest .
```
2. **运行容器**
```bash
docker run -d --name jenkins-demo -p 8080:8080 jenkins-demo:latest
```
3. **使用Docker Compose**
```bash
docker-compose up -d
```
## 🔧 Maven命令
```bash
# 编译项目
mvn clean compile
# 运行测试
mvn test
# 生成代码覆盖率报告
mvn test jacoco:report
# 打包应用
mvn clean package
# 跳过测试打包
mvn clean package -DskipTests
# 代码质量扫描需要配置SonarQube
mvn sonar:sonar
# 启动应用
mvn spring-boot:run
```
## 🔍 代码质量
项目集成了以下代码质量工具:
- **JaCoCo** - 代码覆盖率分析
- **SonarQube** - 静态代码分析
- **Maven Surefire** - 单元测试报告
生成测试报告:
```bash
mvn clean test jacoco:report
```
报告位置:`target/site/jacoco/index.html`
## 🚀 Jenkins CI/CD流程
### Pipeline阶段
1. **代码检出** - 从Git仓库拉取代码
2. **环境检查** - 验证Java、Maven等工具版本
3. **编译** - 编译Java源代码
4. **单元测试** - 运行JUnit测试并生成报告
5. **代码扫描** - SonarQube静态代码分析
6. **打包** - 生成可执行JAR文件
7. **Docker构建** - 构建Docker镜像
8. **镜像推送** - 推送到Docker Registry
9. **自动部署** - 部署到目标环境
10. **健康检查** - 验证应用启动状态
### 环境要求
Jenkins服务器需要安装
- JDK 17
- Maven 3.9+
- Docker
- Git
- SonarQube Scanner可选
### 部署配置
修改`Jenkinsfile`中的以下配置:
```groovy
environment {
DOCKER_REGISTRY = 'your-registry.com' // Docker仓库地址
SONAR_HOST_URL = 'http://your-sonar-server:9000' // SonarQube服务器
// ... 其他配置
}
```
## 📦 项目结构
```
jenkins-demo/
├── src/
│ ├── main/
│ │ ├── java/com/jenkins/demo/
│ │ │ ├── JenkinsDemoApplication.java # 应用主类
│ │ │ ├── controller/ # REST控制器
│ │ │ ├── service/ # 业务服务
│ │ │ └── model/ # 数据模型
│ │ └── resources/
│ │ └── application.yml # 应用配置
│ └── test/ # 测试代码
├── Dockerfile # Docker镜像构建
├── Jenkinsfile # Jenkins Pipeline
├── docker-compose.yml # Docker Compose配置
├── pom.xml # Maven配置
└── README.md # 项目文档
```
## 🔗 API示例
### 创建用户
```bash
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"name": "测试用户"
}'
```
### 获取所有用户
```bash
curl http://localhost:8080/api/users
```
### 健康检查
```bash
curl http://localhost:8080/api/health
```
## 📊 监控端点
Spring Boot Actuator提供了以下监控端点
- `/actuator/health` - 健康状态
- `/actuator/info` - 应用信息
- `/actuator/metrics` - 应用指标
- `/actuator/prometheus` - Prometheus指标如果启用
## 🤝 贡献指南
1. Fork项目
2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
3. 提交更改 (`git commit -m 'Add some amazing feature'`)
4. 推送到分支 (`git push origin feature/amazing-feature`)
5. 开启Pull Request
## 📄 许可证
本项目使用MIT许可证。详情请参见[LICENSE](LICENSE)文件。
## 📞 联系方式
如有问题或建议,请通过以下方式联系:
- 项目Issues: [GitHub Issues](https://github.com/your-username/jenkins-demo/issues)
- 邮箱: your-email@example.com
---
🎉 **祝您使用愉快!** 如果这个项目对您有帮助请给个⭐Star支持一下