本次改造将 section 同步从“controller 直接调用专用批处理 service”的方式,收敛为“controller + 公共编排 service + 业务处理器”的统一批量同步架构。
目标收益:
文件:ruoyi-server/ruoyi-server-sync/src/main/java/org/dromara/server/sync/controller/SyncReceiveController.java
入口方法:
syncSections(BatchSyncRequest<SectionRecordRequest> request)职责:
/api/sync/receive/sections 请求request.businessType = SyncResourceConstants.SECTIONCommonBatchSyncService.sync(request)BatchSyncResult文件:ruoyi-server/ruoyi-server-sync/src/main/java/org/dromara/server/sync/business/batch/CommonBatchSyncService.java
主方法:
sync(BatchSyncRequest<T> request)职责:
request 和 recordsbusinessType 获取业务处理器SyncBatchTrackerService.start(...)BatchSyncResult关键内部方法:
getHandler(...):根据 businessType 获取处理器partition(...):按固定大小切分分片executeChunks(...):执行分片,同步或并发buildResult(...):构造最终返回结果resolveStatus(...):计算批次状态文件:ruoyi-server/ruoyi-server-sync/src/main/java/org/dromara/server/sync/business/batch/BatchSyncBusinessHandler.java
作用: 定义公共编排层与具体业务处理器之间的统一契约。
核心方法:
getBusinessType()getApiName()getResourceType()resolveTenantId(...)validateRecord(...)getBizKey(...)toDispatchDto(...)dispatchChunk(...)文件:ruoyi-server/ruoyi-server-sync/src/main/java/org/dromara/server/sync/business/batch/SectionBatchSyncService.java
注册方式:
@Service(SyncResourceConstants.SECTION)实现接口:
BatchSyncBusinessHandler<SectionRecordRequest, RemoteSectionSyncBatchDto>职责拆分:
validateRecord(...)
getBizKey(...)
sectionId 作为错误定位主键toDispatchDto(...)
RemoteSectionSyncBatchDtodispatchChunk(...)
SyncBatchErrorRecordsyncSections(...)
CommonBatchSyncService接口文件:ruoyi-api/ruoyi-api-ecs/src/main/java/org/dromara/ecs/api/RemoteSectionSyncService.java
实现文件:ruoyi-modules/ruoyi-ecs/src/main/java/org/dromara/ecs/dubbo/RemoteSectionSyncServiceImpl.java
调用方法:
syncSectionBatch(List<RemoteSectionSyncBatchDto> records)职责:
ISyncSectionService.receiveSectionBatch(...)RemoteBatchSyncResultDto文件:ruoyi-modules/ruoyi-ecs/src/main/java/org/dromara/ecs/service/sync/SyncSectionServiceImpl.java
核心方法:
receiveSectionBatch(...)processSection(...)职责:
delFlag=1 的删除组delFlag=0 的新增/修改组otherId 更新删除标记otherId 判断新增或修改文件:
ruoyi-modules/ruoyi-ecs/src/main/java/org/dromara/ecs/service/impl/EcsSectionServiceImpl.javaruoyi-modules/ruoyi-ecs/src/main/java/org/dromara/ecs/mapper/EcsSectionMapper.java职责:
queryByOtherId(...):查询已存在记录insertByBo(...):新增课表updateByBo(...):更新课表updateDelFlagByOtherId(...):按 otherId + tenantId 更新删除标记/api/sync/receive/sectionsSyncReceiveController.syncSections(...)businessType = SECTIONCommonBatchSyncService.sync(...)SpringUtils.getBean("SECTION", BatchSyncBusinessHandler.class) 获取 SectionBatchSyncServiceSyncBatchTrackerService.start(...) 创建批次RemoteSectionSyncBatchDtoSectionBatchSyncService.dispatchChunk(...)dispatchChunk(...) 调用 RemoteSectionSyncService.syncSectionBatch(...)RemoteSectionSyncServiceImpl.syncSectionBatch(...) 转内部 DTO 后调用 SyncSectionServiceImpl.receiveSectionBatch(...)SyncBatchTrackerService.saveErrors(...) 保存错误明细SyncBatchTrackerService.finish(...) 更新批次状态BatchSyncResult在 CommonBatchSyncService 中约定:
CHUNK_SIZE = 100MAX_PARALLEL_CHUNKS = 5执行规则:
threadPoolTaskExecutor错误来源分两类:
发生位置:公共编排层
处理方式:
SyncBatchTrackerService.error(...) 构造 VALIDATION_ERROR发生位置:section 分片处理器或远端服务
处理方式:
BIZ_ERRORSyncBatchErrorRecordSyncBatchTrackerService.saveErrors(...) 批量保存SyncReceiveControllerCommonBatchSyncServiceBatchSyncBusinessHandlerSectionBatchSyncServiceSyncBatchTrackerServiceBatchSyncRequestBatchSyncResultBatchChunkSyncResultRemoteSectionSyncServiceRemoteSectionSyncServiceImplSyncSectionServiceImplEcsSectionServiceImplEcsSectionMapper如果后续要接入新的同步类型,如 course、classroom:
SyncResourceConstants 中增加业务类型常量BatchSyncBusinessHandlerbusinessTypeCommonBatchSyncService.sync(...)这样就不需要重复实现批次校验、分片、并发、错误归集和批次跟踪逻辑。