xc 1 year ago
commit
91aed7885e
33 changed files with 1715 additions and 0 deletions
  1. 33 0
      .gitignore
  2. BIN
      .mvn/wrapper/maven-wrapper.jar
  3. 2 0
      .mvn/wrapper/maven-wrapper.properties
  4. 38 0
      canal-client/pom.xml
  5. 16 0
      canal-client/src/main/java/cn/superdesk/canal/client/annotation/CanalTable.java
  6. 97 0
      canal-client/src/main/java/cn/superdesk/canal/client/client/AbstractCanalClient.java
  7. 14 0
      canal-client/src/main/java/cn/superdesk/canal/client/client/CanalClient.java
  8. 137 0
      canal-client/src/main/java/cn/superdesk/canal/client/client/KafkaCanalClient.java
  9. 16 0
      canal-client/src/main/java/cn/superdesk/canal/client/enums/TableNameEnum.java
  10. 30 0
      canal-client/src/main/java/cn/superdesk/canal/client/factory/AbstractModelFactory.java
  11. 17 0
      canal-client/src/main/java/cn/superdesk/canal/client/factory/IModelFactory.java
  12. 28 0
      canal-client/src/main/java/cn/superdesk/canal/client/factory/MapColumnModelFactory.java
  13. 56 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/AbstractFlatMessageHandler.java
  14. 17 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/CanalThreadUncaughtExceptionHandler.java
  15. 21 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/EntryHandler.java
  16. 44 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/MapRowDataHandlerImpl.java
  17. 9 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/MessageHandler.java
  18. 11 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/RowDataHandler.java
  19. 19 0
      canal-client/src/main/java/cn/superdesk/canal/client/handler/SyncFlatMessageHandlerImpl.java
  20. 57 0
      canal-client/src/main/java/cn/superdesk/canal/client/util/EntryUtil.java
  21. 20 0
      canal-client/src/main/java/cn/superdesk/canal/client/util/FieldUtil.java
  22. 47 0
      canal-client/src/main/java/cn/superdesk/canal/client/util/GenericUtil.java
  23. 74 0
      canal-client/src/main/java/cn/superdesk/canal/client/util/HandlerUtil.java
  24. 56 0
      canal-client/src/main/java/cn/superdesk/canal/client/util/StringConvertUtil.java
  25. 37 0
      canal2kafka-spring-boot-autoconfigure/pom.xml
  26. 61 0
      canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/CanalKafkaClientAutoConfiguration.java
  27. 101 0
      canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/CanalProperties.java
  28. 28 0
      canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/ThreadPoolAutoConfiguration.java
  29. 2 0
      canal2kafka-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
  30. 43 0
      canal2kafka-spring-boot-starter/pom.xml
  31. 308 0
      mvnw
  32. 205 0
      mvnw.cmd
  33. 71 0
      pom.xml

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+README.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/

BIN
.mvn/wrapper/maven-wrapper.jar


+ 2 - 0
.mvn/wrapper/maven-wrapper.properties

@@ -0,0 +1,2 @@
+distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
+wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar

+ 38 - 0
canal-client/pom.xml

@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>cn.superdesk</groupId>
+        <artifactId>canal2kafka</artifactId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>canal-client</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.alibaba.otter</groupId>
+            <artifactId>canal.client</artifactId>
+            <version>${canal.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate.javax.persistence</groupId>
+            <artifactId>hibernate-jpa-2.1-api</artifactId>
+            <version>1.0.2.Final</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.kafka</groupId>
+            <artifactId>kafka-clients</artifactId>
+            <version>${kafka.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 16 - 0
canal-client/src/main/java/cn/superdesk/canal/client/annotation/CanalTable.java

@@ -0,0 +1,16 @@
+package cn.superdesk.canal.client.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:36
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface CanalTable {
+    String value() default "";
+}

+ 97 - 0
canal-client/src/main/java/cn/superdesk/canal/client/client/AbstractCanalClient.java

@@ -0,0 +1,97 @@
+package cn.superdesk.canal.client.client;
+
+import cn.superdesk.canal.client.handler.MessageHandler;
+import com.alibaba.otter.canal.client.kafka.KafkaCanalConnector;
+import com.alibaba.otter.canal.protocol.Message;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:00
+ */
+@SuppressWarnings("unchecked,rawtypes")
+public class AbstractCanalClient implements CanalClient {
+    protected final Logger log = LoggerFactory.getLogger(KafkaCanalClient.class);
+
+    private KafkaCanalConnector connector;
+    private MessageHandler messageHandler;
+    protected static volatile boolean running = false;
+
+    private Thread thread = null;
+
+    protected Thread.UncaughtExceptionHandler handler = (t, e) -> log.error("parse events has an error", e);
+
+    protected Integer batchSize = 1;
+    protected Long timeout = 1L;
+    protected TimeUnit unit = TimeUnit.SECONDS;
+    protected String filter = StringUtils.EMPTY;
+
+
+    @Override
+    public void start() {
+        log.info("start canal client");
+        thread = new Thread(this::process);
+        thread.setName("canal-client-thread");
+        thread.setUncaughtExceptionHandler(handler);
+        running = true;
+        thread.start();
+    }
+
+    @Override
+    public void stop() {
+        if (!running) {
+            return;
+        }
+        running = false;
+        if (thread != null) {
+            try {
+                thread.join();
+            } catch (InterruptedException e) {
+                log.error("canal client stop error", e);
+            }
+        }
+    }
+
+    @Override
+    public void process() {
+        while (running) {
+            try {
+                connector.connect();
+                connector.subscribe(filter);
+                while (running) {
+                    Message message = connector.getWithoutAck(batchSize, timeout, unit);
+                    long batchId = message.getId();
+
+                    if (batchId != -1 && !message.getEntries().isEmpty()) {
+                        messageHandler.handleMessage(message);
+                    }
+                    connector.ack(batchId);
+                }
+            } catch (Exception e) {
+                log.error("canal client异常", e);
+            }
+        }
+        connector.unsubscribe();
+        connector.disconnect();
+    }
+
+    public KafkaCanalConnector getConnector() {
+        return connector;
+    }
+
+    public void setConnector(KafkaCanalConnector connector) {
+        this.connector = connector;
+    }
+
+    public MessageHandler<?> getMessageHandler() {
+        return messageHandler;
+    }
+
+    public void setMessageHandler(MessageHandler<?> messageHandler) {
+        this.messageHandler = messageHandler;
+    }
+}

+ 14 - 0
canal-client/src/main/java/cn/superdesk/canal/client/client/CanalClient.java

@@ -0,0 +1,14 @@
+package cn.superdesk.canal.client.client;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:44
+ */
+public interface CanalClient {
+    void start();
+
+    void stop();
+
+    void process();
+
+}

+ 137 - 0
canal-client/src/main/java/cn/superdesk/canal/client/client/KafkaCanalClient.java

@@ -0,0 +1,137 @@
+package cn.superdesk.canal.client.client;
+
+import cn.superdesk.canal.client.handler.MessageHandler;
+import com.alibaba.otter.canal.client.kafka.KafkaCanalConnector;
+import com.alibaba.otter.canal.protocol.FlatMessage;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:44
+ */
+@SuppressWarnings("unchecked,rawtypes")
+public class KafkaCanalClient extends AbstractCanalClient {
+
+    private final Logger log = LoggerFactory.getLogger(KafkaCanalClient.class);
+
+
+    @Override
+    public void process() {
+        KafkaCanalConnector connector = (KafkaCanalConnector) getConnector();
+        MessageHandler messageHandler = getMessageHandler();
+        while (running) {
+            try {
+                connector.connect();
+                connector.subscribe();
+                while (running) {
+                    try {
+                        List<FlatMessage> messages = connector.getFlatListWithoutAck(timeout, unit);
+                        log.info("kafka canal client获取到消息 : {}", messages);
+
+                        if (messages != null) {
+                            for (FlatMessage flatMessage : messages) {
+                                messageHandler.handleMessage(flatMessage);
+                            }
+                        }
+                        connector.ack();
+                    } catch (Exception e) {
+                        log.error("canal client 消费异常", e);
+                    }
+                }
+            } catch (Exception e) {
+                log.error("canal client 连接异常", e);
+            }
+        }
+    }
+
+    public static Builder builder() {
+        return Builder.builder();
+    }
+
+
+    public static class Builder {
+        private String filter = StringUtils.EMPTY;
+        private Integer batchSize = 1;
+        private Long timeout = 1L;
+        private TimeUnit unit = TimeUnit.SECONDS;
+        private String servers;
+        private String topic;
+        private Integer partition;
+        private String groupId;
+
+        private MessageHandler messageHandler;
+
+
+        private Builder() {
+        }
+
+        public static Builder builder() {
+            return new Builder();
+        }
+
+        public Builder servers(String servers) {
+            this.servers = servers;
+            return this;
+        }
+
+        public Builder topic(String topic) {
+            this.topic = topic;
+            return this;
+        }
+
+        public Builder partition(Integer partition) {
+            this.partition = partition;
+            return this;
+        }
+
+        public Builder groupId(String groupId) {
+            this.groupId = groupId;
+            return this;
+        }
+
+        public Builder filter(String filter) {
+            this.filter = filter;
+            return this;
+        }
+
+        public Builder batchSize(Integer batchSize) {
+            this.batchSize = batchSize;
+            return this;
+        }
+
+        public Builder timeout(Long timeout) {
+            this.timeout = timeout;
+            return this;
+        }
+
+        public Builder unit(TimeUnit unit) {
+            this.unit = unit;
+            return this;
+        }
+
+        public Builder messageHandler(MessageHandler messageHandler) {
+            this.messageHandler = messageHandler;
+            return this;
+        }
+
+        public KafkaCanalClient build() {
+            KafkaCanalConnector connector = new KafkaCanalConnector(servers, topic, partition, groupId, batchSize, true);
+            KafkaCanalClient client = new KafkaCanalClient();
+            client.setConnector(connector);
+            client.setMessageHandler(messageHandler);
+            client.filter = this.filter;
+            client.unit = this.unit;
+            client.batchSize = this.batchSize;
+            client.timeout = this.timeout;
+            return client;
+        }
+
+    }
+
+
+}

+ 16 - 0
canal-client/src/main/java/cn/superdesk/canal/client/enums/TableNameEnum.java

@@ -0,0 +1,16 @@
+package cn.superdesk.canal.client.enums;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:34
+ */
+public enum TableNameEnum {
+    ALL;
+
+    @Override
+    public String
+
+    toString() {
+        return super.toString();
+    }
+}

+ 30 - 0
canal-client/src/main/java/cn/superdesk/canal/client/factory/AbstractModelFactory.java

@@ -0,0 +1,30 @@
+package cn.superdesk.canal.client.factory;
+
+import cn.superdesk.canal.client.enums.TableNameEnum;
+import cn.superdesk.canal.client.handler.EntryHandler;
+import cn.superdesk.canal.client.util.GenericUtil;
+import cn.superdesk.canal.client.util.HandlerUtil;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:52
+ */
+public abstract class AbstractModelFactory<T> implements IModelFactory<T> {
+
+    @Override
+    public <R> R newInstance(EntryHandler entryHandler, T t) throws Exception {
+        String canalTableName = HandlerUtil.getCanalTableName(entryHandler);
+        if (TableNameEnum.ALL.name().toLowerCase().equals(canalTableName)) {
+            return (R) t;
+        }
+        Class<R> tableClass = GenericUtil.getTableClass(entryHandler);
+        if (tableClass != null) {
+            return newInstance(tableClass, t);
+        }
+        return null;
+    }
+
+
+    abstract <R> R newInstance(Class<R> c, T t) throws Exception;
+}
+

+ 17 - 0
canal-client/src/main/java/cn/superdesk/canal/client/factory/IModelFactory.java

@@ -0,0 +1,17 @@
+package cn.superdesk.canal.client.factory;
+
+import cn.superdesk.canal.client.handler.EntryHandler;
+
+import java.util.Set;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:51
+ */
+public interface IModelFactory<T> {
+    <R> R newInstance(EntryHandler entryHandler, T t) throws Exception;
+
+    default <R> R newInstance(EntryHandler entryHandler, T t, Set<String> updateColumn) throws Exception {
+        return null;
+    }
+}

+ 28 - 0
canal-client/src/main/java/cn/superdesk/canal/client/factory/MapColumnModelFactory.java

@@ -0,0 +1,28 @@
+package cn.superdesk.canal.client.factory;
+
+import cn.superdesk.canal.client.util.EntryUtil;
+import cn.superdesk.canal.client.util.FieldUtil;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Map;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:52
+ */
+public class MapColumnModelFactory extends AbstractModelFactory<Map<String, String>> {
+
+
+    @Override
+    <R> R newInstance(Class<R> c, Map<String, String> valueMap) throws Exception {
+        R                   object      = c.newInstance();
+        Map<String, String> columnNames = EntryUtil.getFieldName(object.getClass());
+        for (Map.Entry<String, String> entry : valueMap.entrySet()) {
+            String fieldName = columnNames.get(entry.getKey());
+            if (StringUtils.isNotEmpty(fieldName)) {
+                FieldUtil.setFieldValue(object, fieldName, entry.getValue());
+            }
+        }
+        return object;
+    }
+}

+ 56 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/AbstractFlatMessageHandler.java

@@ -0,0 +1,56 @@
+package cn.superdesk.canal.client.handler;
+
+import cn.superdesk.canal.client.util.HandlerUtil;
+import com.alibaba.otter.canal.protocol.CanalEntry;
+import com.alibaba.otter.canal.protocol.FlatMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public abstract class AbstractFlatMessageHandler implements MessageHandler<FlatMessage> {
+
+
+	private final Map<String, EntryHandler<?>> tableHandlerMap;
+
+
+	private final RowDataHandler<List<Map<String, String>>> rowDataHandler;
+
+
+	private final Logger logger = LoggerFactory.getLogger(AbstractFlatMessageHandler.class);
+
+
+	public AbstractFlatMessageHandler(List<? extends EntryHandler<?>> entryHandlers, RowDataHandler<List<Map<String, String>>> rowDataHandler) {
+		this.tableHandlerMap = HandlerUtil.getTableHandlerMap(entryHandlers);
+		this.rowDataHandler = rowDataHandler;
+	}
+
+	@Override
+	public void handleMessage(FlatMessage flatMessage) {
+		List<Map<String, String>> data = flatMessage.getData();
+		if (data != null && !data.isEmpty()) {
+			for (int i = 0; i < data.size(); i++) {
+				CanalEntry.EventType      eventType = CanalEntry.EventType.valueOf(flatMessage.getType());
+				List<Map<String, String>> maps;
+				if (eventType.equals(CanalEntry.EventType.UPDATE)) {
+					Map<String, String> map    = data.get(i);
+					Map<String, String> oldMap = flatMessage.getOld().get(i);
+					maps = Stream.of(map, oldMap).collect(Collectors.toList());
+				} else {
+					maps = Stream.of(data.get(i)).collect(Collectors.toList());
+				}
+				try {
+					EntryHandler<?> entryHandler = HandlerUtil.getEntryHandler(tableHandlerMap, flatMessage.getTable());
+					if (entryHandler != null) {
+						rowDataHandler.handlerRowData(maps, entryHandler, eventType);
+					}
+				} catch (Exception e) {
+					throw new RuntimeException("parse event has an error , data:" + data.toString(), e);
+				}
+			}
+		}
+	}
+}

+ 17 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/CanalThreadUncaughtExceptionHandler.java

@@ -0,0 +1,17 @@
+package cn.superdesk.canal.client.handler;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:40
+ */
+
+public class CanalThreadUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{
+    private final Logger log = LoggerFactory.getLogger(CanalThreadUncaughtExceptionHandler.class);
+    @Override
+    public void uncaughtException(Thread t, Throwable e) {
+        log.error("thread " + t.getName() + " throws exception", e);
+    }
+}

+ 21 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/EntryHandler.java

@@ -0,0 +1,21 @@
+package cn.superdesk.canal.client.handler;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:37
+ */
+public interface EntryHandler<T> {
+    default void insert(T t) {
+
+    }
+
+
+    default void update(T before, T after) {
+
+    }
+
+
+    default void delete(T t) {
+
+    }
+}

+ 44 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/MapRowDataHandlerImpl.java

@@ -0,0 +1,44 @@
+package cn.superdesk.canal.client.handler;
+
+import cn.superdesk.canal.client.factory.IModelFactory;
+import com.alibaba.otter.canal.protocol.CanalEntry;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:51
+ */
+public class MapRowDataHandlerImpl implements RowDataHandler<List<Map<String, String>>> {
+
+    private IModelFactory<Map<String, String>> modelFactory;
+
+    public MapRowDataHandlerImpl(IModelFactory<Map<String, String>> modelFactory) {
+        this.modelFactory = modelFactory;
+    }
+
+    @Override
+    public <R> void handlerRowData(List<Map<String, String>> list, EntryHandler<R> entryHandler, CanalEntry.EventType eventType) throws Exception {
+
+        if (entryHandler != null) {
+            switch (eventType) {
+                case INSERT:
+                    R entry = modelFactory.newInstance(entryHandler, list.get(0));
+                    entryHandler.insert(entry);
+                    break;
+                case UPDATE:
+                    R before = modelFactory.newInstance(entryHandler, list.get(1));
+                    R after = modelFactory.newInstance(entryHandler, list.get(0));
+                    entryHandler.update(before, after);
+                    break;
+                case DELETE:
+                    R o = modelFactory.newInstance(entryHandler, list.get(0));
+                    entryHandler.delete(o);
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}

+ 9 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/MessageHandler.java

@@ -0,0 +1,9 @@
+package cn.superdesk.canal.client.handler;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:49
+ */
+public interface MessageHandler<T> {
+    void handleMessage(T t);
+}

+ 11 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/RowDataHandler.java

@@ -0,0 +1,11 @@
+package cn.superdesk.canal.client.handler;
+
+import com.alibaba.otter.canal.protocol.CanalEntry;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:32
+ */
+public interface RowDataHandler<T> {
+    <R> void handlerRowData(T t, EntryHandler<R> entryHandler, CanalEntry.EventType eventType) throws Exception;
+}

+ 19 - 0
canal-client/src/main/java/cn/superdesk/canal/client/handler/SyncFlatMessageHandlerImpl.java

@@ -0,0 +1,19 @@
+package cn.superdesk.canal.client.handler;
+
+import com.alibaba.otter.canal.protocol.FlatMessage;
+
+import java.util.List;
+import java.util.Map;
+
+public class SyncFlatMessageHandlerImpl extends AbstractFlatMessageHandler {
+
+
+	public SyncFlatMessageHandlerImpl(List<? extends EntryHandler<?>> entryHandlers, RowDataHandler<List<Map<String, String>>> rowDataHandler) {
+		super(entryHandlers, rowDataHandler);
+	}
+
+	@Override
+	public void handleMessage(FlatMessage flatMessage) {
+		super.handleMessage(flatMessage);
+	}
+}

+ 57 - 0
canal-client/src/main/java/cn/superdesk/canal/client/util/EntryUtil.java

@@ -0,0 +1,57 @@
+package cn.superdesk.canal.client.util;
+
+import org.apache.commons.lang3.reflect.FieldUtils;
+
+import javax.persistence.Column;
+import javax.persistence.Transient;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:53
+ */
+public class EntryUtil {
+
+    private static Map<Class, Map<String, String>> cache = new ConcurrentHashMap<>();
+
+
+    /**
+     * 获取字段名称和实体属性的对应关系
+     *
+     * @param c class
+     * @return map
+     */
+    public static Map<String, String> getFieldName(Class c) {
+        Map<String, String> map = cache.get(c);
+        if (map == null) {
+            List<Field> fields = FieldUtils.getAllFieldsList(c);
+            //如果实体类中存在column 注解,则使用column注解的名称为字段名
+            map = fields.stream().filter(EntryUtil::notTransient)
+                    .filter(field -> !Modifier.isStatic(field.getModifiers()))
+                    .collect(Collectors.toMap(EntryUtil::getColumnName, Field::getName));
+            cache.putIfAbsent(c, map);
+        }
+        return map;
+    }
+
+
+    private static String getColumnName(Field field) {
+        Column annotation = field.getAnnotation(Column.class);
+        if (annotation != null) {
+            return annotation.name();
+        } else {
+            return field.getName();
+        }
+    }
+
+
+    private static boolean notTransient(Field field) {
+        Transient annotation = field.getAnnotation(Transient.class);
+        return annotation == null;
+    }
+}

+ 20 - 0
canal-client/src/main/java/cn/superdesk/canal/client/util/FieldUtil.java

@@ -0,0 +1,20 @@
+package cn.superdesk.canal.client.util;
+
+import java.lang.reflect.Field;
+
+public class FieldUtil {
+
+
+	public static void setFieldValue(Object object, String fieldName, String value) throws NoSuchFieldException, IllegalAccessException {
+		Field field;
+		try {
+			field = object.getClass().getDeclaredField(fieldName);
+		} catch (NoSuchFieldException e) {
+			field = object.getClass().getSuperclass().getDeclaredField(fieldName);
+		}
+		field.setAccessible(true);
+		Class<?> type   = field.getType();
+		Object   result = StringConvertUtil.convertType(type, value);
+		field.set(object, result);
+	}
+}

+ 47 - 0
canal-client/src/main/java/cn/superdesk/canal/client/util/GenericUtil.java

@@ -0,0 +1,47 @@
+package cn.superdesk.canal.client.util;
+
+import cn.superdesk.canal.client.handler.EntryHandler;
+
+import javax.persistence.Table;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:35
+ */
+public class GenericUtil {
+    private static Map<Class<? extends EntryHandler>, Class> cache = new ConcurrentHashMap<>();
+
+    @SuppressWarnings("rawtypes")
+    static String getTableGenericProperties(EntryHandler entryHandler) {
+        Class<?> tableClass = getTableClass(entryHandler);
+        if (tableClass != null) {
+            Table annotation = tableClass.getAnnotation(Table.class);
+            if (annotation != null) {
+                return annotation.name();
+            }
+        }
+        return null;
+    }
+
+    @SuppressWarnings("unchecked,rawtypes")
+    public static <T> Class<T> getTableClass(EntryHandler object) {
+        Class<? extends EntryHandler> handlerClass = object.getClass();
+        Class                         tableClass   = cache.get(handlerClass);
+        if (tableClass == null) {
+            Type[] interfacesTypes = handlerClass.getGenericInterfaces();
+            for (Type t : interfacesTypes) {
+                Class c = (Class) ((ParameterizedType) t).getRawType();
+                if (c.equals(EntryHandler.class)) {
+                    tableClass = (Class<T>) ((ParameterizedType) t).getActualTypeArguments()[0];
+                    cache.putIfAbsent(handlerClass, tableClass);
+                    return tableClass;
+                }
+            }
+        }
+        return tableClass;
+    }
+}

+ 74 - 0
canal-client/src/main/java/cn/superdesk/canal/client/util/HandlerUtil.java

@@ -0,0 +1,74 @@
+package cn.superdesk.canal.client.util;
+
+import cn.superdesk.canal.client.annotation.CanalTable;
+import cn.superdesk.canal.client.enums.TableNameEnum;
+import cn.superdesk.canal.client.handler.EntryHandler;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author xc
+ * @since 2023/12/21 16:33
+ */
+@SuppressWarnings("rawtypes")
+public class HandlerUtil {
+    public static EntryHandler getEntryHandler(List<? extends EntryHandler> entryHandlers, String tableName) {
+        EntryHandler globalHandler = null;
+        for (EntryHandler handler : entryHandlers) {
+            String canalTableName = getCanalTableName(handler);
+            if (TableNameEnum.ALL.name().toLowerCase().equals(canalTableName)) {
+                globalHandler = handler;
+                continue;
+            }
+            if (tableName.equals(canalTableName)) {
+                return handler;
+            }
+            String name = GenericUtil.getTableGenericProperties(handler);
+            if (name != null) {
+                if (name.equals(tableName)) {
+                    return handler;
+                }
+            }
+        }
+        return globalHandler;
+    }
+
+
+    public static Map<String, EntryHandler<?>> getTableHandlerMap(List<? extends EntryHandler<?>> entryHandlers) {
+        Map<String, EntryHandler<?>> map = new ConcurrentHashMap<>();
+        if (entryHandlers != null && !entryHandlers.isEmpty()) {
+            for (EntryHandler handler : entryHandlers) {
+                String canalTableName = getCanalTableName(handler);
+                if (canalTableName != null) {
+                    map.putIfAbsent(canalTableName.toLowerCase(), handler);
+                } else {
+                    String name = GenericUtil.getTableGenericProperties(handler);
+                    if (name != null) {
+                        map.putIfAbsent(name.toLowerCase(), handler);
+                    }
+                }
+            }
+        }
+        return map;
+    }
+
+
+    public static EntryHandler getEntryHandler(Map<String, EntryHandler<?>> map, String tableName) {
+        EntryHandler entryHandler = map.get(tableName);
+        if (entryHandler == null) {
+            return map.get(TableNameEnum.ALL.name().toLowerCase());
+        }
+        return entryHandler;
+    }
+
+
+    public static String getCanalTableName(EntryHandler entryHandler) {
+        CanalTable canalTable = entryHandler.getClass().getAnnotation(CanalTable.class);
+        if (canalTable != null) {
+            return canalTable.value();
+        }
+        return null;
+    }
+}

+ 56 - 0
canal-client/src/main/java/cn/superdesk/canal/client/util/StringConvertUtil.java

@@ -0,0 +1,56 @@
+package cn.superdesk.canal.client.util;
+
+import java.math.BigDecimal;
+import java.text.ParseException;
+import java.util.Date;
+
+
+public class StringConvertUtil {
+
+
+    private static final String[] PARSE_PATTERNS = {
+            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
+            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
+            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
+
+
+    static Object convertType(Class<?> type, String columnValue) {
+        if (columnValue == null) {
+            return null;
+        } else if (type.equals(Integer.class)) {
+            return Integer.parseInt(columnValue);
+        } else if (type.equals(Long.class)) {
+            return Long.parseLong(columnValue);
+        } else if (type.equals(Boolean.class)) {
+            return convertToBoolean(columnValue);
+        } else if (type.equals(BigDecimal.class)) {
+            return new BigDecimal(columnValue);
+        } else if (type.equals(Double.class)) {
+            return Double.parseDouble(columnValue);
+        } else if (type.equals(Float.class)) {
+            return Float.parseFloat(columnValue);
+        } else if (type.equals(Date.class)) {
+            return parseDate(columnValue);
+        } else if (type.equals(java.sql.Date.class)) {
+            return parseDate(columnValue);
+        } else {
+            return columnValue;
+        }
+    }
+
+    private static Date parseDate(String str) {
+        if (str == null) {
+            return null;
+        }
+        try {
+            return org.apache.commons.lang.time.DateUtils.parseDate(str, PARSE_PATTERNS);
+        } catch (ParseException e) {
+            return null;
+        }
+    }
+
+    private static boolean convertToBoolean(String value) {
+        return "1".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
+    }
+
+}

+ 37 - 0
canal2kafka-spring-boot-autoconfigure/pom.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>cn.superdesk</groupId>
+        <artifactId>canal2kafka</artifactId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>canal2kafka-spring-boot-autoconfigure</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>cn.superdesk</groupId>
+            <artifactId>canal-client</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 61 - 0
canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/CanalKafkaClientAutoConfiguration.java

@@ -0,0 +1,61 @@
+package cn.superdesk.canal2kafka.spring.boot.autoconfigure;
+
+import cn.superdesk.canal.client.client.KafkaCanalClient;
+import cn.superdesk.canal.client.factory.MapColumnModelFactory;
+import cn.superdesk.canal.client.handler.*;
+import com.alibaba.otter.canal.protocol.FlatMessage;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * canal kafka客户端自动配置类
+ * @author xc
+ * @since 2023/12/21 15:19
+ */
+@EnableConfigurationProperties(CanalProperties.class)
+@Configuration
+@ConditionalOnBean(value = {EntryHandler.class})
+@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "kafka")
+@Import(ThreadPoolAutoConfiguration.class)
+public class CanalKafkaClientAutoConfiguration {
+    private final CanalProperties canalProperties;
+
+
+    public CanalKafkaClientAutoConfiguration(CanalProperties canalProperties) {
+        this.canalProperties = canalProperties;
+    }
+
+    @Bean
+    public RowDataHandler<List<Map<String, String>>> rowDataHandler() {
+        return new MapRowDataHandlerImpl(new MapColumnModelFactory());
+    }
+
+    @Bean
+    public MessageHandler<FlatMessage> messageHandler(RowDataHandler<List<Map<String, String>>> rowDataHandler, List<EntryHandler<?>> entryHandlers) {
+        return new SyncFlatMessageHandlerImpl(entryHandlers, rowDataHandler);
+    }
+
+
+
+    @Bean(initMethod = "start", destroyMethod = "stop")
+    public KafkaCanalClient canalKafkaClient(MessageHandler<FlatMessage> messageHandler) {
+        return KafkaCanalClient.builder()
+                .servers(canalProperties.getServers())
+                .groupId(canalProperties.getGroupId())
+                .topic(canalProperties.getDestination())
+                .messageHandler(messageHandler)
+                .filter(canalProperties.getFilter())
+                .batchSize(canalProperties.getBatchSize())
+                .timeout(canalProperties.getTimeout())
+                .unit(canalProperties.getUnit())
+                .build();
+    }
+
+}

+ 101 - 0
canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/CanalProperties.java

@@ -0,0 +1,101 @@
+package cn.superdesk.canal2kafka.spring.boot.autoconfigure;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:20
+ */
+@ConfigurationProperties(prefix = "canal")
+public class CanalProperties {
+    public static final String CANAL_PREFIX = "canal";
+    public static final String CANAL_ASYNC  = CANAL_PREFIX + "." + "async";
+    public static final String CANAL_MODE   = CANAL_PREFIX + "." + "mode";
+
+    private String mode;
+    private String servers;
+    private String destination;
+    private String filter = StringUtils.EMPTY;
+    private Integer batchSize = 1;
+
+    private Long timeout = 1L;
+
+    private TimeUnit unit = TimeUnit.SECONDS;
+    private Integer partition;
+    private String groupId;
+
+    public String getMode() {
+        return mode;
+    }
+
+    public void setMode(String mode) {
+        this.mode = mode;
+    }
+
+    public String getServers() {
+        return servers;
+    }
+
+    public void setServers(String servers) {
+        this.servers = servers;
+    }
+
+    public String getDestination() {
+        return destination;
+    }
+
+    public void setDestination(String destination) {
+        this.destination = destination;
+    }
+
+    public String getFilter() {
+        return filter;
+    }
+
+    public void setFilter(String filter) {
+        this.filter = filter;
+    }
+
+    public Integer getBatchSize() {
+        return batchSize;
+    }
+
+    public void setBatchSize(Integer batchSize) {
+        this.batchSize = batchSize;
+    }
+
+    public Long getTimeout() {
+        return timeout;
+    }
+
+    public void setTimeout(Long timeout) {
+        this.timeout = timeout;
+    }
+
+    public TimeUnit getUnit() {
+        return unit;
+    }
+
+    public void setUnit(TimeUnit unit) {
+        this.unit = unit;
+    }
+
+    public Integer getPartition() {
+        return partition;
+    }
+
+    public void setPartition(Integer partition) {
+        this.partition = partition;
+    }
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+}

+ 28 - 0
canal2kafka-spring-boot-autoconfigure/src/main/java/cn/superdesk/canal2kafka/spring/boot/autoconfigure/ThreadPoolAutoConfiguration.java

@@ -0,0 +1,28 @@
+package cn.superdesk.canal2kafka.spring.boot.autoconfigure;
+
+import cn.superdesk.canal.client.handler.CanalThreadUncaughtExceptionHandler;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * @author xc
+ * @since 2023/12/21 15:38
+ */
+@Configuration
+@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true", matchIfMissing = true)
+public class ThreadPoolAutoConfiguration {
+
+    @Bean(destroyMethod = "shutdown")
+    public ExecutorService executorService() {
+        BasicThreadFactory factory = new BasicThreadFactory.Builder()
+                .namingPattern("canal-execute-thread-%d")
+                .uncaughtExceptionHandler(new CanalThreadUncaughtExceptionHandler())
+                .build();
+        return Executors.newFixedThreadPool(10, factory);
+    }
+}

+ 2 - 0
canal2kafka-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+  cn.superdesk.canal2kafka.spring.boot.autoconfigure.CanalKafkaClientAutoConfiguration

+ 43 - 0
canal2kafka-spring-boot-starter/pom.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>cn.superdesk</groupId>
+        <artifactId>canal2kafka</artifactId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>canal2kafka-spring-boot-starter</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>cn.superdesk</groupId>
+            <artifactId>canal2kafka-spring-boot-autoconfigure</artifactId>
+            <version>${parent.version}</version>
+        </dependency>
+    </dependencies>
+
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.5.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 308 - 0
mvnw

@@ -0,0 +1,308 @@
+#!/bin/sh
+# ----------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# ----------------------------------------------------------------------------
+
+# ----------------------------------------------------------------------------
+# Apache Maven Wrapper startup batch script, version 3.2.0
+#
+# Required ENV vars:
+# ------------------
+#   JAVA_HOME - location of a JDK home dir
+#
+# Optional ENV vars
+# -----------------
+#   MAVEN_OPTS - parameters passed to the Java VM when running Maven
+#     e.g. to debug Maven itself, use
+#       set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
+#   MAVEN_SKIP_RC - flag to disable loading of mavenrc files
+# ----------------------------------------------------------------------------
+
+if [ -z "$MAVEN_SKIP_RC" ] ; then
+
+  if [ -f /usr/local/etc/mavenrc ] ; then
+    . /usr/local/etc/mavenrc
+  fi
+
+  if [ -f /etc/mavenrc ] ; then
+    . /etc/mavenrc
+  fi
+
+  if [ -f "$HOME/.mavenrc" ] ; then
+    . "$HOME/.mavenrc"
+  fi
+
+fi
+
+# OS specific support.  $var _must_ be set to either true or false.
+cygwin=false;
+darwin=false;
+mingw=false
+case "$(uname)" in
+  CYGWIN*) cygwin=true ;;
+  MINGW*) mingw=true;;
+  Darwin*) darwin=true
+    # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
+    # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
+    if [ -z "$JAVA_HOME" ]; then
+      if [ -x "/usr/libexec/java_home" ]; then
+        JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME
+      else
+        JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
+      fi
+    fi
+    ;;
+esac
+
+if [ -z "$JAVA_HOME" ] ; then
+  if [ -r /etc/gentoo-release ] ; then
+    JAVA_HOME=$(java-config --jre-home)
+  fi
+fi
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched
+if $cygwin ; then
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=$(cygpath --path --unix "$CLASSPATH")
+fi
+
+# For Mingw, ensure paths are in UNIX format before anything is touched
+if $mingw ; then
+  [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] &&
+    JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)"
+fi
+
+if [ -z "$JAVA_HOME" ]; then
+  javaExecutable="$(which javac)"
+  if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then
+    # readlink(1) is not available as standard on Solaris 10.
+    readLink=$(which readlink)
+    if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then
+      if $darwin ; then
+        javaHome="$(dirname "\"$javaExecutable\"")"
+        javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac"
+      else
+        javaExecutable="$(readlink -f "\"$javaExecutable\"")"
+      fi
+      javaHome="$(dirname "\"$javaExecutable\"")"
+      javaHome=$(expr "$javaHome" : '\(.*\)/bin')
+      JAVA_HOME="$javaHome"
+      export JAVA_HOME
+    fi
+  fi
+fi
+
+if [ -z "$JAVACMD" ] ; then
+  if [ -n "$JAVA_HOME"  ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+      # IBM's JDK on AIX uses strange locations for the executables
+      JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+      JAVACMD="$JAVA_HOME/bin/java"
+    fi
+  else
+    JAVACMD="$(\unset -f command 2>/dev/null; \command -v java)"
+  fi
+fi
+
+if [ ! -x "$JAVACMD" ] ; then
+  echo "Error: JAVA_HOME is not defined correctly." >&2
+  echo "  We cannot execute $JAVACMD" >&2
+  exit 1
+fi
+
+if [ -z "$JAVA_HOME" ] ; then
+  echo "Warning: JAVA_HOME environment variable is not set."
+fi
+
+# traverses directory structure from process work directory to filesystem root
+# first directory with .mvn subdirectory is considered project base directory
+find_maven_basedir() {
+  if [ -z "$1" ]
+  then
+    echo "Path not specified to find_maven_basedir"
+    return 1
+  fi
+
+  basedir="$1"
+  wdir="$1"
+  while [ "$wdir" != '/' ] ; do
+    if [ -d "$wdir"/.mvn ] ; then
+      basedir=$wdir
+      break
+    fi
+    # workaround for JBEAP-8937 (on Solaris 10/Sparc)
+    if [ -d "${wdir}" ]; then
+      wdir=$(cd "$wdir/.." || exit 1; pwd)
+    fi
+    # end of workaround
+  done
+  printf '%s' "$(cd "$basedir" || exit 1; pwd)"
+}
+
+# concatenates all lines of a file
+concat_lines() {
+  if [ -f "$1" ]; then
+    # Remove \r in case we run on Windows within Git Bash
+    # and check out the repository with auto CRLF management
+    # enabled. Otherwise, we may read lines that are delimited with
+    # \r\n and produce $'-Xarg\r' rather than -Xarg due to word
+    # splitting rules.
+    tr -s '\r\n' ' ' < "$1"
+  fi
+}
+
+log() {
+  if [ "$MVNW_VERBOSE" = true ]; then
+    printf '%s\n' "$1"
+  fi
+}
+
+BASE_DIR=$(find_maven_basedir "$(dirname "$0")")
+if [ -z "$BASE_DIR" ]; then
+  exit 1;
+fi
+
+MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR
+log "$MAVEN_PROJECTBASEDIR"
+
+##########################################################################################
+# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
+# This allows using the maven wrapper in projects that prohibit checking in binary data.
+##########################################################################################
+wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar"
+if [ -r "$wrapperJarPath" ]; then
+    log "Found $wrapperJarPath"
+else
+    log "Couldn't find $wrapperJarPath, downloading it ..."
+
+    if [ -n "$MVNW_REPOURL" ]; then
+      wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar"
+    else
+      wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar"
+    fi
+    while IFS="=" read -r key value; do
+      # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' )
+      safeValue=$(echo "$value" | tr -d '\r')
+      case "$key" in (wrapperUrl) wrapperUrl="$safeValue"; break ;;
+      esac
+    done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties"
+    log "Downloading from: $wrapperUrl"
+
+    if $cygwin; then
+      wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath")
+    fi
+
+    if command -v wget > /dev/null; then
+        log "Found wget ... using wget"
+        [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet"
+        if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
+            wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
+        else
+            wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
+        fi
+    elif command -v curl > /dev/null; then
+        log "Found curl ... using curl"
+        [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent"
+        if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
+            curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath"
+        else
+            curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath"
+        fi
+    else
+        log "Falling back to using Java to download"
+        javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java"
+        javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class"
+        # For Cygwin, switch paths to Windows format before running javac
+        if $cygwin; then
+          javaSource=$(cygpath --path --windows "$javaSource")
+          javaClass=$(cygpath --path --windows "$javaClass")
+        fi
+        if [ -e "$javaSource" ]; then
+            if [ ! -e "$javaClass" ]; then
+                log " - Compiling MavenWrapperDownloader.java ..."
+                ("$JAVA_HOME/bin/javac" "$javaSource")
+            fi
+            if [ -e "$javaClass" ]; then
+                log " - Running MavenWrapperDownloader.java ..."
+                ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath"
+            fi
+        fi
+    fi
+fi
+##########################################################################################
+# End of extension
+##########################################################################################
+
+# If specified, validate the SHA-256 sum of the Maven wrapper jar file
+wrapperSha256Sum=""
+while IFS="=" read -r key value; do
+  case "$key" in (wrapperSha256Sum) wrapperSha256Sum=$value; break ;;
+  esac
+done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties"
+if [ -n "$wrapperSha256Sum" ]; then
+  wrapperSha256Result=false
+  if command -v sha256sum > /dev/null; then
+    if echo "$wrapperSha256Sum  $wrapperJarPath" | sha256sum -c > /dev/null 2>&1; then
+      wrapperSha256Result=true
+    fi
+  elif command -v shasum > /dev/null; then
+    if echo "$wrapperSha256Sum  $wrapperJarPath" | shasum -a 256 -c > /dev/null 2>&1; then
+      wrapperSha256Result=true
+    fi
+  else
+    echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available."
+    echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties."
+    exit 1
+  fi
+  if [ $wrapperSha256Result = false ]; then
+    echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2
+    echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2
+    echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2
+    exit 1
+  fi
+fi
+
+MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin; then
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME")
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=$(cygpath --path --windows "$CLASSPATH")
+  [ -n "$MAVEN_PROJECTBASEDIR" ] &&
+    MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR")
+fi
+
+# Provide a "standardized" way to retrieve the CLI args that will
+# work with both Windows and non-Windows executions.
+MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*"
+export MAVEN_CMD_LINE_ARGS
+
+WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
+
+# shellcheck disable=SC2086 # safe args
+exec "$JAVACMD" \
+  $MAVEN_OPTS \
+  $MAVEN_DEBUG_OPTS \
+  -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
+  "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
+  ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

+ 205 - 0
mvnw.cmd

@@ -0,0 +1,205 @@
+@REM ----------------------------------------------------------------------------
+@REM Licensed to the Apache Software Foundation (ASF) under one
+@REM or more contributor license agreements.  See the NOTICE file
+@REM distributed with this work for additional information
+@REM regarding copyright ownership.  The ASF licenses this file
+@REM to you under the Apache License, Version 2.0 (the
+@REM "License"); you may not use this file except in compliance
+@REM with the License.  You may obtain a copy of the License at
+@REM
+@REM    https://www.apache.org/licenses/LICENSE-2.0
+@REM
+@REM Unless required by applicable law or agreed to in writing,
+@REM software distributed under the License is distributed on an
+@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+@REM KIND, either express or implied.  See the License for the
+@REM specific language governing permissions and limitations
+@REM under the License.
+@REM ----------------------------------------------------------------------------
+
+@REM ----------------------------------------------------------------------------
+@REM Apache Maven Wrapper startup batch script, version 3.2.0
+@REM
+@REM Required ENV vars:
+@REM JAVA_HOME - location of a JDK home dir
+@REM
+@REM Optional ENV vars
+@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
+@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
+@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
+@REM     e.g. to debug Maven itself, use
+@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
+@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
+@REM ----------------------------------------------------------------------------
+
+@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
+@echo off
+@REM set title of command window
+title %0
+@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
+@if "%MAVEN_BATCH_ECHO%" == "on"  echo %MAVEN_BATCH_ECHO%
+
+@REM set %HOME% to equivalent of $HOME
+if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
+
+@REM Execute a user defined script before this one
+if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
+@REM check for pre script, once with legacy .bat ending and once with .cmd ending
+if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
+if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
+:skipRcPre
+
+@setlocal
+
+set ERROR_CODE=0
+
+@REM To isolate internal variables from possible post scripts, we use another setlocal
+@setlocal
+
+@REM ==== START VALIDATION ====
+if not "%JAVA_HOME%" == "" goto OkJHome
+
+echo.
+echo Error: JAVA_HOME not found in your environment. >&2
+echo Please set the JAVA_HOME variable in your environment to match the >&2
+echo location of your Java installation. >&2
+echo.
+goto error
+
+:OkJHome
+if exist "%JAVA_HOME%\bin\java.exe" goto init
+
+echo.
+echo Error: JAVA_HOME is set to an invalid directory. >&2
+echo JAVA_HOME = "%JAVA_HOME%" >&2
+echo Please set the JAVA_HOME variable in your environment to match the >&2
+echo location of your Java installation. >&2
+echo.
+goto error
+
+@REM ==== END VALIDATION ====
+
+:init
+
+@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
+@REM Fallback to current working directory if not found.
+
+set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
+IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
+
+set EXEC_DIR=%CD%
+set WDIR=%EXEC_DIR%
+:findBaseDir
+IF EXIST "%WDIR%"\.mvn goto baseDirFound
+cd ..
+IF "%WDIR%"=="%CD%" goto baseDirNotFound
+set WDIR=%CD%
+goto findBaseDir
+
+:baseDirFound
+set MAVEN_PROJECTBASEDIR=%WDIR%
+cd "%EXEC_DIR%"
+goto endDetectBaseDir
+
+:baseDirNotFound
+set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
+cd "%EXEC_DIR%"
+
+:endDetectBaseDir
+
+IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
+
+@setlocal EnableExtensions EnableDelayedExpansion
+for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
+@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
+
+:endReadAdditionalConfig
+
+SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
+set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
+set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
+
+set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar"
+
+FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
+    IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B
+)
+
+@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
+@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
+if exist %WRAPPER_JAR% (
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Found %WRAPPER_JAR%
+    )
+) else (
+    if not "%MVNW_REPOURL%" == "" (
+        SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar"
+    )
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Couldn't find %WRAPPER_JAR%, downloading it ...
+        echo Downloading from: %WRAPPER_URL%
+    )
+
+    powershell -Command "&{"^
+		"$webclient = new-object System.Net.WebClient;"^
+		"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
+		"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
+		"}"^
+		"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^
+		"}"
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Finished downloading %WRAPPER_JAR%
+    )
+)
+@REM End of extension
+
+@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file
+SET WRAPPER_SHA_256_SUM=""
+FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
+    IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B
+)
+IF NOT %WRAPPER_SHA_256_SUM%=="" (
+    powershell -Command "&{"^
+       "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^
+       "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^
+       "  Write-Output 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^
+       "  Write-Output 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^
+       "  Write-Output 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^
+       "  exit 1;"^
+       "}"^
+       "}"
+    if ERRORLEVEL 1 goto error
+)
+
+@REM Provide a "standardized" way to retrieve the CLI args that will
+@REM work with both Windows and non-Windows executions.
+set MAVEN_CMD_LINE_ARGS=%*
+
+%MAVEN_JAVA_EXE% ^
+  %JVM_CONFIG_MAVEN_PROPS% ^
+  %MAVEN_OPTS% ^
+  %MAVEN_DEBUG_OPTS% ^
+  -classpath %WRAPPER_JAR% ^
+  "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
+  %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
+if ERRORLEVEL 1 goto error
+goto end
+
+:error
+set ERROR_CODE=1
+
+:end
+@endlocal & set ERROR_CODE=%ERROR_CODE%
+
+if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
+@REM check for post script, once with legacy .bat ending and once with .cmd ending
+if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
+if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
+:skipRcPost
+
+@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
+if "%MAVEN_BATCH_PAUSE%"=="on" pause
+
+if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
+
+cmd /C exit /B %ERROR_CODE%

+ 71 - 0
pom.xml

@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>cn.superdesk</groupId>
+    <artifactId>canal2kafka</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>pom</packaging>
+    <name>canal2kafka</name>
+    <description>canal2kafka</description>
+    <modules>
+        <module>canal2kafka-spring-boot-autoconfigure</module>
+        <module>canal-client</module>
+        <module>canal2kafka-spring-boot-starter</module>
+    </modules>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <java.version>1.8</java.version>
+        <spring-boot.version>2.0.5.RELEASE</spring-boot.version>
+        <canal.version>1.1.4</canal.version>
+        <kafka.version>1.1.1</kafka.version>
+    </properties>
+
+    <developers>
+        <developer>
+            <name>XiaChuang</name>
+            <email>xiachuang@superdesk.cn</email>
+        </developer>
+    </developers>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.5.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <distributionManagement>
+        <repository>
+            <id>nexus-releases</id>
+            <url>https://nexus.superdesk.cn/repository/maven-releases</url>
+        </repository>
+        <snapshotRepository>
+            <id>nexus-snapshots</id>
+            <url>https://nexus.superdesk.cn/repository/maven-snapshots</url>
+        </snapshotRepository>
+    </distributionManagement>
+
+</project>