1 動作原理
-
canal は、MySQL スレーブの対話プロトコルをシミュレートし、MySQL スレーブとして偽装し、ダンプ プロトコルを MySQL マスターに送信します。
-
MySQL マスターはダンプ リクエストを受信し、バイナリ ログをスレーブ (つまり運河) にプッシュし始めます。
-
canal はバイナリ ログ オブジェクト (元はバイト ストリーム) を解析します。
利点: ビジネス コード、増分ログ サブスクリプションから完全に切り離すことができます。
デメリット: リアルタイム性が高くない mysql ログを購読する DB 内のデータトランザクションが成功すると、canal への同期が開始されます。
2つの運河でMySQLとElasticsearch7間のデータ同期を実現
以下では、MySQL と ES7 の間でデータ同期を実現するための canal および canal アダプターの使用方法を紹介します。
2.1 MySQL 設定の変更
MySQL では、ユーザーを作成し、以下を許可する必要があります。
-- 使用命令登录:mysql -u root -p
-- 创建用户 用户名:canal
create user 'canal'@'%' identified by 'canal';
-- 授权 .表示所有库
grant SELECT, REPLICATION SLAVE, REPLICATION CLIENT on . to 'canal'@'%' identified by 'canal';
次のステップでは、MySQL 構成ファイル my.cnf に次の情報を設定します。
[mysqld]
# 开启binlog
log-bin=mysql-bin
# 选择ROW(行)模式
binlog-format=ROW
# 配置MySQL replaction需要定义,不要和canal的slaveId重复
server_id=1
設定ファイルを変更した後、MySQL を再起動します。
2.2 ダウンロード運河
最新の cana1.1.5 をダウンロードして解凍します。1.1.5 のみが Elasticsearch7 をサポートします。
ダウンロード アドレス:
canal.adapter-1.1.5-SNAPSHOT.tar.gz (アダプター)
canal.deployer-1.1.5-SNAPSHOT.tar.gz (サーバー) ) )
canal.adapter はアダプター、canal.deployer はサーバーです
2.3 Canalサーバーの起動
2.3.1 データベース構成の変更
conf/example ディレクトリに入り、instance.properties をデータベース構成に合わせて変更します。
2.3.2 サーバーの起動
bin ディレクトリに入り、starup.bat をダブルクリックして起動します。次のインターフェイスが表示され、起動が成功したことを示します。
サーバーは正常に起動します。次に、クライアント テストに入ります。
2.4 Canalクライアントの起動
2.4.1 構成の変更
アダプター ディレクトリに入り、application.yml の
yml コンテンツを変更します。
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: rocketMQ #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 127.0.0.1:11111
canal.tcp.zookeeper.hosts:
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
# kafka consumer
kafka.bootstrap.servers: 127.0.0.1:9092
kafka.enable.auto.commit: false
kafka.auto.commit.interval.ms: 1000
kafka.auto.offset.reset: latest
kafka.request.timeout.ms: 40000
kafka.session.timeout.ms: 30000
kafka.isolation.level: read_committed
kafka.max.poll.records: 1000
# rocketMQ consumer
rocketmq.namespace:
rocketmq.namesrv.addr: 127.0.0.1:9876
rocketmq.batch.size: 1000
rocketmq.enable.message.trace: false
rocketmq.customized.trace.topic:
rocketmq.access.channel:
rocketmq.subscribe.filter:
# rabbitMQ consumer
rabbitmq.host:
rabbitmq.virtual.host:
rabbitmq.username:
rabbitmq.password:
rabbitmq.resource.ownerId:
# srcDataSources:
# defaultDS:
# url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true
# username: root
# password: 121212
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
# - name: rdb
# key: mysql1
# properties:
# jdbc.driverClassName: com.mysql.jdbc.Driver
# jdbc.url: jdbc:mysql://127.0.0.1:3306/mytest2?useUnicode=true
# jdbc.username: root
# jdbc.password: 121212
# - name: rdb
# key: oracle1
# properties:
# jdbc.driverClassName: oracle.jdbc.OracleDriver
# jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
# jdbc.username: mytest
# jdbc.password: m121212
# - name: rdb
# key: postgres1
# properties:
# jdbc.driverClassName: org.postgresql.Driver
# jdbc.url: jdbc:postgresql://localhost:5432/postgres
# jdbc.username: postgres
# jdbc.password: 121212
# threads: 1
# commitSize: 3000
# - name: hbase
# properties:
# hbase.zookeeper.quorum: 127.0.0.1
# hbase.zookeeper.property.clientPort: 2181
# zookeeper.znode.parent: /hbase
# - name: es
# hosts: 127.0.0.1:9300 # 127.0.0.1:9200 for rest mode
# properties:
# mode: transport # or rest
# # security.auth: test:123456 # only used for rest mode
# cluster.name: elasticsearch
# - name: kudu
# key: kudu
# properties:
# kudu.master.address: 127.0.0.1 # ',' split multi address
2.4.2 インデックスを作成し、SQL データを Elasticsearch に同期する
http://127.0.0.1:9200/product (PUT リクエスト) を呼び出してインデックスを作成します。product はインデックス名です
{
"mappings" : {
"properties" : {
"attrs" : {
"type" : "nested",
"properties" : {
"attrId" : {
"type" : "long"
},
"attrName" : {
"type" : "keyword"
},
"attrValueId" : {
"type" : "long"
},
"attrValueName" : {
"type" : "keyword"
}
}
},
"tags" : {
"type" : "nested",
"properties" : {
"tagId" : {
"type" : "long"
},
"seq" : {
"type" : "integer"
}
}
},
"brandId" : {
"type" : "long"
},
"brandImg" : {
"type" : "keyword"
},
"brandName" : {
"type" : "keyword"
},
"code" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"commentNum" : {
"type" : "integer"
},
"createTime" : {
"type" : "date"
},
"hasStock" : {
"type" : "boolean"
},
"imgUrls" : {
"type" : "keyword",
"index" : false,
"doc_values" : false
},
"mainImgUrl" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"marketPriceFee" : {
"type" : "long"
},
"priceFee" : {
"type" : "long"
},
"saleNum" : {
"type" : "integer"
},
"sellingPoint" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"shopId" : {
"type" : "long"
},
"shopImg" : {
"type" : "keyword",
"index" : false,
"doc_values" : false
},
"shopName" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"shopType" : {
"type" : "integer"
},
"shopPrimaryCategoryId" : {
"type" : "long"
},
"shopPrimaryCategoryName" : {
"type" : "keyword"
},
"shopSecondaryCategoryId" : {
"type" : "long"
},
"shopSecondaryCategoryName" : {
"type" : "keyword"
},
"primaryCategoryId" : {
"type" : "long"
},
"primaryCategoryName" : {
"type" : "keyword"
},
"secondaryCategoryId" : {
"type" : "long"
},
"secondaryCategoryName" : {
"type" : "keyword"
},
"categoryId" : {
"type" : "long"
},
"categoryName" : {
"type" : "keyword"
},
"spuId" : {
"type" : "long"
},
"spuName" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"spuStatus" : {
"type" : "integer"
},
"success" : {
"type" : "boolean"
}
}
}
}
2.4.3 クライアントの起動
\canal.adapter-1.1.5-SNAPSHOT\bin と入力し、startup.bat をダブルクリックすると、次のインターフェイスが表示され、起動が成功したことが示されます。
3つの注意点
3.1 キューの使用
2.4.1 のクライアント構成ではキューの構成が必要です。私は rocketmq を使用します。必要に応じてキューを選択できます。選択したキューに応じて、構成を変更する必要があります。
3.2 起動シーケンス
最初に Mysql と Elasticsearch を起動し、次に Canal サーバーを起動し、最後にクライアントを起動する必要があります。
4 検証
操作インターフェースで製品を追加すると、この時点で Elasticsearch はこのデータも同期します。詳細は展開されません。