# 基于Spring Cloud的高可用在线视频平台项目架构 ## 技术栈选型 根据2024年最新技术栈和Spring Cloud Alibaba 2023.0.1.0版本,选用以下组件: - **框架基础**: - Spring Boot 3.2.4 - Spring Cloud 2023.0.1 - Spring Cloud Alibaba 2023.0.1.0 - **核心组件**: - Nacos 2.3.2 (服务注册与配置中心) - Sentinel 1.8.6 (流量控制) - Seata 2.0.0 (分布式事务) - RocketMQ 5.1.4 (消息队列) - **其他组件**: - Spring Cloud Gateway (API网关) - OpenFeign (服务调用) - Spring Security + OAuth2 + JWT (安全认证) - MySQL 8.0 (主数据库) - Redis 7.0 (缓存) - MinIO (分布式文件存储) - FFmpeg (视频处理) - Prometheus + Grafana (监控) - Docker + DockerCompose (容器化部署) ## IntelliJ IDEA 创建步骤 ### 1. 创建父工程 (Maven项目) 1. 打开IntelliJ IDEA,选择"File" -> "New" -> "Project" 2. 选择"Maven",点击"Next" 3. 填写项目信息: - GroupId: `com.nuaa.video` - ArtifactId: `video-platform` - Version: `1.0.0` 4. 点击"Finish"创建项目 5. 修改父工程pom.xml: ```xml 4.0.0 org.springframework.boot spring-boot-starter-parent 3.2.4 com.nuaa.video video-platform 1.0.0 pom video-platform 基于Spring Cloud的高可用在线视频平台 17 UTF-8 UTF-8 2023.0.1 2023.0.1.0 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import video-common video-gateway video-auth video-user video-content video-comment video-recommend video-admin org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} ${project.build.sourceEncoding} ``` ### 2. 创建公共模块 (video-common) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-common` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-common的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-common org.projectlombok lombok true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-validation com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-openfeign com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.baomidou mybatis-plus-boot-starter 3.5.5 mysql mysql-connector-java 8.0.33 org.springframework.boot spring-boot-starter-data-redis io.minio minio 8.5.7 cn.hutool hutool-all 5.8.26 io.jsonwebtoken jjwt-api 0.12.3 io.jsonwebtoken jjwt-impl 0.12.3 runtime io.jsonwebtoken jjwt-jackson 0.12.3 runtime ``` ### 3. 创建API网关模块 (video-gateway) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-gateway` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-gateway的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-gateway org.springframework.cloud spring-cloud-starter-gateway com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.cloud spring-cloud-alibaba-sentinel-gateway org.springframework.boot spring-boot-starter-actuator ``` 6. 创建application.yml: ```yaml server: port: 9000 spring: application: name: video-gateway cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP gateway: discovery: locator: enabled: true lower-case-service-id: true routes: - id: video-auth uri: lb://video-auth predicates: - Path=/auth/** filters: - StripPrefix=1 - id: video-user uri: lb://video-user predicates: - Path=/user/** filters: - StripPrefix=1 - id: video-content uri: lb://video-content predicates: - Path=/content/** filters: - StripPrefix=1 - id: video-comment uri: lb://video-comment predicates: - Path=/comment/** filters: - StripPrefix=1 - id: video-recommend uri: lb://video-recommend predicates: - Path=/recommend/** filters: - StripPrefix=1 - id: video-admin uri: lb://video-admin predicates: - Path=/admin/** filters: - StripPrefix=1 sentinel: transport: dashboard: 5.180.253.115:8080 port: 8719 eager: true scg: fallback: mode: response response-body: '{"code":500,"message":"服务不可用,请稍后再试"}' management: endpoints: web: exposure: include: '*' ``` 7. 创建主启动类`GatewayApplication.java`: ```java package com.nuaa.video.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } } ``` ### 4. 创建认证服务模块 (video-auth) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-auth` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-auth的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-auth com.nuaa.video video-common ${project.version} org.springframework.boot spring-boot-starter-oauth2-resource-server org.springframework.security spring-security-oauth2-authorization-server 1.2.1 ``` 6. 创建application.yml: ```yaml server: port: 9001 spring: application: name: video-auth cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8720 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_auth?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: sty@G458221 driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 0 mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl jwt: secret: video-platform-secret-key-2024-nuaa expiration: 86400 token-header: Authorization ``` 7. 创建主启动类`AuthApplication.java`: ```java package com.nuaa.video.auth; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class AuthApplication { public static void main(String[] args) { SpringApplication.run(AuthApplication.class, args); } } ``` ### 5. 创建用户服务模块 (video-user) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-user` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-user的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-user com.nuaa.video video-common ${project.version} ``` 6. 创建application.yml: ```yaml server: port: 9002 spring: application: name: video-user cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8721 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: sty@G458221 driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 1 mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 7. 创建主启动类`UserApplication.java`: ```java package com.nuaa.video.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } } ``` ### 6. 创建内容服务模块 (video-content) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-content` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-content的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-content com.nuaa.video video-common ${project.version} org.apache.rocketmq rocketmq-spring-boot-starter 2.2.3 net.bramp.ffmpeg ffmpeg 0.7.0 ``` 6. 创建application.yml: ```yaml server: port: 9003 spring: application: name: video-content cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8722 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_content?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 2 rocketmq: name-server: 5.180.253.115:9876 producer: group: video-content-group mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl minio: endpoint: http://5.180.253.115:9002 accessKey: minioadmin secretKey: minioadmin bucketName: video-platform ``` 7. 创建主启动类`ContentApplication.java`: ```java package com.nuaa.video.content; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ContentApplication { public static void main(String[] args) { SpringApplication.run(ContentApplication.class, args); } } ``` ### 7. 创建评论服务模块 (video-comment) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-comment` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-comment的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-comment com.nuaa.video video-common ${project.version} ``` 6. 创建application.yml: ```yaml server: port: 9004 spring: application: name: video-comment cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8723 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_comment?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: sty@G458221 driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 3 mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 7. 创建主启动类`CommentApplication.java`: ```java package com.nuaa.video.comment; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class CommentApplication { public static void main(String[] args) { SpringApplication.run(CommentApplication.class, args); } } ``` ### 8. 创建推荐服务模块 (video-recommend) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-recommend` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-recommend的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-recommend com.nuaa.video video-common ${project.version} org.apache.spark spark-mllib_2.12 3.4.1 org.apache.commons commons-math3 3.6.1 ``` 6. 创建application.yml: ```yaml server: port: 9005 spring: application: name: video-recommend cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8724 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_recommend?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: sty@G458221 driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 4 mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 7. 创建主启动类`RecommendApplication.java`: ```java package com.nuaa.video.recommend; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class RecommendApplication { public static void main(String[] args) { SpringApplication.run(RecommendApplication.class, args); } } ``` ### 9. 创建管理后台模块 (video-admin) 1. 右键父工程 -> New -> Module 2. 选择"Maven",点击"Next" 3. 填写模块信息: - Name: `video-admin` - Parent: `com.nuaa.video:video-platform` 4. 点击"Finish" 5. 修改video-admin的pom.xml: ```xml video-platform com.nuaa.video 1.0.0 4.0.0 video-admin com.nuaa.video video-common ${project.version} ``` 6. 创建application.yml: ```yaml server: port: 9006 spring: application: name: video-admin cloud: nacos: discovery: server-addr: 5.180.253.115:8848 namespace: public group: DEFAULT_GROUP sentinel: transport: dashboard: 5.180.253.115:8080 port: 8725 eager: true datasource: url: jdbc:mysql://5.180.253.115:3306/video_admin?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: sty@G458221 driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: 5.180.253.115 port: 6379 password: redis_EsJ8FX database: 5 mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 7. 创建主启动类`AdminApplication.java`: ```java package com.nuaa.video.admin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class, args); } } ```