# nexa-cloud **Repository Path**: zr53722/nexa-cloud ## Basic Information - **Project Name**: nexa-cloud - **Description**: 数字商品交易平台的后端微服务,采用 **Spring Boot 3 + Spring Cloud 2025 + 微服务 + 前后端分离** 架构。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-09-13 - **Last Updated**: 2026-09-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: Java, 虚拟充值, 供应链 ## README # Nexa Cloud(基于 Spring Cloud Alibaba 的微服务平台) > **Spring Boot 3 + Spring Cloud 2025 + 微服务 + 前后端分离** 架构。 **当前状态**:已完成工程骨架、统一基础设施(starter)、OAuth2.1 + JWT 认证授权、MyBatis-Plus 数据访问与 112 个存量表实体,并补齐了商户端 / 运营侧 / 管理端的核心接口。 部分高级能力(Redis 缓存、Kafka 消息、Outbox 等 starter)仍在按阶段(P0 → P8)演进。 ## 项目技术栈 ### 后端(Java 微服务) | 类别 | 技术 / 框架 | 说明 | |---|---|---| | 语言 / 运行 | **Java 21 (JDK)**, Temurin LTS | 全平台统一 JDK 21 | | 构建 | **Maven 3.9+** 多模块聚合 | 根 POM 汇聚全部模块 | | 版本管理 | **BOM(`nexa-cloud-bom`)** | 三方法 + 平台内部构件单版本集中管理 | | 基础框架 | **Spring Boot 3.5.16** | 微服务运行基座 | | 微服务治理 | **Spring Cloud 2025.0.3** (Northfields) | 微服务核心组件 | | 注册发现 / 配置 | **Spring Cloud Alibaba 2025.0.0.0 + Nacos** | 服务注册发现、配置中心 | | 服务间调用 | **OpenFeign** | Feign 契约包 + 令牌中继 + TraceId 透传 | | API 网关 | **Spring Cloud Gateway(`nexa-gateway`)** | 统一路由、鉴权、跨域、Swagger 文档聚合 | | 安全认证 | **Spring Security + Spring Authorization Server(OAuth2.1)** | 授权服务器 + 资源服务器 | | 令牌载体 | **JWT** | Bearer Token,管理者/商户/开放平台多种客户端模式 | | 数据访问 | **MyBatis-Plus 3.5.16** (Spring Boot 3 starter) | CRUD、Wrapper、分页、审计填充 | | 分页 / 拦截器 | **mybatis-plus-jsqlparser** | 分页 / 多租户 / 数据权限插件 | | 数据库 | **MySQL** | 沿用存量 wtcloud 业务库 | | 接口文档 | **Springdoc OpenAPI 2.8.17**(Swagger UI) | 网关聚合各服务 api-docs,支持 JWT Authorize | | Excel | **EasyExcel 3.3.4** | 导入导出 | | 架构守护 | **ArchUnit 1.5.0**(JUnit5) | CI 强制分层规范(controller→application→domain→infra) | | 测试 | **JUnit 5** + spring-boot-starter-test | | ### 前端(`nexa-cloud-web`,Vue 3 生态) | 子项目 | 说明 | |---|---| | `merchant-web` | 商户中心(Arco Design Pro for Vue) | | `manage-web` | 管理运营后台 | | `stock-web` | 库存管理后台 | | `demo-portal` | 演示门户 | 核心前端技术栈: | 类别 | 技术 | |---|---| | 框架 | **Vue 3**(Composition API) | | 语言 | **TypeScript** | | 构建 | **Vite 2** | | 状态管理 | **Pinia** + persistedstate 持久化插件 | | 路由 | **Vue Router 4**(后端菜单动态路由) | | UI 组件库 | **Arco Design Vue**(字节跳动) | | HTTP | **Axios**(请求/响应拦截、Token 与商户上下文注入) | | 图表 / 其他 | ECharts、Vue I18n、dayjs、xlsx、big.js、qrcode.vue、lodash | 代码质量:ESLint + Prettier + Stylelint + Commitlint + Husky + lint-staged。 ### 中间件与基础设施 | 组件 | 用途 | |---|---| | **Nacos** | 服务注册发现、配置中心 | | MySQL | 业务数据库(存量 wtcloud 业务库) | | Redis(规划) | 缓存 / 分布式锁 / 幂等 | | Kafka(规划) | 消息队列 / Outbox 模式 | | Spring Authorization Server | OAuth2.1 认证授权中心 | ## 模块(后端) ``` nexa/ ├── nexa-cloud-bom/ # 统一版本管理(三方 BOM + 平台内部构件) ├── nexa-cloud-starter/ # 统一基础设施层 │ ├── nexa-starter-web/ # ✅ 统一响应体、全局异常、TraceId、分页模型 │ ├── nexa-starter-security/ # ✅ OAuth2 资源服务器、scope/角色切面、商户上下文 │ ├── nexa-starter-doc/ # ✅ Springdoc + JWT 安全方案(Swagger Authorize) │ ├── nexa-starter-feign/ # ✅ 令牌中继与 TraceId 透传 │ ├── nexa-starter-mybatis/ # ✅ MyBatis-Plus + MySQL + 分页 + 审计填充 + 慢 SQL │ └── nexa-starter-{redis,kafka,outbox,idempotent,lock,id,cache,dict,excel, │ monitor,test,notify,approval,file,operation-log}/ # 按阶段推进 ├── nexa-cloud-api/ # 跨服务 Feign 契约包(merchant / order / finance / stock) ├── nexa-cloud-services/ # 业务服务(4 个) │ ├── nexa-merchant-service/ # 8101 商户 / 用户 / 实名 / 商品类目 / 授信 / 提现 / RBAC / 菜单 │ ├── nexa-order-service/ # 8102 进货单 / 供货单 / 直充 / 售后逆向 / 运营订单 │ ├── nexa-finance-service/ # 8103 资金账户 / 流水 / 结算 / 退款 / 对账 │ └── nexa-stock-service/ # 8104 库存 / 卡密 / 批次 / 出入库 / 配额 ├── nexa-cloud-infra/ │ ├── nexa-gateway/ # 9080 路由 + 鉴权 + 跨域 + 文档聚合 │ └── nexa-auth/ # 9000 OAuth2.1 授权服务器 ├── nexa-cloud-arch/ # ArchUnit 架构守护(CI 必跑) └── deploy/ # compose / nginx / sql ``` ## 版本基线 | 项 | 版本 | |---|---| | JDK | 21(Temurin LTS) | | 构建 | Maven 3.9+ 多模块 + BOM | | Spring Boot | 3.5.16 | | Spring Cloud | 2025.0.3(Northfields) | | Spring Security / Authorization Server | 6.5.x / 1.5.x | | Spring Cloud Alibaba | 2025.0.0.0(Nacos 注册发现 + 配置中心) | | MyBatis-Plus | 3.5.16 | | Springdoc OpenAPI | 2.8.17(Swagger UI) | | ArchUnit | 1.5.0 | | EasyExcel | 3.3.4 | ## 目录结构 ``` nexa/ ├── nexa-cloud-bom/ # 统一版本管理(三方 BOM + 平台内部构件) ├── nexa-cloud-starter/ # 统一基础设施层 │ ├── nexa-starter-web/ # ✅ 统一响应体、全局异常、TraceId、分页模型 │ ├── nexa-starter-security/ # ✅ OAuth2 资源服务器、scope/角色切面、商户上下文 │ ├── nexa-starter-doc/ # ✅ Springdoc + JWT 安全方案(Swagger Authorize) │ ├── nexa-starter-feign/ # ✅ 令牌中继与 TraceId 透传 │ ├── nexa-starter-mybatis/ # ✅ MyBatis-Plus + MySQL + 分页 + 审计填充 + 慢 SQL │ └── nexa-starter-{redis,kafka,outbox,idempotent,lock,id,cache,dict,excel, │ monitor,test,notify,approval,file,operation-log}/ # 占位,待各阶段实现 ├── nexa-cloud-api/ # 跨服务 Feign 契约包(merchant / order / finance / stock) ├── nexa-cloud-services/ # 业务服务(4 个) │ ├── nexa-merchant-service/ # 8101 │ ├── nexa-order-service/ # 8102(含售后逆向 + 直充子域) │ ├── nexa-finance-service/ # 8103 │ └── nexa-stock-service/ # 8104 ├── nexa-cloud-infra/ │ ├── nexa-gateway/ # 9080 路由 + 鉴权 + 文档聚合 │ └── nexa-auth/ # 9000 OAuth2.1 授权服务器 ├── nexa-cloud-arch/ # ArchUnit 架构守护(CI 必跑) └── deploy/ # compose / nginx / sql ``` ## 服务与端口 | 服务 | 端口 | Swagger | |---|---|---| | `nexa-gateway` | 9080 | http://localhost:9080/swagger-ui.html(聚合全部服务) | | `nexa-auth` | 9000 | http://localhost:9000/swagger-ui.html | | `nexa-merchant-service` | 8101 | http://localhost:8101/swagger-ui.html | | `nexa-order-service` | 8102 | http://localhost:8102/swagger-ui.html | | `nexa-finance-service` | 8103 | http://localhost:8103/swagger-ui.html | | `nexa-stock-service` | 8104 | http://localhost:8104/swagger-ui.html | ## 构建与启动 ```bash # 全量构建(含 ArchUnit 架构守护测试) mvn -B clean install # 单服务构建 mvn -B -pl nexa-cloud-services/nexa-order-service -am clean package -DskipTests # 启动顺序:auth → 各业务服务 → gateway mvn -pl nexa-cloud-infra/nexa-auth spring-boot:run ``` 本地快速启动(无需 MySQL / Redis / Kafka,但需要 **Nacos 注册中心**): ```bash mvn -pl nexa-cloud-infra/nexa-auth -am spring-boot:run # 9000 mvn -pl nexa-cloud-services/nexa-merchant-service spring-boot:run # 8101 mvn -pl nexa-cloud-infra/nexa-gateway spring-boot:run # 9080 ``` ## 服务注册与发现(Nacos) 本机 Nacos 地址已配置在各服务 `application.yml`: | 项 | 值 | |---|---| | 服务发现 API | `http://127.0.0.1:18081/nacos/`(`spring.cloud.nacos.server-addr=127.0.0.1:18081`,context-path `/nacos`) | | 控制台界面 | `http://127.0.0.1:18082/` | | 默认账号密码 | `nacos / nacos` | - 各服务以 `spring.application.name` 作为 Nacos 服务名自动注册(已验证:启动后控制台可看到实例); - 网关路由使用 `lb://`,由 Nacos 解析实例 + 客户端负载均衡,不再写死服务地址; - 若需多副本,直接启动多个实例即可自动负载均衡。 ## Nacos 配置中心(集中管理 + MySQL 持久化 + 动态刷新) 本平台中间件与业务配置(Redis / Kafka / 数据源 / 网关路由 / 卡密密钥等)已统一集中到 **Nacos 配置中心**管理,服务启动时从 Nacos 拉取,且支持**动态刷新**(改配置无需重启)。 ### 1. 架构:配置即代码(Config-as-Code) ``` nexa-config-center/(git 权威源,人编辑) │ publish-config.ps1 发布 ▼ Nacos 配置中心(运行时源,MySQL 持久化) │ 启动时 / 刷新时拉取 ▼ 6 个微服务(本地 application.yml 只留引导信息) ``` - **权威源**:`nexa-config-center/` 目录(进 git,有版本历史 / 评审 / 回滚); - **运行时源**:Nacos(数据持久化到本地 MySQL 的 `nacos` 库,重启不丢); - 微服务本地 `application.yml` **不写业务配置**,只留:`server.port` 兜底、 Nacos 地址、`spring.cloud.nacos.config.*` 拉取清单。 ### 2. Nacos 服务器侧:持久化到 MySQL | 项 | 配置 / 操作 | |---|---| | 数据源开关 | `spring.sql.init.platform=mysql`(Nacos 3.x 默认 Derby) | | 数据源连接 | `conf/application.properties` 中 `db.url.0` / `db.user` / `db.password` 指向本机 `nacos` 库 | | 建表 | 执行 `conf/mysql-schema.sql`(生成 `config_info` / `users` / `roles` 等表) | | 驱动 | MySQL 驱动 jar 放入 Nacos 的 `plugins/mysql/` 目录 | | 账号 | 手工向 `nacos.users` / `roles` 插入 `nacos` 账号(密码用官方 BCrypt 值)+ `ROLE_ADMIN`,否则 3.x 无法登录 | 判断持久化是否生效:登录控制台后,配置数据能在 MySQL `nacos.config_info` 表中查到。 ### 3. 配置源码仓库 `nexa-config-center/` | 文件 | 角色 | |---|---| | `nexa-common.yml` | 公共配置(Redis / Kafka / 资源服务器 / MyBatis-Plus / SpringDoc / Actuator / 日志),所有服务共享 | | `nexa-gateway.yml` | 网关专属:Sentinel / Gateway 路由 / 放行路径 / 文档聚合 | | `nexa-auth.yml` 等 6 个 | 各服务专属:端口 / 数据源 / 文档信息 / 卡密密钥 | | `publish-config.ps1` | 发布脚本(登录 → 逐个发布 `*.yml` 到 `DEFAULT_GROUP`) | 发布脚本要点(踩坑记录): - **用 `Invoke-RestMethod`,不要用 `curl.exe --data-urlencode`**:后者把内容拼进命令行, Windows 命令行参数上限约 8191 字符,大配置(如 `nexa-gateway.yml` 6000+ 字符)会被**截断**, 导致服务拉到残缺配置(表现为路由 404 / 放行失效); - **脚本须以 UTF-8 BOM 保存**:PowerShell 5.1 默认按 ANSI 读取无 BOM 脚本,中文注释乱码; - Nacos 3.x 已废弃 v1 API,登录与发布走 v3 API (`/nacos/v3/auth/user/login`、`/nacos/v3/admin/cs/config`)。 使用流程: ```powershell cd F:\faliang\java\nexa-config-center .\publish-config.ps1 # 全量发布目录下所有 yml ``` ### 4. 服务侧:传统 Nacos Config 方式(支持热更新) 各服务 `application.yml` 的 `spring.cloud.nacos` 段(以网关为例,其余服务仅 `application.name` 不同): ```yaml spring: application: name: nexa-gateway cloud: nacos: server-addr: 127.0.0.1:18081 username: nacos password: nacos config: # 自动加载 ${spring.application.name}.yml(即 nexa-gateway.yml) file-extension: yml # 公共配置共享,refresh=true 支持热刷新 shared-configs: - data-id: nexa-common.yml group: DEFAULT_GROUP refresh: true refresh-enabled: true # 开启动态刷新 ``` > 注意:Data ID 必须与 `spring.application.name` 完全一致,传统方式才能自动加载; > 去掉 `optional:` 后,Nacos 不可用时服务将**启动失败**(fail-fast,配置中心的标准预期行为)。 ### 5. 动态刷新机制(改配置免重启) 1. Nacos 配置变更 → 服务内 `NacosContextRefresher` 长轮询感知 → 发布 `RefreshEvent`; 2. `ContextRefresher` 重拉环境 + `RefreshScope.refreshAll()`; 3. 所有 `@ConfigurationProperties` Bean(如 `GatewayProperties`、`SecurityProperties`、 `OpenApiProperties`)由 `ConfigurationPropertiesRebinder` 自动重新绑定,**无需手动加注解**; 4. 带 `@RefreshScope` 的 Bean 销毁重建(重新注入 `@Value`); 5. Spring Cloud Gateway `RouteRefreshListener` 重建路由。 已落地代码: - `nexa-stock-service` 的 `CardCipher` 加 `@RefreshScope`(`nexa.card.secret` 经 `@Value` 注入, 密钥变更时重建 Bean,支持密钥轮转)。 **已知边界**: - 网关 `SecurityWebFilterChain`(permit-all 放行路径)在启动时构建,**改放行路径仍需重启网关**; - 其余业务配置(路由 / 日志级别 / 数据源 / 密钥等)改完 Nacos 即热生效。 ### 6. 日常运维流程 ``` 改配置:编辑 nexa-config-center/xxx.yml(或直接在 Nacos 控制台改) 发布: .\nexa-config-center\publish-config.ps1 生效: 除安全链外自动热更新,无需重启;改完建议把内容同步回本地文件保持 git 一致 ``` ## 已落地的两套能力 ### 1. Swagger(Springdoc + JWT) - 由 `nexa-starter-doc` 自动装配,各服务只在 `application.yml` 里改标题,无需 Java 配置; - 安全方案为 `bearer-jwt`,打开 Swagger UI 右上角 **Authorize** 粘贴访问令牌即可调试; - 生产环境用 `nexa.doc.enabled=false` 一键关闭; - 网关聚合了 5 个服务的 `api-docs`(`springdoc.swagger-ui.urls`)。 ### 2. 授权(OAuth2.1 + JWT) 签发方 `nexa-auth` 内置四类客户端(骨架阶段为内存实现,P1 迁移到 `nexa_auth` 库): | client_id | 授权模式 | scope | |---|---|---| | `admin-web` | authorization_code + PKCE | `openid profile admin:read admin:write` | | `merchant-web` | authorization_code + PKCE | `openid profile merchant:read merchant:write` | | `openapi` | client_credentials | `openapi:order:write` `openapi:order:read` `openapi:product:read` `openapi:account:read` | | `internal-service` / `job-client` | client_credentials | `internal` | 消费方 `nexa-starter-security`: - 配置 `spring.security.oauth2.resourceserver.jwt.issuer-uri` 即启用 JWT 资源服务器; - 未认证返回业务码 `10005`(前端自动登出),无权限返回 `10006`; - 注解:`@RequiresScope(...)`(全部命中)、`@RequiresRole(...)`(任一命中),由 AOP 切面执行; - `SecurityUtils` 读取 JWT 中的 `sub / username / roles / merchantId / merchantCode`; - `MerchantContext` 注入当前商户上下文(请求头 `X-Merchant-Code` 优先于 JWT claim)。 > 未配置 `issuer-uri` 时 starter 会装配"放行全部"的兜底过滤链,方便本地先跑通 Swagger; > 上线前务必配置 `issuer-uri`。 ### 统一响应码(沿用前端既有约定) | code | 含义 | |---|---| | `10000` | 成功 | | `10001` | 业务校验失败(参数 / 状态不合法) | | `10002` | 业务逻辑错误 | | `10003` | 特殊业务返回(前端自行处理,不弹错) | | `10005` | 登录失效(前端自动登出) | | `10006` | 无权限 | | `10500` | 服务异常 | ## 数据访问与实体(wtcloud 存量库) - 连接信息在各服务 `application.yml` 的 `spring.datasource.*`,可用环境变量 `NEXA_DB_URL` / `NEXA_DB_USERNAME` / `NEXA_DB_PASSWORD` 覆盖;当前指向 `wtcloud_beta`。 - 实体由存量库 DDL 生成,落在各服务 `domain.entity` 包(ArchUnit 要求实体不反向依赖 `infra`): | 服务 | 实体数 | 覆盖 | |---|---|---| | merchant | 63 | 商户 / 用户 / 实名 / 银行卡 / API 密钥 / 授信 / 提现 / 商品类目 / 商品 / 风控 / RBAC / BD 基础数据 | | stock | 21 | 库存 / 卡密 / 批次 / 组合库存 / 出入库单 / 配额 / 错卡池 / 下载中心 | | order | 14 | 进货单 / FP 单 / 供货单 / 卡密回传 / 直充 / 批量改单 / 售后 | | finance | 14 | 资金账户 / 流水 / 冻结 / 支付 / 退款 / 费用 / 可提现 / 日汇总 / 批次结存 | - 主键:自增表为 `@TableId(type = AUTO)`(71 个),业务雪花 ID 表为 `ASSIGN_ID`(41 个); - 审计字段 `created_at` / `updated_at`(含 `create_time` 等历史命名变体)由 `AuditMetaObjectHandler` 自动填充; - MySQL 保留字列(`key` / `desc`)已用反引号包裹; - 临时/备份表(`*_opstmp1/2`、`cdks_daily_settlement_20260209`、`cdks_daily_settlement_backup`、`migrations`)不生成实体; - Mapper 尚未生成,约定放在各服务 `infra.repository` 包(`@MapperScan` 已配置)。 ## 接口清单(Stub) 所有 Controller 已完成 Swagger 标注、路径与出入参占位,**方法体仅保留 TODO**。 | 服务 | Controller 分组 | |---|---| | auth | 认证登录、OAuth2 客户端、用户凭证 | | merchant | 商户档案 / 协作者 / 银行卡 / API 密钥 / 实名审核 / 授信 / 提现 / 对账报表;商品类目 / 供货商品 / 进货商品 / FP 分销;风控规则 / 黑名单;系统菜单 / 角色 / 用户 / 基础数据 / 操作日志 / 前端菜单树 | | order | 进货单 / FP 单 / 供货单 / 供货单推送;售后单;直充子域;风控执行;运营侧订单;开放平台(订单 / 商品 / 账户) | | finance | 账户 / 加款 / 冻结解冻 / 结算分账 / 退款 / 提现扣款 / 授信还款 / 资金流水 / 汇总对账 | | stock | 库存 / 卡密 / 批次 / 入库单 / 出库单 / 配额 / 错卡池 / 下载中心 / 运营侧库存资产 | ## 约定 - 请求/响应统一 `R`,分页统一 `PageResult`; - 路径 `/api/v1/{资源}`(复数、小写连字符),动作类接口用 `/actions/{动作}`; - 开放平台独立前缀 `/openapi/v1/**`; - 实体已定义(见上文「数据访问与实体」),DTO 待各阶段编写;接口出入参仍用 `Object` 占位,Swagger 中显示为 `object`; - 分层:服务内 `controller → application → domain → infra`,由 `nexa-cloud-arch` 的 ArchUnit 规则守护。 ## 后续阶段(P0 → P8) 见设计文档 §9:P0 地基 → P1 认证 → P2 通用能力 starter → P3 资金 → P4 库存 → P5 商户 → P6 交易(含直充)→ P7 开放平台 → P8 切换上线。