|
|
@@ -0,0 +1,91 @@
|
|
|
+package com.kxs.transfer.api.config;
|
|
|
+
|
|
|
+import com.aliyun.dts.subscribe.clients.ConsumerContext;
|
|
|
+import com.aliyun.dts.subscribe.clients.DTSConsumer;
|
|
|
+import com.aliyun.dts.subscribe.clients.DefaultDTSConsumer;
|
|
|
+import com.aliyun.dts.subscribe.clients.common.RecordListener;
|
|
|
+import com.aliyun.dts.subscribe.clients.record.OperationType;
|
|
|
+import com.aliyun.dts.subscribe.clients.recordprocessor.DbType;
|
|
|
+import com.aliyun.dts.subscribe.clients.recordprocessor.DefaultRecordPrintListener;
|
|
|
+import com.kxs.transfer.api.service.KxsCheckpointService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 分布式 DTS 订阅管道配置
|
|
|
+ *
|
|
|
+ * @author 没秃顶的码农
|
|
|
+ * @date 2024-01-19
|
|
|
+ */
|
|
|
+@Configuration(proxyBeanMethods = false)
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class ConsumerAssignConfig {
|
|
|
+
|
|
|
+ private final KxsCheckpointService kxsCheckpointService;
|
|
|
+
|
|
|
+ // kafka broker url
|
|
|
+ String brokerUrl = "your broker url";
|
|
|
+ // topic to consume, partition is 0
|
|
|
+ String topic = "your dts topic";
|
|
|
+ // user password and sid for auth
|
|
|
+ String sid = "your sid";
|
|
|
+ String userName = "your user name";
|
|
|
+ String password = "your password";
|
|
|
+ // initial checkpoint for first seek(a timestamp to set, eg 1566180200 if you want (Mon Aug 19 10:03:21 CST 2019))
|
|
|
+ String initCheckpoint = "start timestamp";
|
|
|
+ // when use subscribe mode, group config is required. kafka consumer group is enabled
|
|
|
+ ConsumerContext.ConsumerSubscribeMode subscribeMode = ConsumerContext.ConsumerSubscribeMode.ASSIGN;
|
|
|
+ // if force use config checkpoint when start. for checkpoint reset, only assign mode works
|
|
|
+ boolean isForceUseInitCheckpoint = true;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化 初始化消费者
|
|
|
+ *
|
|
|
+ * @return {@link DTSConsumer}
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public DTSConsumer initDTSClient() {
|
|
|
+ ConsumerContext consumerContext = new ConsumerContext(brokerUrl, topic, sid, userName, password, initCheckpoint, subscribeMode);
|
|
|
+
|
|
|
+ //if this parameter is set, force to use the initCheckpoint to initial
|
|
|
+ consumerContext.setForceUseCheckpoint(isForceUseInitCheckpoint);
|
|
|
+
|
|
|
+ //add user store
|
|
|
+ consumerContext.setUserRegisteredStore(new UserMetaStore(kxsCheckpointService));
|
|
|
+
|
|
|
+ DTSConsumer dtsConsumer = new DefaultDTSConsumer(consumerContext);
|
|
|
+
|
|
|
+ dtsConsumer.addRecordListeners(buildRecordListener());
|
|
|
+ dtsConsumer.start();
|
|
|
+ return dtsConsumer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, RecordListener> buildRecordListener() {
|
|
|
+ // user can impl their own listener
|
|
|
+ RecordListener mysqlRecordPrintListener = record -> {
|
|
|
+
|
|
|
+ OperationType operationType = record.getOperationType();
|
|
|
+
|
|
|
+ if(operationType.equals(OperationType.INSERT)
|
|
|
+ || operationType.equals(OperationType.UPDATE)
|
|
|
+ || operationType.equals(OperationType.DELETE)
|
|
|
+ || operationType.equals(OperationType.DDL)
|
|
|
+ || operationType.equals(OperationType.HEARTBEAT)) {
|
|
|
+
|
|
|
+ // consume record
|
|
|
+ RecordListener recordPrintListener = new DefaultRecordPrintListener(DbType.MySQL);
|
|
|
+
|
|
|
+ recordPrintListener.consume(record);
|
|
|
+
|
|
|
+ //commit method push the checkpoint update
|
|
|
+ record.commit("");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return Collections.singletonMap("mysqlRecordPrinter", mysqlRecordPrintListener);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|