- 使用Canal實(shí)現(xiàn)es數(shù)據(jù)實(shí)時(shí)同步
canal :github地址:https://github.com/alibaba/canal/
原理很簡單:數(shù)據(jù)庫開啟bin-log日志慨亲,calan偽裝成mysql的slave 讀取bin-log日志
官方的說明:
canal 模擬 MySQL slave 的交互協(xié)議,偽裝自己為 MySQL slave 宝鼓,向 MySQL master 發(fā)送dump 協(xié)議
MySQL master 收到 dump 請(qǐng)求刑棵,開始推送 binary log 給 slave (即 canal )
canal 解析 binary log 對(duì)象(原始為 byte 流)
最新Canal版本為 1.1.4 支持 ES7
(1)、 下載canal項(xiàng)目(git命令也可以 - - 隨意.)
(2)愚铡、解壓
(3)蛉签、打開終端 cd到項(xiàng)目目錄
mvn clean install
說明:沒有配置 maven 的 自己百度配置一下 - - .
安裝完成后:打開target文件夾
就可以看到 tar包.
把
canal.deployer-1.1.5-SNAPSHOT.tar.gz
canal.adapter-1.1.5-SNAPSHOT.tar.gz
canal.admin-1.1.5-SNAPSHOT.tar.gz
上傳到 centos .并解壓.
mkdir canal-xxxx
tar -zxf canal.xxxx-1.1.5-SNAPSHOT.tar.gz -C /usr/soft/canal-xxxx
先新建目錄,然后再解壓到相應(yīng)目錄. xxxx 是不同的項(xiàng)目.
cd canal-deployer/conf/example
修改instance.properties
vim instance.properties
還有一些其他配置:稍后再說
最后切換到 bin目錄 start.sh 即可
查看日志啟動(dòng)成功
日志在logs下面:
tail -200f example/example.log
canal1.1.4提供 web-ui
官方說明:
canal 1.1.4版本茂附,迎來最重要的WebUI能力正蛙,引入canal-admin工程,
支持面向WebUI的canal動(dòng)態(tài)管理能力营曼,支持配置乒验、任務(wù)、日志等在線白屏運(yùn)維能力蒂阱,
具體文檔:[Canal Admin Guide](https://github.com/alibaba/canal/wiki/Canal-Admin-Guide)
進(jìn)入到 canal-admin目錄:主要配置在 conf中
首先:創(chuàng)建數(shù)據(jù)庫canal_manager
然后編輯application.yml 修改成自己的數(shù)據(jù)庫信息
修改instance-template.properties
最后切換到 bin目錄 啟動(dòng)即可
訪問路徑:
http://192.168.0.196:8089
賬號(hào):admin
密碼:123456
詳情參考:https://github.com/alibaba/canal/wiki/Canal-Admin-Guide
進(jìn)入 canal-adapter/conf 目錄
這里需要配置 application.yml 和es7(因?yàn)槲覀兪褂玫膃s是7.0.1版本)
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: tcp # kafka rocketMQ
canalServerHost: 192.168.0.196:11111 #canal默認(rèn)地址 可以在 canal-deployer 項(xiàng)目中配置
# zookeeperHosts: slave1:2181
# mqServers: 127.0.0.1:9092 #or rocketmq
# flatMessage: true
batchSize: 500
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
username:
password:
vhost:
srcDataSources:
defaultDS:
url: jdbc:mysql://192.168.0.201:3306/khfooddb?useUnicode=true #需要同步數(shù)據(jù)數(shù)據(jù)庫地址
username: root
password: root
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: es7 #es版本選擇 es6 or es7
hosts: 192.168.0.196: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
編輯 es7/mytest_user.yml
為什么是他呢锻全。。录煤。
因?yàn)椋?/p>
相對(duì)應(yīng)鳄厌,也可以自定義。妈踊。了嚎。
vim es7/mytest_user.yml
dataSourceKey: defaultDS
destination: example
groupId: g1
esMapping:
_index: coremerchant_index # es 的index
# _type: _doc
_id: _id
upsert: true
# pk: id
sql: "select a.id as _id,a.id,a.name,a.status,concat(IFNULL(a.latitude, 0), ',', IFNULL(a.longitude, 0)) AS location from core_merchant a"
# objFields:
# _labels: array:;
# etlCondition: "where t.c_time>={}"
commitBatch: 3000
最后切換到 bin 目錄 startup.sh
然后 tailf ../logs/adapter/adapter.log
查看啟動(dòng)日志
最后進(jìn)行測試- -
自己數(shù)據(jù)庫對(duì)應(yīng)的表 INSERT INTO 一條數(shù)據(jù),看看查看效果.