public void Start() { _canalLogger?.LogDebug($"### [{_xdpCanalOption.LogSource}] Canal Client starting..."); Task.Factory.StartNew( () => { // 创建一个简单CanalClient连接对象(此对象不支持集群) // 传入参数分别为 Canal-Server地址、端口、Destination、用户名、密码 _canalConnector = CanalConnectors.NewSingleConnector( _xdpCanalOption.CanalServerIP, _xdpCanalOption.CanalServerPort, _xdpCanalOption.Destination, _xdpCanalOption.UserName, _xdpCanalOption.Password); // 连接 Canal _canalConnector.Connect(); // 订阅,同时传入Filter,如果不传则以Canal的Filter为准。 // Filter是一种过滤规则,通过该规则的表数据变更才会传递过来 _canalConnector.Subscribe(_xdpCanalOption.Filter); while (true) { // 获取数据 BufferSize表示数据大小,单位为字节,默认会发送ACK来表示消费成功 var message = _canalConnector.Get(_xdpCanalOption.BufferSize); // 也可以使用下面这个GetWithoutAck方法表示不发送ACK来表示消费成功 //var message = _canalConnector.GetWithoutAck(_xdpCanalOption.BufferSize); // 批次id 可用于回滚 var batchId = message.Id; if (batchId == -1 || message.Entries.Count <= 0) { Thread.Sleep(_xdpCanalOption.SleepTime); continue; } // 记录变更数据到自定义业务持久化存储区域 if (message.Entries.Count > 0) { RecordChanges(message.Entries); } } }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); _compositeTask = Task.CompletedTask; _canalLogger?.LogDebug($"### [{_xdpCanalOption.LogSource}] Canal Client started..."); }
/// <summary> /// 异步 获取数据 /// </summary> private void LazyCanalGetEntities() { new Thread(() => { _logger.LogInformation("canal receive worker thread start ..."); while (!_isDispose) { try { if (!PreparedAndEnqueue()) { continue; } //队列里面先储备5批次 超过 5批次的话 就开始消费一个批次 在储备 if (_canalQueue.Count >= 5) { _logger.LogInformation("canal receive worker waitOne ..."); _resetFlag = true; _condition.WaitOne(); _resetFlag = false; _logger.LogInformation("canal receive worker continue ..."); } } catch (IOException io) { _logger.LogError(io, "canal receive data err ..."); try { _canalConnector.Connect(); _canalConnector.Subscribe(_canalOption.Subscribe); } catch (Exception) { //ignore } } catch (Exception e) { //ignore _logger.LogError(e, "canal receive data err ..."); } } }).Start(); }
public Task StartAsync(CancellationToken cancellationToken) { try { _canalConnector = CanalConnectors.NewSingleConnector(_canalOption.Host, _canalOption.Port, _canalOption.Destination, _canalOption.MysqlName, _canalOption.MysqlPwd); _canalConnector.Connect(); _canalConnector.Subscribe(_canalOption.Subscribe); _canalConnector.Rollback(); _canalTimer = new System.Threading.Timer(CanalGetData, null, _canalOption.Timer * 1000, _canalOption.Timer * 1000); _logger.LogInformation("canal client start success..."); AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit; } catch (Exception ex) { _logger.LogError(ex, "canal client start error..."); } return(Task.CompletedTask); }
public Task StartAsync(CancellationToken cancellationToken) { try { _canalConnector = CanalConnectors.NewSingleConnector(_canalOption.Host, _canalOption.Port, _canalOption.Destination, _canalOption.MysqlName, _canalOption.MysqlPwd); _canalConnector.Connect(); _canalConnector.Subscribe(_canalOption.Subscribe); _canalConnector.Rollback(); AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit; _logger.LogInformation("canal client start ..."); LazyCanalGetEntities(); LazyCanalDoWork(); CanalServerAckStart(); } catch (Exception ex) { _logger.LogError(ex, "canal client start error..."); } return(Task.CompletedTask); }