mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
fixed mq
This commit is contained in:
@@ -3,11 +3,8 @@ package com.alibaba.otter.canal.adapter.launcher.loader;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.protocol.FlatMessage;
|
|
||||||
import com.alibaba.otter.canal.protocol.Message;
|
|
||||||
import org.apache.kafka.common.errors.WakeupException;
|
import org.apache.kafka.common.errors.WakeupException;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
||||||
@@ -44,8 +41,12 @@ public class CanalAdapterKafkaWorker extends AbstractCanalAdapterWorker {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process() {
|
protected void process() {
|
||||||
while (!running)
|
while (!running) {
|
||||||
;
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
ExecutorService workerExecutor = Executors.newSingleThreadExecutor();
|
ExecutorService workerExecutor = Executors.newSingleThreadExecutor();
|
||||||
int retry = canalClientConfig.getRetries() == null || canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
int retry = canalClientConfig.getRetries() == null || canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
||||||
long timeout = canalClientConfig.getTimeout() == null ? 30000 : canalClientConfig.getTimeout(); // 默认超时30秒
|
long timeout = canalClientConfig.getTimeout() == null ? 30000 : canalClientConfig.getTimeout(); // 默认超时30秒
|
||||||
|
|||||||
@@ -3,11 +3,7 @@ package com.alibaba.otter.canal.adapter.launcher.loader;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import com.alibaba.otter.canal.protocol.FlatMessage;
|
|
||||||
import com.alibaba.otter.canal.protocol.Message;
|
|
||||||
import org.apache.kafka.common.errors.WakeupException;
|
import org.apache.kafka.common.errors.WakeupException;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
||||||
@@ -33,18 +29,27 @@ public class CanalAdapterRocketMQWorker extends AbstractCanalAdapterWorker {
|
|||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
this.flatMessage = flatMessage;
|
this.flatMessage = flatMessage;
|
||||||
this.canalDestination = topic;
|
this.canalDestination = topic;
|
||||||
this.connector = new RocketMQCanalConnector(nameServers, topic, groupId, accessKey, secretKey, flatMessage);
|
this.connector = new RocketMQCanalConnector(nameServers,
|
||||||
|
topic,
|
||||||
|
groupId,
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
canalClientConfig.getBatchSize(),
|
||||||
|
flatMessage);
|
||||||
logger.info("RocketMQ consumer config topic:{}, nameServer:{}, groupId:{}", topic, nameServers, groupId);
|
logger.info("RocketMQ consumer config topic:{}, nameServer:{}, groupId:{}", topic, nameServers, groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process() {
|
protected void process() {
|
||||||
while (!running)
|
while (!running) {
|
||||||
;
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ExecutorService workerExecutor = Executors.newSingleThreadExecutor();
|
ExecutorService workerExecutor = Executors.newSingleThreadExecutor();
|
||||||
int retry = canalClientConfig.getRetries() == null
|
int retry = canalClientConfig.getRetries() == null || canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
||||||
|| canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
|
||||||
long timeout = canalClientConfig.getTimeout() == null ? 30000 : canalClientConfig.getTimeout(); // 默认超时30秒
|
long timeout = canalClientConfig.getTimeout() == null ? 30000 : canalClientConfig.getTimeout(); // 默认超时30秒
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ package com.alibaba.otter.canal.adapter.launcher.loader;
|
|||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.client.CanalConnector;
|
import com.alibaba.otter.canal.client.CanalConnector;
|
||||||
import com.alibaba.otter.canal.client.CanalConnectors;
|
import com.alibaba.otter.canal.client.CanalConnectors;
|
||||||
@@ -58,11 +59,16 @@ public class CanalAdapterWorker extends AbstractCanalAdapterWorker {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process() {
|
protected void process() {
|
||||||
while (!running)
|
while (!running) { // waiting until running == true
|
||||||
; // waiting until running == true
|
while (!running) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int retry = canalClientConfig.getRetries() == null
|
int retry = canalClientConfig.getRetries() == null || canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
||||||
|| canalClientConfig.getRetries() == 0 ? 1 : canalClientConfig.getRetries();
|
|
||||||
if (retry == -1) {
|
if (retry == -1) {
|
||||||
// 重试次数-1代表异常时一直阻塞重试
|
// 重试次数-1代表异常时一直阻塞重试
|
||||||
retry = Integer.MAX_VALUE;
|
retry = Integer.MAX_VALUE;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.alibaba.otter.canal.client.rocketmq;
|
package com.alibaba.otter.canal.client.rocketmq;
|
||||||
|
|
||||||
import com.aliyun.openservices.apache.api.impl.authority.SessionCredentials;
|
|
||||||
import com.aliyun.openservices.apache.api.impl.rocketmq.ClientRPCHook;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
@@ -26,6 +24,8 @@ import com.alibaba.otter.canal.client.impl.SimpleCanalConnector;
|
|||||||
import com.alibaba.otter.canal.protocol.FlatMessage;
|
import com.alibaba.otter.canal.protocol.FlatMessage;
|
||||||
import com.alibaba.otter.canal.protocol.Message;
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
||||||
|
import com.aliyun.openservices.apache.api.impl.authority.SessionCredentials;
|
||||||
|
import com.aliyun.openservices.apache.api.impl.rocketmq.ClientRPCHook;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,24 +48,26 @@ public class RocketMQCanalConnector implements CanalMQConnector {
|
|||||||
private volatile boolean connected = false;
|
private volatile boolean connected = false;
|
||||||
private DefaultMQPushConsumer rocketMQConsumer;
|
private DefaultMQPushConsumer rocketMQConsumer;
|
||||||
private BlockingQueue<ConsumerBatchMessage> messageBlockingQueue;
|
private BlockingQueue<ConsumerBatchMessage> messageBlockingQueue;
|
||||||
|
private int batchSize = -1;
|
||||||
private long batchProcessTimeout = 60 * 1000;
|
private long batchProcessTimeout = 60 * 1000;
|
||||||
private boolean flatMessage;
|
private boolean flatMessage;
|
||||||
private volatile ConsumerBatchMessage lastGetBatchMessage = null;
|
private volatile ConsumerBatchMessage lastGetBatchMessage = null;
|
||||||
private String accessKey;
|
private String accessKey;
|
||||||
private String secretKey;
|
private String secretKey;
|
||||||
|
|
||||||
|
public RocketMQCanalConnector(String nameServer, String topic, String groupName, Integer batchSize,
|
||||||
public RocketMQCanalConnector(String nameServer, String topic, String groupName, boolean flatMessage){
|
boolean flatMessage){
|
||||||
this.nameServer = nameServer;
|
this.nameServer = nameServer;
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
this.groupName = groupName;
|
this.groupName = groupName;
|
||||||
this.flatMessage = flatMessage;
|
this.flatMessage = flatMessage;
|
||||||
this.messageBlockingQueue = new LinkedBlockingQueue<>(1024);
|
this.messageBlockingQueue = new LinkedBlockingQueue<>(1024);
|
||||||
|
this.batchSize = batchSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RocketMQCanalConnector(String nameServer, String topic, String groupName,
|
public RocketMQCanalConnector(String nameServer, String topic, String groupName, String accessKey,
|
||||||
String accessKey, String secretKey, boolean flatMessage){
|
String secretKey, Integer batchSize, boolean flatMessage){
|
||||||
this(nameServer, topic, groupName, flatMessage);
|
this(nameServer, topic, groupName, batchSize, flatMessage);
|
||||||
this.accessKey = accessKey;
|
this.accessKey = accessKey;
|
||||||
this.secretKey = secretKey;
|
this.secretKey = secretKey;
|
||||||
}
|
}
|
||||||
@@ -73,8 +75,7 @@ public class RocketMQCanalConnector implements CanalMQConnector {
|
|||||||
public void connect() throws CanalClientException {
|
public void connect() throws CanalClientException {
|
||||||
|
|
||||||
RPCHook rpcHook = null;
|
RPCHook rpcHook = null;
|
||||||
if(null != accessKey && accessKey.length() > 0
|
if (null != accessKey && accessKey.length() > 0 && null != secretKey && secretKey.length() > 0) {
|
||||||
&& null != secretKey && secretKey.length() > 0){
|
|
||||||
SessionCredentials sessionCredentials = new SessionCredentials();
|
SessionCredentials sessionCredentials = new SessionCredentials();
|
||||||
sessionCredentials.setAccessKey(accessKey);
|
sessionCredentials.setAccessKey(accessKey);
|
||||||
sessionCredentials.setSecretKey(secretKey);
|
sessionCredentials.setSecretKey(secretKey);
|
||||||
@@ -85,6 +86,9 @@ public class RocketMQCanalConnector implements CanalMQConnector {
|
|||||||
if (!StringUtils.isBlank(nameServer)) {
|
if (!StringUtils.isBlank(nameServer)) {
|
||||||
rocketMQConsumer.setNamesrvAddr(nameServer);
|
rocketMQConsumer.setNamesrvAddr(nameServer);
|
||||||
}
|
}
|
||||||
|
if (batchSize != -1) {
|
||||||
|
rocketMQConsumer.setConsumeMessageBatchMaxSize(batchSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws CanalClientException {
|
public void disconnect() throws CanalClientException {
|
||||||
|
|||||||
@@ -99,8 +99,13 @@ public class CanalKafkaClientExample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void process() {
|
private void process() {
|
||||||
while (!running)
|
while (!running) {
|
||||||
;
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
connector.connect();
|
connector.connect();
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
package com.alibaba.otter.canal.client.running.kafka;
|
package com.alibaba.otter.canal.client.running.kafka;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.client.kafka.KafkaOffsetCanalConnector;
|
import java.util.List;
|
||||||
import com.alibaba.otter.canal.client.kafka.protocol.KafkaMessage;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.kafka.common.errors.WakeupException;
|
import org.apache.kafka.common.errors.WakeupException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import java.util.List;
|
import com.alibaba.otter.canal.client.kafka.KafkaOffsetCanalConnector;
|
||||||
import java.util.concurrent.TimeUnit;
|
import com.alibaba.otter.canal.client.kafka.protocol.KafkaMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* KafkaOffsetCanalConnector 使用示例
|
* KafkaOffsetCanalConnector 使用示例
|
||||||
@@ -99,8 +100,13 @@ public class CanalKafkaOffsetClientExample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void process() {
|
private void process() {
|
||||||
while (!running)
|
while (!running) {
|
||||||
;
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
// 修改 AutoOffsetReset 的值,默认(earliest)
|
// 修改 AutoOffsetReset 的值,默认(earliest)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class CanalRocketMQClientExample extends AbstractRocektMQTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public CanalRocketMQClientExample(String nameServers, String topic, String groupId){
|
public CanalRocketMQClientExample(String nameServers, String topic, String groupId){
|
||||||
connector = new RocketMQCanalConnector(nameServers, topic, groupId, false);
|
connector = new RocketMQCanalConnector(nameServers, topic, groupId, 500, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -97,8 +97,13 @@ public class CanalRocketMQClientExample extends AbstractRocektMQTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void process() {
|
private void process() {
|
||||||
while (!running)
|
while (!running) {
|
||||||
;
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
connector.connect();
|
connector.connect();
|
||||||
|
|||||||
@@ -4,8 +4,16 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.*;
|
import java.sql.Connection;
|
||||||
import java.util.*;
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -39,7 +47,7 @@ public class ManagerRemoteConfigMonitor {
|
|||||||
|
|
||||||
private long scanIntervalInSecond = 5;
|
private long scanIntervalInSecond = 5;
|
||||||
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2,
|
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2,
|
||||||
new NamedThreadFactory("remote-canal-config-scan"));
|
new NamedThreadFactory("remote-canal-config-scan"));
|
||||||
|
|
||||||
public ManagerRemoteConfigMonitor(String jdbcUrl, String jdbcUsername, String jdbcPassword){
|
public ManagerRemoteConfigMonitor(String jdbcUrl, String jdbcUsername, String jdbcPassword){
|
||||||
this.jdbcUrl = jdbcUrl;
|
this.jdbcUrl = jdbcUrl;
|
||||||
@@ -75,8 +83,8 @@ public class ManagerRemoteConfigMonitor {
|
|||||||
overrideLocalCanalConfig(configItem.getContent());
|
overrideLocalCanalConfig(configItem.getContent());
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
properties.load(new ByteArrayInputStream(configItem.getContent().getBytes(StandardCharsets.UTF_8)));
|
properties.load(new ByteArrayInputStream(configItem.getContent().getBytes(StandardCharsets.UTF_8)));
|
||||||
scanIntervalInSecond = Integer
|
scanIntervalInSecond = Integer.valueOf(properties.getProperty(CanalConstants.CANAL_AUTO_SCAN_INTERVAL,
|
||||||
.valueOf(properties.getProperty(CanalConstants.CANAL_AUTO_SCAN_INTERVAL, "5"));
|
"5"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -223,8 +231,7 @@ public class ManagerRemoteConfigMonitor {
|
|||||||
Map<String, ConfigItem> changedInstanceConfigs = modifiedInstanceConfigs[0];
|
Map<String, ConfigItem> changedInstanceConfigs = modifiedInstanceConfigs[0];
|
||||||
if (changedInstanceConfigs != null) {
|
if (changedInstanceConfigs != null) {
|
||||||
for (ConfigItem configItem : changedInstanceConfigs.values()) {
|
for (ConfigItem configItem : changedInstanceConfigs.values()) {
|
||||||
try (FileWriter writer = new FileWriter(
|
try (FileWriter writer = new FileWriter(getConfPath() + configItem.getName() + "/instance.properties")) {
|
||||||
getConfPath() + configItem.getName() + "/instance.properties")) {
|
|
||||||
writer.write(configItem.getContent());
|
writer.write(configItem.getContent());
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -351,8 +358,8 @@ public class ManagerRemoteConfigMonitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Listener<Properties> {
|
public interface Listener<T> {
|
||||||
|
|
||||||
void onChange(Properties properties);
|
void onChange(T properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user