Skip to content

Commit 24c3d3a

Browse files
authored
Merge pull request #2 from CanalSharp/dev
support docker sample
2 parents 28f3741 + 989631f commit 24c3d3a

6 files changed

Lines changed: 210 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ CanalSharp 是阿里巴巴开源项目 Canal 的 .NET 客户端。为 .NET 开
1414
5. 业务cache刷新
1515
6. 价格变化等重要业务消息
1616

17-
关于 Canal 的更多信息请访问 https://github.com/alibaba/canal
17+
关于 Canal 的更多信息请访问 https://github.com/alibaba/canal/wiki
1818

1919
## 二.如何使用
2020

2121
1.安装Canal
2222

23-
Canal的安装以及配置使用请查看 https://github.com/alibaba/canal
23+
Canal的安装以及配置使用请查看 https://github.com/alibaba/canal/wiki/QuickStart
2424

2525
2.建立一个.NET Core App项目
2626

@@ -40,7 +40,7 @@ var connector = CanalConnectors.NewSingleConnector("127.0.0.1", 11111, destinati
4040
//连接 Canal
4141
connector.Connect();
4242
//订阅,同时传入Filter,如果不传则以Canal的Filter为准。Filter是一种过滤规则,通过该规则的表数据变更才会传递过来
43-
connector.Subscribe("");
43+
connector.Subscribe(".*\\\\..*");
4444
//获取消息并且需要发送Ack表示消费成功
4545
connector.Get(batchSize);
4646
//获取消息但是不需要发送Ack来表示消费成功

docker/docker-compose.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
version: '3.1'
3+
4+
services:
5+
mysql:
6+
image: mysql:5.7
7+
container_name: mysql-5.7
8+
ports:
9+
- "3306:3306"
10+
environment:
11+
- MYSQL_ROOT_PASSWORD=000000
12+
networks:
13+
- canalsharp
14+
volumes:
15+
- ./mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
16+
- ./var/lib/mysql:/var/lib/mysql
17+
- ./mysql/init/:/docker-entrypoint-initdb.d/
18+
19+
20+
canal-server:
21+
image: canal/canal-server:v1.1.0
22+
container_name: canal-server
23+
ports:
24+
- "11111:11111"
25+
networks:
26+
- canalsharp
27+
volumes:
28+
- ./example/:/home/admin/canal-server/conf/example/
29+
- ./canal-server-logs/:/home/admin/canal-server/logs/
30+
depends_on:
31+
- mysql
32+
33+
networks:
34+
35+
canalsharp:
36+
driver: bridge
37+

docker/example/instance.properties

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#################################################
3+
## mysql serverId , v1.0.26+ will autoGen
4+
# canal.instance.mysql.slaveId=0
5+
6+
# enable gtid use true/false
7+
canal.instance.gtidon=false
8+
9+
# position info
10+
canal.instance.master.address=mysql:3306
11+
canal.instance.master.journal.name=mysql-bin.000001
12+
canal.instance.master.position=154
13+
canal.instance.master.timestamp=
14+
canal.instance.master.gtid=
15+
16+
# rds oss binlog
17+
canal.instance.rds.accesskey=
18+
canal.instance.rds.secretkey=
19+
canal.instance.rds.instanceId=
20+
21+
# table meta tsdb info
22+
canal.instance.tsdb.enable=false
23+
#canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb
24+
#canal.instance.tsdb.dbUsername=canal
25+
#canal.instance.tsdb.dbPassword=canal
26+
27+
#canal.instance.standby.address =
28+
#canal.instance.standby.journal.name =
29+
#canal.instance.standby.position =
30+
#canal.instance.standby.timestamp =
31+
#canal.instance.standby.gtid=
32+
33+
# username/password
34+
canal.instance.dbUsername=canal
35+
canal.instance.dbPassword=canal
36+
canal.instance.connectionCharset=UTF-8
37+
38+
# table regex
39+
canal.instance.filter.regex=.*\\..*
40+
# table black regex
41+
canal.instance.filter.black.regex=
42+
#################################################

docker/mysql/init/init.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
CREATE USER canal IDENTIFIED BY 'canal';
3+
GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
4+
FLUSH PRIVILEGES;
5+
6+
CREATE DATABASE `test`;
7+
8+
USE test;
9+
10+
11+
CREATE TABLE `test` (
12+
`id` INT(11) NOT NULL,
13+
`name` VARCHAR(50) NOT NULL
14+
)
15+
ENGINE=InnoDB
16+
;
17+
18+
19+

docker/mysql/mysqld.cnf

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# The MySQL database server configuration file.
3+
#
4+
# You can copy this to one of:
5+
# - "/etc/mysql/my.cnf" to set global options,
6+
# - "~/.my.cnf" to set user-specific options.
7+
#
8+
# One can use all long options that the program supports.
9+
# Run program with --help to get a list of available options and with
10+
# --print-defaults to see which it would actually understand and use.
11+
#
12+
# For explanations see
13+
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
14+
15+
# This will be passed to all mysql clients
16+
# It has been reported that passwords should be enclosed with ticks/quotes
17+
# escpecially if they contain "#" chars...
18+
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
19+
20+
# Here is entries for some specific programs
21+
# The following values assume you have at least 32M ram
22+
23+
[mysqld_safe]
24+
socket = /var/run/mysqld/mysqld.sock
25+
nice = 0
26+
27+
[mysqld]
28+
#
29+
# * Basic Settings
30+
#
31+
user = mysql
32+
pid-file = /var/run/mysqld/mysqld.pid
33+
socket = /var/run/mysqld/mysqld.sock
34+
port = 3306
35+
basedir = /usr
36+
datadir = /var/lib/mysql
37+
tmpdir = /tmp
38+
lc-messages-dir = /usr/share/mysql
39+
skip-external-locking
40+
#
41+
# Instead of skip-networking the default is now to listen only on
42+
# localhost which is more compatible and is not less secure.
43+
bind-address = 0.0.0.0
44+
#
45+
# * Fine Tuning
46+
#
47+
key_buffer_size = 16M
48+
max_allowed_packet = 16M
49+
thread_stack = 192K
50+
thread_cache_size = 8
51+
# This replaces the startup script and checks MyISAM tables if needed
52+
# the first time they are touched
53+
myisam-recover-options = BACKUP
54+
#max_connections = 100
55+
#table_cache = 64
56+
#thread_concurrency = 10
57+
#
58+
# * Query Cache Configuration
59+
#
60+
query_cache_limit = 1M
61+
query_cache_size = 16M
62+
#
63+
# * Logging and Replication
64+
#
65+
# Both location gets rotated by the cronjob.
66+
# Be aware that this log type is a performance killer.
67+
# As of 5.1 you can enable the log at runtime!
68+
#general_log_file = /var/log/mysql/mysql.log
69+
#general_log = 1
70+
#
71+
# Error log - should be very few entries.
72+
#
73+
log_error = /var/log/mysql/error.log
74+
#
75+
# Here you can see queries with especially long duration
76+
#log_slow_queries = /var/log/mysql/mysql-slow.log
77+
#long_query_time = 2
78+
#log-queries-not-using-indexes
79+
#
80+
# The following can be used as easy to replay backup logs or for replication.
81+
# note: if you are setting up a replication slave, see README.Debian about
82+
# other settings you may need to change.
83+
server-id = 1
84+
log_bin = /var/log/mysql/mysql-bin
85+
binlog-format=ROW
86+
expire_logs_days = 10
87+
max_binlog_size = 100M
88+
#binlog_do_db = include_database_name
89+
#binlog_ignore_db = include_database_name
90+
#
91+
# * InnoDB
92+
#
93+
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
94+
# Read the manual for more InnoDB related options. There are many!
95+
#
96+
# * Security Features
97+
#
98+
# Read the manual, too, if you want chroot!
99+
# chroot = /var/lib/mysql/
100+
#
101+
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
102+
#
103+
# ssl-ca=/etc/mysql/cacert.pem
104+
# ssl-cert=/etc/mysql/server-cert.pem
105+
# ssl-key=/etc/mysql/server-key.pem

sample/CanalSharp.SimpleClient/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ static void Main(string[] args)
1818
//连接 Canal
1919
connector.Connect();
2020
//订阅,同时传入Filter,如果不传则以Canal的Filter为准。Filter是一种过滤规则,通过该规则的表数据变更才会传递过来
21-
connector.Subscribe("");
21+
connector.Subscribe(".*\\\\..*");
2222
while (true)
2323
{
2424
//获取消息数据
2525
var message = connector.Get(5000);
2626
var batchId = message.Id;
2727
if (batchId == -1 || message.Entries.Count <= 0)
2828
{
29-
Console.WriteLine("没有数据了.............");
29+
Console.WriteLine("=====没有数据了=====");
3030
Thread.Sleep(300);
31+
continue;
3132
}
3233
PrintEntry(message.Entries);
3334
}
@@ -67,7 +68,7 @@ private static void PrintEntry(List<Entry> entrys)
6768
}
6869
else if (eventType == EventType.Insert)
6970
{
70-
PrintColumn(rowData.BeforeColumns.ToList());
71+
PrintColumn(rowData.AfterColumns.ToList());
7172
}
7273
else
7374
{

0 commit comments

Comments
 (0)