java學(xué)習(xí)

          java學(xué)習(xí)

           

          canal簡(jiǎn)單安裝使用

          1、數(shù)據(jù)庫配置

          首先使用canal需要修改數(shù)據(jù)庫配置

          [mysqld] 
          log-bin=mysql-bin # 開啟
          binlog binlog-format=ROW # 選擇 ROW 模式
          server_id=1 # 配置 MySQL replaction 需要定義,不要和 canal 的 slaveId 重復(fù)

          創(chuàng)建canal數(shù)據(jù)庫用戶

          CREATE USER canal IDENTIFIED BY 'canal';  
          GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON
          *.* TO 'canal'@'%';
          FLUSH PRIVILEGES;

           

          2、安裝canal

          下載:https://github.com/alibaba/canal/releases

          解壓(修改版本號(hào)):tar zxvf canal.deployer-1.1.4.tar.gz -C ./canal

          配置開放服務(wù)器端口:11110、11111、11112

          修改canal配置文件(這里設(shè)置了兩個(gè)instance,即兩個(gè)數(shù)據(jù)庫):

          vi canal/conf/canal.properties 
          canal.destinations
          = example1,example2

           配置instance:

          cp -R canal/conf/example conf/example1
          mv conf/example conf/example2

          第一個(gè)數(shù)據(jù)庫配置

          vi canal/conf/example1/instance.properties
          canal.instance.master.address
          =32.1.2.140:3306

          第二個(gè)數(shù)據(jù)庫配置

          vi canal/conf/example2/instance.properties
          canal.instance.master.address
          =32.1.2.140:3307

          #如果需要新增一個(gè)instance,只需要修改canal.properties文件,并新增一個(gè)instance配置即可,無需重啟canal。

          運(yùn)行:

          sh canal/bin/startup.sh # 查看日志
          cat canal/logs/canal/canal

           

          3、Java使用樣例

          引入pom依賴,需要與安裝的canal版本一致

          復(fù)制代碼
          <dependencies>     <dependency>         <groupId>com.alibaba.otter</groupId>         <artifactId>canal.client</artifactId>         <version>1.1.4</version>     </dependency> </dependencies>
          復(fù)制代碼

          示例代碼(異步打印兩個(gè)數(shù)據(jù)庫的修改內(nèi)容):

          復(fù)制代碼
          package cn.spicybar.dblog;  import com.alibaba.otter.canal.client.CanalConnector; import com.alibaba.otter.canal.client.CanalConnectors; import com.alibaba.otter.canal.protocol.CanalEntry.Entry; import com.alibaba.otter.canal.protocol.CanalEntry.EntryType; import com.alibaba.otter.canal.protocol.CanalEntry.RowChange; import com.alibaba.otter.canal.protocol.Message;  import java.net.InetSocketAddress; import java.util.List;  public class CanalClient {      public static void main(String[] args) {         new Thread(() -> initConnector("example1")).start();         new Thread(() -> initConnector("example2")).start();     }      private static void initConnector(String destination) {         CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("32.1.0.237", 11111), destination, "", "");         try {             connector.connect();             connector.subscribe(".*\\..*");             connector.rollback();             while (true) {                 Message message = connector.getWithoutAck(1000);                 if (message.getId() != -1 && message.getEntries().size() > 0) {                     printEntry(message.getEntries());                 }                 connector.ack(message.getId());             }         } finally {             connector.disconnect();         }     }      private static void printEntry(List<Entry> entries) {         for (Entry entry : entries) {             if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {                 continue;             }             try {                 RowChange rowChange = RowChange.parseFrom(entry.getStoreValue());                 System.out.println(rowChange.getSql());             } catch (Exception e) {                 throw new RuntimeException("ERROR ## parser error, data:" + entry.toString(), e);             }         }     }

          posted on 2020-08-31 10:55 楊軍威 閱讀(674) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 赣榆县| 墨玉县| 定州市| 乌鲁木齐县| 化州市| 辉南县| 齐齐哈尔市| 菏泽市| 克拉玛依市| 通海县| 桐城市| 名山县| 抚顺市| 永丰县| 永定县| 博罗县| 来宾市| 阳春市| 隆子县| 冕宁县| 南部县| 万安县| 常德市| 名山县| 洛宁县| 铁岭县| 沙田区| 武宣县| 扎赉特旗| 麻阳| 固始县| 神池县| 舟曲县| 临邑县| 永年县| 郯城县| 靖州| 尼勒克县| 德州市| 临邑县| 普兰县|