|
|
@@ -1,9 +1,12 @@
|
|
|
package com.lewaimai.mysql.elasticsearch;
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.alibaba.otter.canal.client.CanalConnector;
|
|
|
import com.alibaba.otter.canal.client.CanalConnectors;
|
|
|
@@ -11,25 +14,30 @@ import com.alibaba.otter.canal.protocol.Message;
|
|
|
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
|
|
import com.lewaimai.mysql.elasticsearch.common.AbstractConsumerLifeCycle;
|
|
|
import com.lewaimai.mysql.elasticsearch.common.BatchQueue;
|
|
|
-import com.lewaimai.mysql.elasticsearch.common.ConsumerLifeCycle;
|
|
|
+import com.lewaimai.mysql.elasticsearch.common.config.ApplicationConfig;
|
|
|
import com.lewaimai.mysql.elasticsearch.common.config.Canal;
|
|
|
import com.lewaimai.mysql.elasticsearch.common.config.ConfigBean;
|
|
|
-import com.lewaimai.mysql.elasticsearch.common.config.ConfigInstance;
|
|
|
+import com.lewaimai.mysql.elasticsearch.common.config.ConfigUtils;
|
|
|
|
|
|
/**
|
|
|
* 消费者服务,主要负责从Canal拉取数据变更事件。
|
|
|
* @author jogin
|
|
|
*
|
|
|
*/
|
|
|
+@Component
|
|
|
public class ConsumerServer extends AbstractConsumerLifeCycle {
|
|
|
private static Logger logger = LoggerFactory.getLogger(ConsumerLauncher.class);
|
|
|
+ @Autowired
|
|
|
+ private BatchQueue batchQueue;
|
|
|
+
|
|
|
public ConsumerServer() {
|
|
|
}
|
|
|
|
|
|
public void start() {
|
|
|
super.start();
|
|
|
+ ConfigUtils configUtils = ApplicationConfig.getContext().getBean(ConfigUtils.class);
|
|
|
//创建连接
|
|
|
- Canal canalConfig = ConfigInstance.instance().getConfig().getCanal();
|
|
|
+ Canal canalConfig = configUtils.getConfig().getCanal();
|
|
|
CanalConnector connector = CanalConnectors
|
|
|
.newSingleConnector(
|
|
|
new InetSocketAddress(canalConfig.getIp(), canalConfig.getPort()),
|
|
|
@@ -37,7 +45,7 @@ public class ConsumerServer extends AbstractConsumerLifeCycle {
|
|
|
canalConfig.getUser(),
|
|
|
canalConfig.getPassword());
|
|
|
try {
|
|
|
- ConfigBean configBean = ConfigInstance.instance().getConfig();
|
|
|
+ ConfigBean configBean = configUtils.getConfig();
|
|
|
connector.connect();
|
|
|
connector.subscribe(configBean.getDbName() + "\\..*");
|
|
|
connector.rollback();
|
|
|
@@ -56,10 +64,18 @@ public class ConsumerServer extends AbstractConsumerLifeCycle {
|
|
|
} else {
|
|
|
//处理binlog
|
|
|
logger.debug("处理消息. ");
|
|
|
-
|
|
|
- BatchQueue.instance().add(batchId, message.getEntries());
|
|
|
+ int batchQueueSize = batchQueue.getSize();
|
|
|
+ while (batchQueueSize == configBean.getBatchQueueSize()) {
|
|
|
+ try {
|
|
|
+ logger.debug("batch队列已满, sleep...");
|
|
|
+ batchAck(connector);
|
|
|
+ Thread.sleep(1000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ batchQueue.add(batchId, message.getEntries());
|
|
|
+ batchAck(connector);
|
|
|
}
|
|
|
-// connector.ack(batchId); // 提交确认
|
|
|
}
|
|
|
} catch (CanalClientException e) {
|
|
|
logger.error("Canel server连接不上.");
|
|
|
@@ -69,4 +85,17 @@ public class ConsumerServer extends AbstractConsumerLifeCycle {
|
|
|
logger.info("退出Consumer Server.");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量确认Canal消息
|
|
|
+ * @param connector
|
|
|
+ */
|
|
|
+ public void batchAck(CanalConnector connector) {
|
|
|
+ List<Long> batchIds = batchQueue.getCompleteBatch();
|
|
|
+ if (batchIds != null) {
|
|
|
+ for (Long batchId : batchIds) {
|
|
|
+ connector.ack(batchId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|