duxinglangzi
2022-06-24 4c9cf718ceb525f76da1fa4700a6e39bf964ebd8
提交 | 用户 | age
de8c2b 1 # Canal Spring Boot Starter 使用实例
D 2
3 ### 在spring boot 项目配置文件 application.yml内增加以下内容
4 ```yaml
5 spring:
6   canal:
7     instances:
8       example:                  # 拉取 example 目标的数据
9         host: 192.168.10.179    # canal 所在机器的ip
10         port: 11111             # canal 默认暴露端口
11         user-name: canal        # canal 用户名
12         password: canal         # canal 密码
13         batch-size: 600         # canal 每次拉取的数据条数
14         retry-count: 5          # 重试次数,如果重试5次后,仍无法连接,则断开
15         cluster-enabled: false  # 是否开启集群
16         zookeeper-address:      # zookeeper 地址(开启集群的情况下生效), 例: 192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181
17         acquire-interval: 1000  # 未拉取到消息情况下,获取消息的时间间隔毫秒值
18         subscribe: .*\\..*      # 默认情况下拉取所有库、所有表
4c9cf7 19   prod:
D 20     example: example1
21     database: books
de8c2b 22
D 23 ```
24
25 ### 在spring boot 项目中的代码使用实例 
26 ```java
27
28
29 import com.alibaba.otter.canal.protocol.CanalEntry;
30 import com.duxinglangzi.canal.starter.annotation.CanalListener;
31 import com.duxinglangzi.canal.starter.annotation.CanalUpdateListener;
32 import org.springframework.stereotype.Service;
33
34 import java.util.stream.Collectors;
35
36 /**
37  * @author wuqiong 2022/4/12
38  * @description
39  */
40 @Service
41 public class CanalListenerTest {
42
43     /**
627420 44      * 目前 Listener 方法的参数必须为 CanalEntry.EventType , CanalEntry.RowData 
de8c2b 45      * 程序在启动过程中会做检查
D 46      */
a87aa7 47     
D 48     /**
49      * 监控更新操作
50      * 支持动态参数配置,配置项需在 yml 或 properties 进行配置
51      * 目标是 ${prod.example} 的  ${prod.database} 库  users表
52      */
53     @CanalUpdateListener(destination = "${prod.example}", database = "${prod.database}", table = {"users"})
54     public void listenerExampleBooksUsers(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
55         printChange("listenerExampleBooksUsers",eventType, rowData);
56     }
de8c2b 57
D 58     /**
59      * 监控更新操作 ,目标是 example的  books库  users表
60      */
61     @CanalUpdateListener(destination = "example", database = "books", table = {"users"})
62     public void listenerExampleBooksUsers(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
63         printChange("listenerExampleBooksUsers",eventType, rowData);
64     }
65
66     /**
67      * 监控更新操作 ,目标是 example的  books库  books表
68      */
69     @CanalUpdateListener(destination = "example", database = "books", table = {"books"})
70     public void listenerExampleBooksBooks(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
71         printChange("listenerExampleBooksBooks",eventType, rowData);
72     }
73
74     /**
75      * 监控更新操作 ,目标是 example的  books库的所有表
76      */
77     @CanalListener(destination = "example", database = "books", eventType = CanalEntry.EventType.UPDATE)
78     public void listenerExampleBooksAll(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
79         printChange("listenerExampleBooksAll",eventType, rowData);
80     }
81
82     /**
83      * 监控更新操作 ,目标是 example的  所有库的所有表
84      */
85     @CanalListener(destination = "example", eventType = CanalEntry.EventType.UPDATE)
86     public void listenerExampleAll(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
87         printChange("listenerExampleAll",eventType, rowData);
88     }
89
90     /**
91      * 监控更新、删除、新增操作 ,所有配置的目标下的所有库的所有表
92      */
93     @CanalListener(eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE})
94     public void listenerAllDml(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
95         printChange("listenerAllDml",eventType, rowData);
96     }
97
98     public void printChange(String method,CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
99         if (eventType == CanalEntry.EventType.DELETE) {
af6d64 100             rowData.getBeforeColumnsList().stream().collect(Collectors.toList()).forEach(ele -> {
de8c2b 101                 System.out.println("[方法: "+method+" ,  delete 语句 ] --->> 字段名: " + ele.getName() + ", 删除的值为: " + ele.getValue());
D 102             });
103         }
104
105         if (eventType == CanalEntry.EventType.INSERT) {
106             rowData.getAfterColumnsList().stream().collect(Collectors.toList()).forEach(ele -> {
107                 System.out.println("[方法: "+method+" ,insert 语句 ] --->> 字段名: " + ele.getName() + ", 新增的值为: " + ele.getValue());
108             });
109         }
110
111         if (eventType == CanalEntry.EventType.UPDATE) {
112             for (int i = 0; i < rowData.getAfterColumnsList().size(); i++) {
113                 CanalEntry.Column afterColumn = rowData.getAfterColumnsList().get(i);
114                 CanalEntry.Column beforeColumn = rowData.getBeforeColumnsList().get(i);
115                 System.out.println("[方法: "+method+" , update 语句 ] -->> 字段名," + afterColumn.getName() +
116                         " , 是否修改: " + afterColumn.getUpdated() +
117                         " , 修改前的值: " + beforeColumn.getValue() +
118                         " , 修改后的值: " + afterColumn.getValue());
119             }
120         }
121     }
122
123
124 }
125
126
127
128 ```
129
130
131
132