update:数据同步分发机构岗位绑定关系

This commit is contained in:
hewencai
2025-12-16 12:03:15 +08:00
parent a57d05ccd6
commit 8782631eaa
31 changed files with 1278 additions and 669 deletions

View File

@@ -66,13 +66,15 @@ public class HandlerRegistry {
public void init() { public void init() {
log.info("[HandlerRegistry] 开始初始化 Handler 注册表..."); log.info("[HandlerRegistry] 开始初始化 Handler 注册表...");
// 注册增量同步 Handler // 注册增量同步 Handler支持一个Handler注册多个事件类型
if (syncEventHandlers != null) { if (syncEventHandlers != null) {
for (SyncEventHandler<?> handler : syncEventHandlers) { for (SyncEventHandler<?> handler : syncEventHandlers) {
DatabusEventType eventType = handler.getSupportedEventType(); List<DatabusEventType> eventTypes = handler.getSupportedEventTypes();
incrementalHandlers.put(eventType, handler); for (DatabusEventType eventType : eventTypes) {
log.info("[HandlerRegistry] 注册增量Handler: {} -> {}", incrementalHandlers.put(eventType, handler);
eventType, handler.getClass().getSimpleName()); log.info("[HandlerRegistry] 注册增量Handler: {} -> {}",
eventType, handler.getClass().getSimpleName());
}
} }
} }

View File

@@ -5,22 +5,49 @@ import com.zt.plat.module.databus.enums.DatabusEventType;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
/** /**
* 同步事件处理器接口 * 同步事件处理器接口
* <p> * <p>
* 业务系统需要实现此接口来处理增量数据同步 * 业务系统需要实现此接口来处理增量数据同步
* <p>
* 支持单事件和多事件处理:
* - 单事件:实现 getSupportedEventType()
* - 多事件:实现 getSupportedEventTypes()推荐一个Handler处理多个操作
* *
* @author ZT * @author ZT
*/ */
public interface SyncEventHandler<T> { public interface SyncEventHandler<T> {
/** /**
* 获取支持的事件类型 * 获取支持的事件类型(单个)
* <p>
* 默认实现:返回 getSupportedEventTypes() 的第一个元素
* <p>
* 建议新Handler优先实现 getSupportedEventTypes()
* *
* @return 事件类型枚举 * @return 事件类型枚举
*/ */
DatabusEventType getSupportedEventType(); default DatabusEventType getSupportedEventType() {
List<DatabusEventType> types = getSupportedEventTypes();
return types.isEmpty() ? null : types.get(0);
}
/**
* 获取支持的事件类型列表(多个)
* <p>
* 默认实现:返回包含 getSupportedEventType() 的单元素列表
* <p>
* 子类可以覆盖此方法返回多个事件类型从而一个Handler处理多个操作
*
* @return 事件类型列表
*/
default List<DatabusEventType> getSupportedEventTypes() {
DatabusEventType type = getSupportedEventType();
return type == null ? Collections.emptyList() : Collections.singletonList(type);
}
/** /**
* 处理同步消息 * 处理同步消息

View File

@@ -1,50 +0,0 @@
package com.zt.plat.framework.databus.client.handler.dept;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusDeptData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
/**
* 部门创建事件处理器
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 DeptSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(DeptSyncService.class)
public class SystemDeptCreateHandler implements SyncEventHandler<DatabusDeptData> {
@Resource
private DeptSyncService deptSyncService;
@Override
public DatabusEventType getSupportedEventType() {
return DatabusEventType.SYSTEM_DEPT_CREATE;
}
@Override
public void handle(DatabusMessage<DatabusDeptData> message) {
DatabusDeptData data = message.getData();
log.info("[DeptSync] 收到部门创建事件, id={}, name={}, parentId={}",
data.getId(), data.getName(), data.getParentId());
try {
deptSyncService.create(data);
log.info("[DeptSync] 部门创建成功, id={}", data.getId());
} catch (Exception e) {
log.error("[DeptSync] 部门创建失败, id={}", data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -1,49 +0,0 @@
package com.zt.plat.framework.databus.client.handler.dept;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusDeptData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
/**
* 部门删除事件处理器
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 DeptSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(DeptSyncService.class)
public class SystemDeptDeleteHandler implements SyncEventHandler<DatabusDeptData> {
@Resource
private DeptSyncService deptSyncService;
@Override
public DatabusEventType getSupportedEventType() {
return DatabusEventType.SYSTEM_DEPT_DELETE;
}
@Override
public void handle(DatabusMessage<DatabusDeptData> message) {
DatabusDeptData data = message.getData();
log.info("[DeptSync] 收到部门删除事件, id={}", data.getId());
try {
deptSyncService.delete(data.getId());
log.info("[DeptSync] 部门删除成功, id={}", data.getId());
} catch (Exception e) {
log.error("[DeptSync] 部门删除失败, id={}", data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -0,0 +1,81 @@
package com.zt.plat.framework.databus.client.handler.dept;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusDeptData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* 部门增量同步事件处理器(合并版)
* <p>
* 处理事件类型:
* - SYSTEM_DEPT_CREATE部门创建
* - SYSTEM_DEPT_UPDATE部门更新
* - SYSTEM_DEPT_DELETE部门删除
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 DeptSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(DeptSyncService.class)
public class SystemDeptHandler implements SyncEventHandler<DatabusDeptData> {
@Resource
private DeptSyncService deptSyncService;
@Override
public List<DatabusEventType> getSupportedEventTypes() {
return Arrays.asList(
DatabusEventType.SYSTEM_DEPT_CREATE,
DatabusEventType.SYSTEM_DEPT_UPDATE,
DatabusEventType.SYSTEM_DEPT_DELETE
);
}
@Override
public void handle(DatabusMessage<DatabusDeptData> message) {
DatabusDeptData data = message.getData();
DatabusEventType eventType = message.getEventType();
log.info("[DeptSync] 收到部门{}事件, id={}, name={}, parentId={}",
eventType.getAction(), data.getId(), data.getName(), data.getParentId());
try {
switch (eventType) {
case SYSTEM_DEPT_CREATE:
deptSyncService.create(data);
log.info("[DeptSync] 部门创建成功, id={}", data.getId());
break;
case SYSTEM_DEPT_UPDATE:
deptSyncService.update(data);
log.info("[DeptSync] 部门更新成功, id={}", data.getId());
break;
case SYSTEM_DEPT_DELETE:
deptSyncService.delete(data.getId());
log.info("[DeptSync] 部门删除成功, id={}", data.getId());
break;
default:
log.warn("[DeptSync] 未知事件类型: {}", eventType);
}
} catch (Exception e) {
log.error("[DeptSync] 部门{}失败, id={}", eventType.getAction(), data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -1,49 +0,0 @@
package com.zt.plat.framework.databus.client.handler.dept;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusDeptData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
/**
* 部门更新事件处理器
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 DeptSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(DeptSyncService.class)
public class SystemDeptUpdateHandler implements SyncEventHandler<DatabusDeptData> {
@Resource
private DeptSyncService deptSyncService;
@Override
public DatabusEventType getSupportedEventType() {
return DatabusEventType.SYSTEM_DEPT_UPDATE;
}
@Override
public void handle(DatabusMessage<DatabusDeptData> message) {
DatabusDeptData data = message.getData();
log.info("[DeptSync] 收到部门更新事件, id={}, name={}", data.getId(), data.getName());
try {
deptSyncService.update(data);
log.info("[DeptSync] 部门更新成功, id={}", data.getId());
} catch (Exception e) {
log.error("[DeptSync] 部门更新失败, id={}", data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -1,50 +0,0 @@
package com.zt.plat.framework.databus.client.handler.post;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusPostData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
/**
* 岗位创建事件处理器
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 PostSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(PostSyncService.class)
public class SystemPostCreateHandler implements SyncEventHandler<DatabusPostData> {
@Resource
private PostSyncService postSyncService;
@Override
public DatabusEventType getSupportedEventType() {
return DatabusEventType.SYSTEM_POST_CREATE;
}
@Override
public void handle(DatabusMessage<DatabusPostData> message) {
DatabusPostData data = message.getData();
log.info("[PostSync] 收到岗位创建事件, id={}, name={}, code={}",
data.getId(), data.getName(), data.getCode());
try {
postSyncService.create(data);
log.info("[PostSync] 岗位创建成功, id={}", data.getId());
} catch (Exception e) {
log.error("[PostSync] 岗位创建失败, id={}", data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -1,49 +0,0 @@
package com.zt.plat.framework.databus.client.handler.post;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusPostData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
/**
* 岗位删除事件处理器
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 PostSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(PostSyncService.class)
public class SystemPostDeleteHandler implements SyncEventHandler<DatabusPostData> {
@Resource
private PostSyncService postSyncService;
@Override
public DatabusEventType getSupportedEventType() {
return DatabusEventType.SYSTEM_POST_DELETE;
}
@Override
public void handle(DatabusMessage<DatabusPostData> message) {
DatabusPostData data = message.getData();
log.info("[PostSync] 收到岗位删除事件, id={}", data.getId());
try {
postSyncService.delete(data.getId());
log.info("[PostSync] 岗位删除成功, id={}", data.getId());
} catch (Exception e) {
log.error("[PostSync] 岗位删除失败, id={}", data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

View File

@@ -0,0 +1,81 @@
package com.zt.plat.framework.databus.client.handler.post;
import com.zt.plat.framework.databus.client.handler.SyncEventHandler;
import com.zt.plat.module.databus.api.data.DatabusPostData;
import com.zt.plat.module.databus.api.message.DatabusMessage;
import com.zt.plat.module.databus.enums.DatabusEventType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* 岗位增量同步事件处理器(合并版)
* <p>
* 处理事件类型:
* - SYSTEM_POST_CREATE岗位创建
* - SYSTEM_POST_UPDATE岗位更新
* - SYSTEM_POST_DELETE岗位删除
* <p>
* 使用条件:
* 1. zt.databus.sync.client.enabled=true
* 2. 存在 PostSyncService Bean
*
* @author ZT
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnBean(PostSyncService.class)
public class SystemPostHandler implements SyncEventHandler<DatabusPostData> {
@Resource
private PostSyncService postSyncService;
@Override
public List<DatabusEventType> getSupportedEventTypes() {
return Arrays.asList(
DatabusEventType.SYSTEM_POST_CREATE,
DatabusEventType.SYSTEM_POST_UPDATE,
DatabusEventType.SYSTEM_POST_DELETE
);
}
@Override
public void handle(DatabusMessage<DatabusPostData> message) {
DatabusPostData data = message.getData();
DatabusEventType eventType = message.getEventType();
log.info("[PostSync] 收到岗位{}事件, id={}, name={}, code={}",
eventType.getAction(), data.getId(), data.getName(), data.getCode());
try {
switch (eventType) {
case SYSTEM_POST_CREATE:
postSyncService.create(data);
log.info("[PostSync] 岗位创建成功, id={}", data.getId());
break;
case SYSTEM_POST_UPDATE:
postSyncService.update(data);
log.info("[PostSync] 岗位更新成功, id={}", data.getId());
break;
case SYSTEM_POST_DELETE:
postSyncService.delete(data.getId());
log.info("[PostSync] 岗位删除成功, id={}", data.getId());
break;
default:
log.warn("[PostSync] 未知事件类型: {}", eventType);
}
} catch (Exception e) {
log.error("[PostSync] 岗位{}失败, id={}", eventType.getAction(), data.getId(), e);
throw e; // 抛出异常触发重试
}
}
}

Some files were not shown because too many files have changed in this diff Show More