# demo_springboot **Repository Path**: attacker/demo_springboot ## Basic Information - **Project Name**: demo_springboot - **Description**: java spring boot 测试jar - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-07 - **Last Updated**: 2025-09-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Demo SpringBoot 项目 基于Spring Boot + Nacos + Redis + MySQL的微服务示例项目。 ## 快速开始 ### 环境要求 - JDK 17+ - Maven 3.8+ - MySQL 8.0+ - Redis 6.0+ - Nacos 2.1.2 2021.0.6.0 ### 数据库初始化 1. **创建数据库** ```sql CREATE DATABASE bookdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ``` 2. **导入数据表** ```bash mysql -u root -p bookdb < init.sql ``` ### Redis配置 项目支持Redis单机和集群两种部署模式,无需修改代码,直接在Nacos配置中心进行配置切换。 #### Redis单机模式 在Nacos配置中心的 `demo-springboot.yml` 文件中添加以下配置: ```yaml spring: redis: # Redis服务器地址 host: 10.10.10.99 # Redis服务器端口 port: 6379 # 连接超时时间 timeout: 5s # 密码(可选) # password: your_password # 数据库索引(默认为0) # database: 0 lettuce: pool: # 连接池最大连接数 max-active: 50 # 连接池最大空闲连接数 max-idle: 20 # 连接池最小空闲连接数 min-idle: 5 # 连接池最大阻塞等待时间 max-wait: 4s ``` #### Redis集群模式 在Nacos配置中心的 `demo-springboot.yml` 文件中添加以下配置: **阿里云Redis集群配置:** ```yaml spring: redis: # 连接超时时间 timeout: 5s # 密码(可选) # password: your_password lettuce: pool: # 连接池最大连接数 max-active: 50 # 连接池最大空闲连接数 max-idle: 20 # 连接池最小空闲连接数 min-idle: 5 # 连接池最大阻塞等待时间 max-wait: 4s cluster: refresh: # 开启自适应集群拓扑刷新 adaptive: true # 集群拓扑刷新周期(秒) period: 60 cluster: # 阿里云Redis集群(单个访问地址) nodes: test-hk-trust-redis-cluster.ast1001.com:6379 # max-redirects 会自动计算,无需配置 ``` **自建Redis集群配置:** ```yaml spring: redis: timeout: 5s # password: your_password lettuce: pool: max-active: 50 max-idle: 20 min-idle: 5 max-wait: 4s cluster: refresh: adaptive: true period: 60 cluster: # 自建Redis集群(多个节点地址) nodes: redis-1:6379,redis-2:6379,redis-3:6379,redis-4:6379,redis-5:6379,redis-6:6379 # max-redirects: 8 # 可选:手动指定,不配置则根据节点数自动计算 ``` #### 配置切换说明 - **单机模式**:只配置 `spring.redis.host` 和 `spring.redis.port` - **集群模式**:配置 `spring.redis.cluster.nodes`,系统会自动检测并使用集群模式 - **配置优先级**:如果同时配置了单机和集群,集群配置优先生效 - **智能重定向**:`max-redirects` 会根据节点数量自动计算,无需手动配置 #### 重定向次数自动计算规则 | 集群类型 | 节点数量 | 自动计算的重定向次数 | 说明 | |----------|----------|---------------------|------| | 阿里云集群 | 1个地址 | 3次 | 云服务推荐值 | | 小型集群 | 2-3个节点 | 3次 | 最小推荐值 | | 中型集群 | 4-6个节点 | 等于节点数 | 如6个节点=6次重定向 | | 大型集群 | 7+个节点 | 最大10次 | 防止过度重定向 | #### 常用配置示例 **开发环境(单机):** ```yaml spring: redis: host: localhost port: 6379 timeout: 3s ``` **测试环境(阿里云集群):** ```yaml spring: redis: cluster: nodes: test-redis-cluster.ast1001.com:6379 timeout: 5s password: your_password ``` **生产环境(自建集群):** ```yaml spring: redis: password: your_secure_password cluster: nodes: prod-redis-1:6379,prod-redis-2:6379,prod-redis-3:6379,prod-redis-4:6379,prod-redis-5:6379,prod-redis-6:6379 timeout: 10s lettuce: pool: max-active: 100 max-idle: 50 min-idle: 10 ``` ### 配置说明 - 本地配置:`src/main/resources/application.yml` (应用名称、Nacos连接) - Nacos配置:`demo-springboot.yml` (Redis、MySQL、日志等) #### Nacos配置步骤 1. **配置Nacos** 在Nacos中创建配置文件: - Data ID: `demo-springboot.yml` - Group: `DEFAULT_GROUP` - 命名空间: `test` - 配置内容: 参考 `demo-springboot.yml` 文件 ```bash # 启动Nacos (如果还没启动) sh startup.sh -m standalone ``` 2. **启动应用** ```bash ./start.sh ``` ## 容器部署 ### Docker构建和运行 ```bash # 1. 构建JAR包 mvn clean package -DskipTests # 2. 构建Docker镜像 docker build -t demo-springboot:latest . # 3. 运行容器 docker run -d -p 9080:9080 \ -e nacos.config.server-addr=nacos.opsx.vip:8848 \ -e nacos.config.namespace=test \ -e nacos.config.username=nacos \ -e nacos.config.password=xxxxx \ demo-springboot:latest ``` ### 常见问题 #### 1. Java版本不兼容 **错误**: `UnsupportedClassVersionError: class file version 61.0` **解决**: 确保容器使用Java 17运行时 #### 2. Nacos配置导入失败 **错误**: `No spring.config.import property has been defined` **解决**: 已在application.yml中添加`spring.config.import: optional:nacos:demo-springboot.yml` > ⚠️ **注意**: 项目使用Java 17编译,Spring Cloud 2021.x+版本 ### API接口 启动后访问: #### API文档 - **Swagger UI**: - **OpenAPI JSON**: #### 主要接口 ##### 1. 图书管理 (`/api/books`) - **获取图书列表**: `GET /api/books` - **创建图书**: `POST /api/books` ##### 2. 用户管理 (`/api/user`) - **用户登录**: `POST /api/user/login/` ##### 3. Redis管理 (`/api/redis`) - **测试连接**: `GET /api/redis/ping` - **缓存操作**: `GET/POST/DELETE /api/redis/cache` - **类型检查**: `GET /api/redis/type` ##### 4. 健康检查和监控 (`/actuator`) - **应用健康状态**: `GET /actuator/health` - **性能指标**: `GET /actuator/metrics` - **应用信息**: `GET /actuator/info` **快速测试:** ```bash # 1. 获取图书列表 curl "http://localhost:9080/api/books" # 2. 用户登录 curl -X POST "http://localhost:9080/api/user/login/" \ -H "Content-Type: application/json" \ -d '{"username":"super123","password":"super@123"}' # 3. 测试Redis连接 curl "http://localhost:9080/api/redis/ping" # 4. Redis缓存操作 curl -X POST "http://localhost:9080/api/redis/cache" \ -H "Content-Type: application/json" \ -d '{"key":"test","value":"hello","type":"string"}' # 5. 检查应用健康状态 curl "http://localhost:9080/actuator/health" # 6. 查看性能指标 curl "http://localhost:9080/actuator/metrics" ``` ## 前端管理界面 访问 `http://localhost:5176` 使用现代化Vue3管理界面: - **Dashboard**: 系统概览和状态监控 - **图书管理**: 图书增删改查操作 - **Redis管理**: 缓存操作、类型检查、数据可视化 - **用户认证**: 登录/登出功能 默认登录账号:`super123` / `super@123` ## 监控和健康检查 ### Spring Boot Actuator 项目集成了Spring Boot Actuator,提供完整的应用监控和健康检查功能。 #### 健康检查端点 - **综合健康状态**: `/actuator/health` - 包含数据库连接状态 - Redis连接状态 - 自定义健康检查(内存使用率等) - JVM运行状态 - **性能监控**: `/actuator/metrics` - JVM内存使用情况 - HTTP请求统计 - 数据库连接池状态 - 系统资源使用情况 #### K8s健康检查配置 ```yaml # livenessProbe - 存活探针 livenessProbe: failureThreshold: 3 httpGet: path: /actuator/health port: 9080 scheme: HTTP initialDelaySeconds: 180 periodSeconds: 5 successThreshold: 1 timeoutSeconds: 1 # readinessProbe - 就绪探针 readinessProbe: failureThreshold: 3 httpGet: path: /actuator/health port: 9080 scheme: HTTP initialDelaySeconds: 180 periodSeconds: 5 successThreshold: 1 timeoutSeconds: 1 ``` ### 调试和诊断功能 项目还包含以下调试组件: - **网络诊断**: 启动时自动检查数据库、Redis、DNS连接 - **配置调试**: 打印所有配置信息,便于排查配置问题 - **详细日志**: 记录连接状态和错误信息 > 💡 **提示**: 更多API接口详情请访问 [Swagger文档](http://localhost:9080/swagger-ui.html)