canal
This commit is contained in:
@@ -37,6 +37,14 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.otter</groupId>
|
||||
<artifactId>canal.client</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>com.tashow.cloud</groupId>
|
||||
@@ -69,6 +77,13 @@
|
||||
<artifactId>tashow-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tashow.cloud</groupId>
|
||||
<artifactId>tashow-data-canal</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- RPC 远程调用相关 -->
|
||||
<dependency>
|
||||
<groupId>com.tashow.cloud</groupId>
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
package com.tashow.cloud.infra.framework;
|
||||
|
||||
|
||||
import com.alibaba.otter.canal.client.CanalConnector;
|
||||
import com.alibaba.otter.canal.client.CanalConnectors;
|
||||
import com.alibaba.otter.canal.protocol.CanalEntry.*;
|
||||
import com.alibaba.otter.canal.protocol.Message;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
@Component
|
||||
public class CanalClient {
|
||||
|
||||
//sql队列
|
||||
private Queue<String> SQL_QUEUE = new ConcurrentLinkedQueue<>();
|
||||
|
||||
|
||||
/**
|
||||
* canal入库方法
|
||||
*/
|
||||
public void run() {
|
||||
CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("43.139.42.137",
|
||||
11111), "example", "", "");
|
||||
int batchSize = 1000;
|
||||
try {
|
||||
connector.connect();
|
||||
// connector.subscribe(".*\\..*");
|
||||
connector.subscribe("tashow-platform");
|
||||
|
||||
connector.rollback();
|
||||
try {
|
||||
while (true) {
|
||||
//尝试从master那边拉去数据batchSize条记录,有多少取多少
|
||||
Message message = connector.getWithoutAck(batchSize);
|
||||
long batchId = message.getId();
|
||||
int size = message.getEntries().size();
|
||||
if (batchId == -1 || size == 0) {
|
||||
Thread.sleep(1000);
|
||||
} else {
|
||||
dataHandle(message.getEntries());
|
||||
}
|
||||
connector.ack(batchId);
|
||||
|
||||
//当队列里面堆积的sql大于一定数值的时候就模拟执行
|
||||
if (SQL_QUEUE.size() >= 1) {
|
||||
executeQueueSql();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} finally {
|
||||
connector.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟执行队列里面的sql语句
|
||||
*/
|
||||
public void executeQueueSql() {
|
||||
int size = SQL_QUEUE.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
String sql = SQL_QUEUE.poll();
|
||||
System.out.println("[sql]----> " + sql);
|
||||
|
||||
this.execute(sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据处理
|
||||
*
|
||||
* @param entrys
|
||||
*/
|
||||
private void dataHandle(List<Entry> entrys) throws InvalidProtocolBufferException {
|
||||
for (Entry entry : entrys) {
|
||||
if(entry.getHeader().getSchemaName().equals("hc")){
|
||||
return;
|
||||
}
|
||||
if (EntryType.ROWDATA == entry.getEntryType()) {
|
||||
RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
|
||||
EventType eventType = rowChange.getEventType();
|
||||
if (eventType == EventType.DELETE) {
|
||||
saveDeleteSql(entry);
|
||||
} else if (eventType == EventType.UPDATE) {
|
||||
saveUpdateSql(entry);
|
||||
} else if (eventType == EventType.INSERT) {
|
||||
saveInsertSql(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存更新语句
|
||||
*
|
||||
* @param entry
|
||||
*/
|
||||
private void saveUpdateSql(Entry entry) {
|
||||
try {
|
||||
RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
|
||||
List<RowData> rowDatasList = rowChange.getRowDatasList();
|
||||
for (RowData rowData : rowDatasList) {
|
||||
List<Column> newColumnList = rowData.getAfterColumnsList();
|
||||
StringBuffer sql = new StringBuffer("update " + entry.getHeader().getTableName() + " set ");
|
||||
for (int i = 0; i < newColumnList.size(); i++) {
|
||||
sql.append(" " + newColumnList.get(i).getName()
|
||||
+ " = '" + newColumnList.get(i).getValue() + "'");
|
||||
if (i != newColumnList.size() - 1) {
|
||||
sql.append(",");
|
||||
}
|
||||
}
|
||||
sql.append(" where ");
|
||||
List<Column> oldColumnList = rowData.getBeforeColumnsList();
|
||||
for (Column column : oldColumnList) {
|
||||
if (column.getIsKey()) {
|
||||
//暂时只支持单一主键
|
||||
sql.append(column.getName() + "=" + column.getValue());
|
||||
break;
|
||||
}
|
||||
}
|
||||
SQL_QUEUE.add(sql.toString());
|
||||
}
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存删除语句
|
||||
*
|
||||
* @param entry
|
||||
*/
|
||||
private void saveDeleteSql(Entry entry) {
|
||||
try {
|
||||
RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
|
||||
List<RowData> rowDatasList = rowChange.getRowDatasList();
|
||||
for (RowData rowData : rowDatasList) {
|
||||
List<Column> columnList = rowData.getBeforeColumnsList();
|
||||
StringBuffer sql = new StringBuffer("delete from " + entry.getHeader().getTableName() + " where ");
|
||||
for (Column column : columnList) {
|
||||
if (column.getIsKey()) {
|
||||
//暂时只支持单一主键
|
||||
sql.append(column.getName() + "=" + column.getValue());
|
||||
break;
|
||||
}
|
||||
}
|
||||
SQL_QUEUE.add(sql.toString());
|
||||
}
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存插入语句
|
||||
*
|
||||
* @param entry
|
||||
*/
|
||||
private void saveInsertSql(Entry entry) {
|
||||
try {
|
||||
RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());
|
||||
List<RowData> rowDatasList = rowChange.getRowDatasList();
|
||||
for (RowData rowData : rowDatasList) {
|
||||
List<Column> columnList = rowData.getAfterColumnsList();
|
||||
StringBuffer sql = new StringBuffer("insert into " + entry.getHeader().getTableName() + " (");
|
||||
for (int i = 0; i < columnList.size(); i++) {
|
||||
sql.append(columnList.get(i).getName());
|
||||
if (i != columnList.size() - 1) {
|
||||
sql.append(",");
|
||||
}
|
||||
}
|
||||
sql.append(") VALUES (");
|
||||
for (int i = 0; i < columnList.size(); i++) {
|
||||
sql.append("'" + columnList.get(i).getValue() + "'");
|
||||
if (i != columnList.size() - 1) {
|
||||
sql.append(",");
|
||||
}
|
||||
}
|
||||
sql.append(")");
|
||||
SQL_QUEUE.add(sql.toString());
|
||||
}
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 入库
|
||||
* @param sql
|
||||
*/
|
||||
public void execute(String sql) {
|
||||
System.out.println("sql======="+sql);
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,11 @@ spring:
|
||||
username: nacos # Nacos 账号
|
||||
password: nacos # Nacos 密码
|
||||
discovery: # 【配置中心】配置项
|
||||
namespace: liwq # 命名空间。这里使用 dev 开发环境
|
||||
namespace: 76667956-2ac2-4e05-906b-4bca4ebcc5f0 # 命名空间。这里使用 dev 开发环境
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
metadata:
|
||||
version: 1.0.0 # 服务实例的版本号,可用于灰度发布
|
||||
config: # 【注册中心】配置项
|
||||
namespace: liwq # 命名空间。这里使用 dev 开发环境
|
||||
namespace: 76667956-2ac2-4e05-906b-4bca4ebcc5f0 # 命名空间。这里使用 dev 开发环境
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
|
||||
|
||||
Reference in New Issue
Block a user