canal简单场景应用Demo

canal之简单场景应用Demo

转载自本人文章:http://techblog.sishuxuefu.com/atricle.html?5dfc918bdd3c1300729ce424

用于数据同步

针对mysql场景进行介绍

参考文档 https://www.bookstack.cn/read/canal-v1.1.4/507a40ad1c1335da.md

非集群-表映射模式

场景:将A库中的表同步到B库中的表,表与表之间可以做字段级别的映射配置

组件:

  1. deployer模拟了myslq从库
  2. adapter提供了具体的映射实现,需要连接到deployer提供的服务
  3. 可选admin,这里不做介绍

deployer

也是canal的server端,伪装成mysql从库

配置文件详细说明可以参考 https://www.bookstack.cn/read/canal-v1.1.4/10a3a22ce51cd92e.md

配置文件
  1. canal.properties配置文件

    此模式下不需要调整,但是可以更改暴露的服务端口

  2. example/instance.properties

    1. 配置主库的ip、port、用户名、密码等

    2. 根据需要配置同步的binlog位置,如果留空,默认是当前位置

    3. canal.instance.filter.regex给出了对表监听的过滤,详细可见本组件连接。

  3. 启动类

    com/alibaba/otter/canal/deployer/CanalLauncher.java

client-adapter

适配器,完成同步工作,需要连接到deployer

此模式下介绍:

  • 完全镜像,类似mysql主从复制

  • 部分映射,可配置表A到表B的映射字段规则

    • 所有字段映射
    • 部分字段映射
配置文件
  1. auncher模块中的application.yml

    1. 配置与deployer的连接
    2. 配置源库和目标库
    3. 为instance命名

    示例文件

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
canalServerHost: 127.0.0.1:11111
batchSize: 500
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
srcDataSources:
defaultDS:
 url: jdbc:mysql://192.168.2.116:13306/avengers_canal?useUnicode=true
 username: canal
 password: canal
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/avengers_test?useUnicode=true
               jdbc.username: root
               jdbc.password: 123456
  1. rdb模块中resources/rdb目录的配置

    示例文件

    dataSourceKey: defaultDS
    destination: example
    groupId: g1
    outerAdapterKey: mysql1
    concurrent: true
    dbMapping:
    database: avengers_canal
    table: config_orders
    targetTable: config_orders
    targetPk:
     id: id
    targetColumns:
     id:
     status:
     name:
     org_id:
     valid_date_start:
     valid_date_end:
     config_orders_type_id:
     config_period_price_id:
     serie:
     tenant_id:
     created_at:
     updated_at:
     created_by:
     updated_by:
     is_delete:
    
    

    dataSourceKey:对应了application.yml中的canal.conf.srcDataSources.defaultDS

    destination:对应了canal.conf.canalAdapters[0].instance

    groupId:canal.conf.canalAdapters[0].groups[0].groupId

    outerAdapterKey:canal.conf.canalAdapters[0].groups[0].outerAdapters[1].key

    dbMapping下配置具体的表映射规则

    • database对应defaultDS下配置的数据库名称
    • table原表
    • targetTable目标表
    • targetPk主键映射
    • targetColumns列映射,需要把主键写上。value不写默认名称一致。
    • mapAll属性为true时,全映射,此时如果有targetColumns,以targetColumns为准
  2. 如果是在源码中调试,如果变动了就需要install到本地库

rest接口
  1. 手动etl

    支持将某个表下的数据从某事件点开始进行同步

    curl -X POST \
    http://127.0.0.1:8081/etl/rdb/mysql1/config_period_price.yml \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    -d params=2018-10-21%2000%3A00%3A00
    

    config_period_price:rdb配置的yml文件名

    rdb:同步方式canal.conf.canalAdapters[0].groups[0].outerAdapters[1].name

    mysql1:canal.conf.canalAdapters[0].groups[0].outerAdapters[1].key

  2. 获取所有instance

    http://127.0.0.1:8081/destinations

  3. 统计某表总数

    http://127.0.0.1:8081/count/rdb/mysql1/config_discount.yml

  4. 同步开关

    curl http://127.0.0.1:8081/syncSwitch/example/off -X PUT
    

admin

运维与配置中心

client

客户端

附录

简单工具集

python批量生成yml工具

https://gitee.com/fantasyzsp/py-yml2md/blob/master/mysql/ReadTables.py

批量请求Etl接口脚本

https://gitee.com/fantasyzsp/py-yml2md/blob/master/mysql/Etl.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值