mysql logger connector_聊聊canal的MysqlConnection

本文详细探讨了Canal框架中MysqlConnection的实现,包括connect、reconnect、disconnect方法以及如何通过ErosaConnection接口的seek和dump方法来获取和处理MySQL的binlog数据。MysqlConnection在连接、断开连接以及数据同步方面有着关键作用,通过不同dump方法支持基于binlog文件名、GTID和时间戳的数据同步。同时,还展示了如何查询服务器ID和处理心跳设置。
摘要由CSDN通过智能技术生成

本文主要研究一下canal的MysqlConnection

ErosaConnection

canal-1.1.4/parse/src/main/java/com/alibaba/otter/canal/parse/inbound/ErosaConnection.java

public interface ErosaConnection {

public void connect() throws IOException;

public void reconnect() throws IOException;

public void disconnect() throws IOException;

/**

* 用于快速数据查找,和dump的区别在于,seek会只给出部分的数据

*/

public void seek(String binlogfilename, Long binlogPosition, String gtid, SinkFunction func) throws IOException;

public void dump(String binlogfilename, Long binlogPosition, SinkFunction func) throws IOException;

public void dump(long timestamp, SinkFunction func) throws IOException;

/**

* 通过GTID同步binlog

*/

public void dump(GTIDSet gtidSet, SinkFunction func) throws IOException;

// -------------

public void dump(String binlogfilename, Long binlogPosition, MultiStageCoprocessor coprocessor) throws IOException;

public void dump(long timestamp, MultiStageCoprocessor coprocessor) throws IOException;

public void dump(GTIDSet gtidSet, MultiStageCoprocessor coprocessor) throws IOException;

ErosaConnection fork();

public long queryServerId() throws IOException;

}

ErosaConnection接口定义了connect、reconnect、disconnect、seek、dump、fork、queryServerId方法

MysqlConnection

canal-1.1.4/parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlConnection.java

public class MysqlConnection implements ErosaConnection {

private static final Logger logger = LoggerFactory.getLogger(MysqlConnection.class);

private MysqlConnector connector;

private long slaveId;

private Charset charset = Charset.forName("UTF-8");

private BinlogFormat binlogFormat;

private BinlogImage binlogImage;

// tsdb releated

private AuthenticationInfo authInfo;

protected int connTimeout = 5 * 1000; // 5秒

protected int soTimeout = 60 * 60 * 1000; // 1小时

private int binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;

// dump binlog bytes, 暂不包括meta与TSDB

private AtomicLong receivedBinlogBytes;

public MysqlConnection(){

}

public MysqlConnection(InetSocketAddress address, String username, String password){

authInfo = new AuthenticationInfo();

authInfo.setAddress(address);

authInfo.setUsername(username);

authInfo.setPassword(password);

connector = new MysqlConnector(address, username, password);

// 将connection里面的参数透传下

connector.setSoTimeout(soTimeout);

connector.setConnTimeout(connTimeout);

}

public MysqlConnection(InetSocketAddress address, String username, String password, byte charsetNumber,

String defaultSchema){

authInfo = new AuthenticationInfo();

authInfo.setAddress(address);

authInfo.setUsername(username);

authInfo.setPassword(password);

authInfo.setDefaultDatabaseName(defaultSchema);

connector = new MysqlConnector(address, username, password, charsetNumber, defaultSchema);

// 将connection里面的参数透传下

connector.setSoTimeout(soTimeout);

connector.setConnTimeout(connTimeout);

}

public void connect() throws IOException {

connector.connect();

}

public void reconnect() throws IOException {

connector.reconnect();

}

public void disconnect() throws IOException {

connector.disconnect();

}

public boolean isConnected() {

return connector.isConnected();

}

public MysqlConnection fork() {

MysqlConnection connection = new MysqlConnection();

connection.setCharset(getChars

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值