|
@@ -25,10 +25,7 @@ import org.springframework.scheduling.annotation.Async;
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.SecureRandom;
|
|
import java.security.SecureRandom;
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* binlog监听类
|
|
* binlog监听类
|
|
@@ -46,9 +43,6 @@ public class BinlogClientRunner implements CommandLineRunner {
|
|
@Autowired
|
|
@Autowired
|
|
private RwdObjectWdMapper rwdObjectWdMapper;
|
|
private RwdObjectWdMapper rwdObjectWdMapper;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private DBUtil dbUtil;
|
|
|
|
-
|
|
|
|
@Value("${binlog.host}")
|
|
@Value("${binlog.host}")
|
|
private String host;
|
|
private String host;
|
|
|
|
|
|
@@ -119,14 +113,6 @@ public class BinlogClientRunner implements CommandLineRunner {
|
|
log.info(tableName+"同步第"+i+"页,数量"+rwdObjectWds.size());
|
|
log.info(tableName+"同步第"+i+"页,数量"+rwdObjectWds.size());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if(binlogPosition==null){
|
|
|
|
- binlogPosition = new BinlogPosition();
|
|
|
|
- }
|
|
|
|
- binlogPosition.setServerId(serverId);
|
|
|
|
- //查询binlogName
|
|
|
|
- String binlogName = dbUtil.queryBinlogName("show binary logs");
|
|
|
|
- binlogPosition.setBinlogName(binlogName);
|
|
|
|
- binlogPositionService.saveOrUpdate(binlogPosition);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -199,30 +185,51 @@ public class BinlogClientRunner implements CommandLineRunner {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ //处理rotate事件
|
|
|
|
+ saveRotatePosition( event);
|
|
}
|
|
}
|
|
}));
|
|
}));
|
|
client.connect();
|
|
client.connect();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void saveRotatePosition(Event event){
|
|
|
|
+ List<EventType> excludePositionEventType = new ArrayList<>();
|
|
|
|
+ excludePositionEventType.add(EventType.FORMAT_DESCRIPTION);
|
|
|
|
+ excludePositionEventType.add(EventType.HEARTBEAT);
|
|
|
|
+ if (!excludePositionEventType.contains(event.getHeader().getEventType())) {
|
|
|
|
+ //处理rotate事件,这里会替换调binlog fileName
|
|
|
|
+ if (event.getHeader().getEventType().equals(EventType.ROTATE)) {
|
|
|
|
+ BinlogPosition binlogPositionSave = binlogPositionService.getPosition();
|
|
|
|
+ if(null == binlogPositionSave){
|
|
|
|
+ binlogPositionSave = new BinlogPosition();
|
|
|
|
+ }
|
|
|
|
+ RotateEventData rotateEventData = (RotateEventData) event.getData();
|
|
|
|
+ binlogPositionSave.setBinlogName(rotateEventData.getBinlogFilename());
|
|
|
|
+ binlogPositionSave.setPosition(rotateEventData.getBinlogPosition());
|
|
|
|
+ binlogPositionSave.setServerId(event.getHeader().getServerId());
|
|
|
|
+ //将最新的配置保存到mysql中
|
|
|
|
+ log.info("保存的数据{}", JSON.toJSONString(binlogPositionSave));
|
|
|
|
+ binlogPositionService.saveOrUpdate(binlogPositionSave);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private void saveBinlogPosition(Event event) {
|
|
private void saveBinlogPosition(Event event) {
|
|
BinlogPosition binlogPositionSave = new BinlogPosition();
|
|
BinlogPosition binlogPositionSave = new BinlogPosition();
|
|
//处理rotate事件,这里会替换调binlog fileName
|
|
//处理rotate事件,这里会替换调binlog fileName
|
|
if (event.getHeader().getEventType().equals(EventType.ROTATE)) {
|
|
if (event.getHeader().getEventType().equals(EventType.ROTATE)) {
|
|
- RotateEventData rotateEventData = (RotateEventData) event.getData();
|
|
|
|
- binlogPositionSave.setBinlogName(rotateEventData.getBinlogFilename());
|
|
|
|
- binlogPositionSave.setPosition(rotateEventData.getBinlogPosition());
|
|
|
|
- binlogPositionSave.setServerId(event.getHeader().getServerId());
|
|
|
|
|
|
+ saveRotatePosition(event);
|
|
} else {
|
|
} else {
|
|
//统一处理事件对应的binlog position
|
|
//统一处理事件对应的binlog position
|
|
binlogPositionSave = binlogPositionService.getPosition();
|
|
binlogPositionSave = binlogPositionService.getPosition();
|
|
- if(binlogPositionSave==null){
|
|
|
|
|
|
+ if(null == binlogPositionSave){
|
|
binlogPositionSave = new BinlogPosition();
|
|
binlogPositionSave = new BinlogPosition();
|
|
}
|
|
}
|
|
EventHeaderV4 eventHeaderV4 = (EventHeaderV4) event.getHeader();
|
|
EventHeaderV4 eventHeaderV4 = (EventHeaderV4) event.getHeader();
|
|
binlogPositionSave.setPosition(eventHeaderV4.getPosition());
|
|
binlogPositionSave.setPosition(eventHeaderV4.getPosition());
|
|
binlogPositionSave.setServerId(event.getHeader().getServerId());
|
|
binlogPositionSave.setServerId(event.getHeader().getServerId());
|
|
}
|
|
}
|
|
- //将最新的配置保存到Redis中
|
|
|
|
|
|
+ //将最新的配置保存到mysql中
|
|
log.info("保存的数据{}", JSON.toJSONString(binlogPositionSave));
|
|
log.info("保存的数据{}", JSON.toJSONString(binlogPositionSave));
|
|
binlogPositionService.saveOrUpdate(binlogPositionSave);
|
|
binlogPositionService.saveOrUpdate(binlogPositionSave);
|
|
}
|
|
}
|