fix(databus): 修复客户端消息处理和防止消息循环

1. 修复消息格式不匹配问题
   - 增量消息:兼容 SyncMessage 格式,从 dataSnapshot 字段反序列化数据
   - 批量消息:添加 getDataType() 方法获取泛型类型,正确转换 JSONObject

2. 防止消息循环
   - 添加 zt.databus.change.producer.enabled 配置项
   - 客户端禁用变更消息发送,避免 客户端写入 → 发送变更 → 循环

3. 修复 Feign 客户端注入
   - 在 RpcConfiguration 中添加 DeptApi、PostApi
   - 确保客户端能通过 Feign 调用本地 system-server API

相关文件:
- DatabusClientConsumer.java: 修复消息解析逻辑
- BatchSyncEventHandler.java: 添加 getDataType() 方法
- DatabusChangeProducer.java: 添加 enabled 开关
- RpcConfiguration.java: 启用 DeptApi/PostApi Feign 客户端

Ref: 修复 ClassCastException 和消息循环问题
This commit is contained in:
hewencai
2025-12-03 11:10:57 +08:00
parent dfca38feb7
commit 62494ced45
37 changed files with 659 additions and 41 deletions

View File

@@ -8,6 +8,7 @@ import com.zt.plat.module.system.dal.dataobject.dept.PostDO;
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
@@ -16,6 +17,9 @@ import jakarta.annotation.Resource;
* Databus 数据变更消息生产者
* <p>
* 用于发送部门、用户、岗位变更消息到 MQ供 databus-server 消费
* <p>
* 注意:客户端系统(分公司)应该禁用此功能,避免形成消息循环
* 配置项zt.databus.change.producer.enabled=false
*
* @author ZT
*/
@@ -26,6 +30,15 @@ public class DatabusChangeProducer {
@Resource
private RocketMQTemplate rocketMQTemplate;
/**
* 是否启用变更消息发送
* <p>
* 集团侧(数据源):设置为 true发送变更消息
* 分公司侧(客户端):设置为 false禁用变更消息避免循环
*/
@Value("${zt.databus.change.producer.enabled:true}")
private boolean enabled;
// ==================== 部门变更消息 ====================
/**
@@ -61,6 +74,11 @@ public class DatabusChangeProducer {
}
private void sendDeptChangeMessage(DatabusDeptChangeMessage message) {
if (!enabled) {
log.debug("[Databus] 变更消息发送已禁用, 跳过部门变更消息, action={}, deptId={}",
message.getAction(), message.getDeptId());
return;
}
try {
rocketMQTemplate.asyncSend(DatabusDeptChangeMessage.TOPIC, message, new org.apache.rocketmq.client.producer.SendCallback() {
@Override
@@ -118,6 +136,11 @@ public class DatabusChangeProducer {
}
private void sendUserChangeMessage(DatabusUserChangeMessage message) {
if (!enabled) {
log.debug("[Databus] 变更消息发送已禁用, 跳过用户变更消息, action={}, userId={}",
message.getAction(), message.getUserId());
return;
}
try {
rocketMQTemplate.asyncSend(DatabusUserChangeMessage.TOPIC, message, new org.apache.rocketmq.client.producer.SendCallback() {
@Override
@@ -171,6 +194,11 @@ public class DatabusChangeProducer {
}
private void sendPostChangeMessage(DatabusPostChangeMessage message) {
if (!enabled) {
log.debug("[Databus] 变更消息发送已禁用, 跳过岗位变更消息, action={}, postId={}",
message.getAction(), message.getPostId());
return;
}
try {
rocketMQTemplate.asyncSend(DatabusPostChangeMessage.TOPIC, message, new org.apache.rocketmq.client.producer.SendCallback() {
@Override

View File

@@ -187,4 +187,10 @@ seata:
undo:
logTable: undo_log
dataValidation: true
logSerialization: jackson
logSerialization: jackson
zt:
databus:
# 变更消息生产者配置
change:
producer:
enabled: false