一、组装数据
上一篇 只是正常启动,但是线程是等待中,没有数据接入处理。现在开始模拟同步数据,并分析其中原理
AbstractEventParser类的start方法
2、mysql主从复制重点就在这里了,当前因为 startposition里部位空对象,
EntryPosition[included=false,journalName=mysql-bin.000023,position=123,serverId=1,gtid=<null>,timestamp=1528939448000]
3、
// 重新链接,因为在找position过程中可能有状态,需要断开后重建 erosaConnection.reconnect();
改变一下状态:connect为true、
4、dum数据
// 4. 开始dump数据 // 判断所属instance是否启用GTID模式,是的话调用ErosaConnection中GTID对应方法dump数据 if (isGTIDMode()) { erosaConnection.dump(MysqlGTIDSet.parse(startPosition.getGtid()), sinkHandler); } else { if (StringUtils.isEmpty(startPosition.getJournalName()) && startPosition.getTimestamp() != null) { erosaConnection.dump(startPosition.getTimestamp(), sinkHandler); } else { erosaConnection.dump(startPosition.getJournalName(), startPosition.getPosition(), sinkHandler); } }
4.1 dump详细内容
public void dump(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException { updateSettings(); sendRegisterSlave(); sendBinlogDump(binlogfilename, binlogPosition); DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize()); fetcher.start(connector.getChannel()); LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT); LogContext context = new LogContext(); while (fetcher.fetch()) { LogEvent event = null; event = decoder.decode(fetcher, context); if (event == null) { throw new CanalParseException("parse failed"); } if (!func.sink(event)) { break; } if (event.getSemival() == 1) { sendSemiAck(context.getLogPosition().getFileName(), binlogPosition); } } }
4.1.1 设置mysql的通用参数,心跳间隔时间,内存参数
private void updateSettings() throws IOException { try { update("set wait_timeout=9999999"); } catch (Exception e) { logger.warn("update wait_timeout failed", e); } try { update("set net_write_timeout=1800"); } catch (Exception e) { logger.warn("update net_write_timeout failed", e); }
构造器初始化第一个参数
public QueryCommandPacket(){ setCommand((byte) 0x03); }
public byte[] toBytes() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(getCommand()); out.write(getQueryString().getBytes("UTF-8"));// 链接建立时默认指定编码为UTF-8 return out.toByteArray(); }
二、发送数据
三、接受数据
mysql协议规定了,通用接受数据的每个类型;参考链接